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