我有一台服务器,它使用HTTP请求发布到我的网站并等待响应。程序中名为heartbeat()的函数将每3秒向我的网站发送一次HTTP请求。
我无法设法获取HTTP请求库,因此我必须在线查看C ++中的自定义代码才能完成。它工作但我发现,如果我在2天后离开服务器,我的网络完全放慢了速度。是的,这肯定是我的服务器这样做,因为我一直在看我的网络,如果我没有我的服务器2天,这是完全没问题的。这是服务器。
老实说我真的迷失了其他可能造成这种情况的原因,所以我想知道是否有人可以查看我的心跳/ http功能,看看是否有任何可能导致某种数据包的缺陷自从此功能每3秒调用一次后,泄漏或可能会在2天后减慢网络速度的事情。
void loop(){
...
while(1){
if(heartClock.getElapsedTime().asSeconds() >= 3){
heartClock.restart();
sf::Thread heartThread(std::bind(&MasterServerHandler::heartbeat_server, cst::masterIP, tcp_port, udp_port, match->num_players(), false));
heartThread.launch();
}
}
}
/*
heartbeat_server: Posts a heartbeat to the master server list to let it know this server is still online.
masterServerIP: IP of the website to post HTTP request to
tcp_port: Posting the TCP port of the server to the master server list
udp_port: Posting the UDP port of the server to the master server list
curPlayers: Posting how many players are currently connected to the server
viewResponse: Whether or not we want to view the response back
*/
void MasterServerHandler::heartbeat_server(std::string masterServerIP, int tcp_port, int udp_port, int curPlayers, bool viewResponse){
if(cst::postServer == false) return;
std::cout << "heartbeat!" << std::endl;
std::mutex mut;
mut.lock();
std::string file = "heartbeat.php";
std::string tcp_port_s = "";
std::string udp_port_s = "";
std::string curPlayers_s = "";
stringstream sPort;
sPort << tcp_port; tcp_port_s = sPort.str(); sPort.str(std::string());
sPort << udp_port; udp_port_s = sPort.str(); sPort.str(std::string());
sPort << curPlayers; curPlayers_s = sPort.str(); sPort.str(std::string());
std::string data = "?tcp_port=" + tcp_port_s + "&udp_port=" + udp_port_s + "&curPlayers=" + curPlayers_s;
std::string response = post_data(file, data);
if(viewResponse){
cout << "WEB RESPONSE: " << endl;
cout << response << endl;
}
mut.unlock();
}
/*
post_data: Posts data via an HTTP request
file: The file to post data to
data: The data to post
*/
std::string MasterServerHandler::post_data(std::string file, std::string data){
string request;
string response;
int resp_leng;
char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;
WSADATA wsaData;
const char* ipaddress = cst::masterIP.c_str();
std::string host = cst::masterHost;
int port = 80;
request+="GET /masterserverlist/" + string(file) + data + " HTTP/1.0\r\n";
request+="Host: " + host + "\r\n";
request+="\r\n";
//open socket
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
cout << "WEB socket() failed" << endl;
//connect
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
serveraddr.sin_port = htons((unsigned short) port);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
cout << "WEB connect() failed" << endl;
//send request
if (send(sock, request.c_str(), request.length(), 0) != request.length())
cout << "WEB send() sent a different number of bytes than expected" << endl;;
//get response
response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
//note: download lag is not handled in this code
}
return response;
}
或者,如果有人知道更好的HTTP请求函数,那就太棒了。