1

我遇到了 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;
}
4

3 回答 3

1

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

2 将写入更改为:

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

这将包括您正在寻找的 NUL 字符。

3 将签名更改sendPacketint 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++ 程序中几乎没有裸指针的空间。

于 2012-08-14T21:35:29.947 回答
0

该行strcat(strData, "\0");不做任何事情,因为"\0"它是一个空字符串 - C 风格的字符串总是在第一个停止,\0无论它包含多少。将单个字符附加到std::string.

于 2012-08-14T21:31:54.363 回答
0

完全重写答案

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

那边的strcpy没用。像这样使用它并修改您的 sendPacket 函数以发送strlen(strData)+1(因为字符串的 .c_str 末尾已经有\0NULL 终止符。但 strlen 只计数那个,不包括空值。

但是,您可能只希望此标头部分具有此行为。

于 2012-08-14T21:34:46.300 回答