嗨,我有网络编程的问题。是否可以使用接收特定消息recv()
并忽略同时发送的其他消息是可能的。假设服务器和客户端已经连接。
服务器.c
sprintf(client_message, "%s hello this is test message 1", packages.userName);
write(connfd , client_message , strlen(client_message)); //Send the message back to client
sprintf(client_message, "%s hello this is test message 2", packages.userName);
write(connfd , client_message , strlen(client_message)); //Send the message back to client
sprintf(client_message, "%s hello this is test message 3", packages.userName);
write(connfd , client_message , strlen(client_message)); //Send the message back to client
client.c (但我只想收到第二条消息)
char server_reply[2000];
int received_bytes = 0;
int remaining_bytes = sizeof(server_reply);
while (remaining_bytes > 0) {
int res = recv(sockfd , &server_reply[remaining_bytes] , remaining_bytes, 0);
if (res < 0) {
printf("Connection lost from server...\n");
isconnected = 0;
close(sockfd);
break;
}
received_bytes += res;
remaining_bytes -= res;
}
puts(server_reply);
谁能帮忙谢谢