我正在尝试编写自己的网络服务器,但我遇到了一些recv()函数的问题。我调试了我的程序,发现每次当我到达这一行时
if (connect(sockfd, (struct sockaddr*) &server_address, sizeof(server_address)) < 0) {
cout << "[Client] Connection failed" << endl;
}
我进入服务器端程序并继续执行程序。而且,尽管我还没有从客户端发送消息,但recv()函数得到了一些东西(但它不应该像这样工作,对吧?recv()应该被阻止,直到客户端的消息来了) 。我检查了这条消息,这个消息绝对是空的。
我的tcp服务器实现:
void tcp_server::connection_handler(int socket) {
char read_buffer[128000];
memset(&read_buffer, 0, sizeof(read_buffer));
while ((static_cast<size_t>(recv(socket, read_buffer, 128000, 0))) > 0) {
cout << "[Server] Client message accepted" << endl;
cout << "[Server] Client message: " << endl << read_buffer << endl;
string converted_message_for_send = convert_client_message(read_buffer);
if (send(socket, converted_message_for_send.c_str(), converted_message_for_send.size(), 0) == -1) {
cout << "[Server] Message send failure" << endl;
}
cout << converted_message_for_send << endl;
cout << "[Server] Message sent to client" << endl << endl;
cout << "============================" << endl << endl;
memset(&read_buffer, 0, 128000);
}
}
void tcp_server::take_requests() {
int client_socket;
while (true) {
client_socket = accept(listener_socket, (struct sockaddr*)& server_address, &socket_length);
cout << "----------------------------" << endl << endl;
cout << "[Server] New connection accepted" << endl << endl;
cout << "----------------------------" << endl << endl;
thread handling_thread(&tcp_server::connection_handler, this, client_socket);
handling_thread.detach();
}
}
我的司机程序:
TEST_CASE("Check base read/send functions","Client") {
unsigned short int PORT = 8080;
int sockfd;
struct sockaddr_in server_address{};
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_address, '0', sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
if (inet_aton("127.0.0.1", &server_address.sin_addr) == 0) {
cout << "[Server] Invalid IP address" << endl;
}
if (connect(sockfd, (struct sockaddr*) &server_address, sizeof(server_address)) < 0) { // im getting in server side program here while debugging
cout << "[Client] Connection failed" << endl;
}
cout << "[Client] All setting are done" << endl;
cout << "[Client] Succefully connected to the server" << endl << endl;
cout << "----------------------------" << endl << endl;
string message = "GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1\n"
"Host: net.tutsplus.com\n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8*\n"
"Accept-Language: en-us,en;q=0.5\n"
"Accept-Encoding: gzip,deflate\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"
"Keep-Alive: 300\n"
"Connection: keep-alive\n"
"Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120\n"
"Pragma: no-cache\n"
"Cache-Control: no-cache\n"
"\n";
if (write(sockfd, message.c_str(), message.size()) == -1) {
cout << "[Client] Message sending failed" << endl;
}
cout << "[Client] Message sent to server" << endl;
char buffer[128000];
memset(&buffer, 0, 128000);
read(sockfd, buffer, 128000);
string received_message = string(buffer);
string proper_response = "HTTP/1.1 404 Not Found\n"
"Content-Length: 26\n"
"\n"
"Oops! Something went wrong\n";
REQUIRE(received_message == proper_response);
cout << "[Client] Server message: " << buffer << endl;
cout << "============================" << endl << endl;
}
所以,第一个问题是:我需要强制recv()等待来自客户端的消息。第二个问题是:当我进入服务器端程序时(在套接字连接步骤中),我需要返回客户端程序(从客户端发送消息)。