gpt4 book ai didi

C 回显服务器,回显一个句子后不能正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 07:25:34 25 4
gpt4 key购买 nike

在测试这段代码时,我发现这个回显服务器对一个词工作得很好。如果尝试回显一个句子,服务器也会回显它,但在那之后它会回显输入与前一个回显句子的最后一个词。我使用 memset 清除了缓冲区,但仍然遇到同样的问题。无法理解问题出在哪里。

服务器代码

#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write

int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");

//Receive a message from client
memset(client_message, 0, sizeof(client_message));
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{


//Send the message back to client
write(client_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");
}

return 0;
}

客户端代码:

#include<stdio.h>   
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>

int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");

server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}

puts("Connected\n");

//keep communicating with server
while(1)
{
printf("Enter message : ");
//scanf("%s" , message);
fgets(message, sizeof(message), stdin);

//Send some data
if( send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}

//Receive a reply from the server
if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}

puts("Server reply :");
puts(server_reply);

memset(message, 0, sizeof(message));
memset(server_reply, 0, sizeof(server_reply));
}

close(sock);
return 0;
}

示例输出

Enter message : sample
Server reply :
sample

Enter message : sample test
Server reply :
sample test

Enter message : gamer
Server reply :
gamer
test

Enter message : pro
Server reply :
pro
r
test

最佳答案

你的问题是这一行:

write(client_sock , client_message , strlen(client_message));

您的客户端不发送 \0 终止符。因此,如果您首先发送一个长字符串,然后发送一个短字符串,那么短字符串将只覆盖开头。将其更改为:

write(client_sock , client_message , read_size); // send exactly as much as you got

并且在客户端只打印收到的数量:

ssize_t answer_size;
if((answer_size = recv(sock , server_reply , sizeof(server_reply) , 0)) < 0)
{
puts("recv failed");
break;
}

printf("Server reply :\n%.*s\n", answer_size, server_reply);

另外请记住,由于具体实现原因,您可能会或不会收到第一个 recv 发送的所有内容,建议在 while 中运行它循环(必要时使用非阻塞模式)。 send 也是如此。

关于C 回显服务器,回显一个句子后不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com