gpt4 book ai didi

c++ - 无法按 NULL 字节 (\0) 拆分

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:14 24 4
gpt4 key购买 nike

我在使用 C++ 和套接字时遇到问题。我正在尝试拆分(基本上,读取)NULL 字节 ("\0"),因为这是 Flash 终止的内容。我也在尝试用这个来写。据我所知,拆分工作正常。

我的问题是我无法将 PACKET +\0 成功写入 Flash 客户端,我确信问题出在服务器端。我是 C++ 的新手,所以我无法调试,而且我在其他地方几乎没有帮助(通常是因为我问过的人不知道),或者我被那些人指到这里。在此先感谢您对此事的任何帮助。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sstream>
#include <string>
#include <thread>
int intPort = 8080;
int resSocket;
struct sockaddr_in strctAddr;
void log(std::string strText, std::string strType = "INFO"){
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 50, "%c",timeinfo);
std::cout << "[" << buffer << "][" << strType << "] > " << strText << std::endl;
}
void error(std::string strError){
log(strError, "SHUTDOWN");
exit(1);
}
/* initialize() method
* Starts up server by setting up a socket() and bind()ing to intPort to listen() for clients
*/
int initialize(int intPort){
std::stringstream objStringStream;
objStringStream << intPort;
log("Initializing socket server");
resSocket = socket(AF_INET, SOCK_STREAM, 0);
if(resSocket < 0) error("Could not create socket.");
bzero((char *) &strctAddr, sizeof(strctAddr)); //I'm pretty sure this clears strctAddr or something
strctAddr.sin_family = AF_INET; //sets socket as AF_INET
strctAddr.sin_addr.s_addr = INADDR_ANY; //sets socket address to "0"
strctAddr.sin_port = htons(intPort); //sets socket port to the value of intPort
setsockopt(resSocket, SOL_SOCKET, SO_REUSEADDR, (struct sockaddr *) &strctAddr, sizeof(strctAddr));
if(bind(resSocket, (struct sockaddr *) &strctAddr, sizeof(strctAddr)) < 0) //Binds socket
error("Could not bind");
listen(resSocket, 5);
log("Listening for clients on " + objStringStream.str(), "FINE");
return 1;
}
/* write()'s strData to resSock */
int sendPacket(int resSock, char* strData){
int intWrite;
strcat(strData, "\0");
log("Sending packet: " + std::string(strData), "SEND");
intWrite = write(resSock, strData, strlen(strData));
return intWrite;
}
/* handles resSock's data (strData) */
void handleData(int resSock, char* strData){
char * chData;
chData = strtok(strData, "\0");
while(chData != NULL){
std::string strPacket = chData;
log("Received data: " + std::string(strPacket), "RECV");
if(strPacket.compare("<policy-file-request/>") == 0){
log("Policy request received");
std::string strSend = "<cross-domain-policy><allow-access-from domain='*' to-ports='*'/></cross-domain-policy>";
char chSend[6486];
strcpy(chSend, strSend.c_str());
sendPacket(resSock, chSend);
}
chData = strtok(NULL, "\0");
}
}
/* handles resSock's incoming data and disconnections */
void handleClient(int resSock){
char chBuffer[6486];
int intRead;
while(true){
bzero(chBuffer, 6486);
intRead = read(resSock, chBuffer, 6486);
if(chBuffer == NULL) continue;
if(intRead <= 0){
log("Client disconnected");
close(resSock);
break;
} else {
handleData(resSock, chBuffer);
}
}
}
/* accept()'s incomming connections and spawns a thread for each of them */
void listenToClients(){
while(true){
std::stringstream objStringStream;
struct sockaddr_in clntAddr;
socklen_t intClients = sizeof(clntAddr);
int resClient = accept(resSocket, (struct sockaddr *) &clntAddr, &intClients);
if(resClient < 0) log("Failed to accept client", "ERROR");
char floatIP[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &clntAddr.sin_addr, floatIP, sizeof floatIP);
objStringStream << floatIP;
log("New client connected (IP: " + objStringStream.str() + ")");
std::thread objThread(handleClient, resClient);
objThread.detach();
}
}
int main(){
initialize(intPort);
listenToClients();
return 0;
}

最佳答案

1 删除什么都不做的 strcat(strData, "\0");

2 将写入更改为:

write(resSock, strData, strlen(strData)+1);

其中将包含您要查找的 NUL 字符。

3 将sendPacket的签名改为int sendPacket(int resSock, const char* strData)

4 将sendPacket的调用改为

std::string strSend = "<cross-domain-policy><allow-access-from domain='*' to-ports='*'/></cross-domain-policy>";
sendPacket(resSock, strSend.c_str());

或者更简单:

sendPacket(resSock, "<cross-domain-policy><allow-access-from domain='*' to-ports='*'/></cross-domain-policy>");

5 最后,重组所有内容以传递 const std::string&,并在最后可能的时刻调用 c_str()。在编写良好的 C++ 程序中几乎没有空指针的空间。

关于c++ - 无法按 NULL 字节 (\0) 拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com