如果我在一段时间后发送缓冲区,我的客户端正确发送到服务器.但是如果我在没有间隔的情况下发送消息,则服务器会收到一个缓冲区.例如,如果我发送两个缓冲区"message1"和"message2"并在这两个发送操作服务器之间放置1秒睡眠,则接收2个缓冲区并打印message1"\n"message2.但是如果我在没有睡眠的情况下发送这两个缓冲区服务器获得1个缓冲区并打印message1message2.我无法理解为什么会发生这种情况.我的服务器和客户端代码如下.
server.c
#include#include //strlen #include //strlen #include #include //inet_addr #include //write #include //for threading , link with lpthread void *connection_handler(void *); int main(int argc, char *argv[]) { int socket_desc, new_socket, c, *new_sock; struct sockaddr_in server, client; char *message; //Create socket socket_desc = socket(AF_INET, SOCK_STREAM, 0); if (socket_desc == -1) { printf("Could not create socket"); } //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(10240); //Bind if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) { puts("bind failed"); return 1; } puts("bind done"); //Listen listen(socket_desc, 3); //Accept and incoming connection puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); while ((new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))) { puts("Connection accepted"); //Reply to the client // message = "Hello Client , I have received your connection. And now I will assign a handler for you\n"; // write(new_socket, message, strlen(message)); pthread_t sniffer_thread; new_sock = malloc(1); *new_sock = new_socket; if (pthread_create(&sniffer_thread, NULL, connection_handler, (void*)new_sock) < 0) { perror("could not create thread"); return 1; } //Now join the thread , so that we dont terminate before the thread //pthread_join( sniffer_thread , NULL); puts("Handler assigned"); } if (new_socket<0) { perror("accept failed"); return 1; } while (1); return 0; } void *connection_handler(void *socket_desc) { //Get the socket descriptor int sock = *(int*)socket_desc; int read_size; char *message, client_message[2000]; //Send some messages to the client // message = "Greetings! I am your connection handler\n"; // write(sock, message, strlen(message)); // // message = "Now type something and i shall repeat what you type \n"; // write(sock, message, strlen(message)); //Receive a message from client while ((read_size = recv(sock, client_message, 2000, 0)) > 0) { puts(client_message); //Send the message back to client //write(sock, client_message, strlen(client_message)); memset(client_message, 0, sizeof(client_message)); } if (read_size == 0) { puts("Client disconnected"); fflush(stdout); } else if (read_size == -1) { perror("recv failed"); } //Free the socket pointer free(socket_desc); return 0; }
client.c
#include#include //strlen #include #include //inet_addr int main(int argc, char *argv[]) { int socket_desc; struct sockaddr_in server; char *message, server_reply[2000]; //Create socket socket_desc = socket(AF_INET, SOCK_STREAM, 0); if (socket_desc == -1) { printf("Could not create socket"); } server.sin_addr.s_addr = inet_addr("192.168.246.26"); server.sin_family = AF_INET; server.sin_port = htons(10240); //Connect to remote server if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) { puts("connect error"); return 1; } puts("Connected\n"); //Send some data message = "nayan"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "Mazhar vai"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "Sarwar vai"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "joy"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "sumon"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "rahim"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "karim"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "jamal"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "kamal"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } /*sleep(1);*/ message = NULL; message = "salam"; if (send(socket_desc, message, strlen(message), 0) < 0) { puts("Send failed"); return 1; } //Receive a reply from the server // if (recv(socket_desc, server_reply, 2000, 0) < 0) // { // puts("recv failed"); // } // puts("Reply received\n"); // puts(server_reply); return 0; }
Some program.. 5
TCP是一种流协议(如使用所示SOCK_STREAM
).这意味着没有数据包的开始或结束,它只是一个字节流.这也意味着单个接收呼叫的读取数据可能少于使用单个发送呼叫发送的数据,或者使用多个(包括尾随部分)发送呼叫发送的数据.
要处理它,你需要提出一个更高级别的协议来放在TCP之上.它需要包含正在发送的数据的长度(以固定长度格式),或者您需要有其他方式来知道消息的开头或至少结束.
TCP是一种流协议(如使用所示SOCK_STREAM
).这意味着没有数据包的开始或结束,它只是一个字节流.这也意味着单个接收呼叫的读取数据可能少于使用单个发送呼叫发送的数据,或者使用多个(包括尾随部分)发送呼叫发送的数据.
要处理它,你需要提出一个更高级别的协议来放在TCP之上.它需要包含正在发送的数据的长度(以固定长度格式),或者您需要有其他方式来知道消息的开头或至少结束.