C++ とソケットに問題があります。これはFlashが終了するものであるため、NULLバイト(「\ 0」)を分割(基本的には読み取り)しようとしています。私もこれで書こうとしています。私が知る限り、分割は正常に機能しています。
私の問題は、Flash クライアントに PACKET + \0 を正常に書き込めないことです。この問題はサーバー側にあると確信しています。私は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;
}