这是我的客户端-服务器代码,它一次仅适用于单个用户,客户端可以发送任意数量的消息。
我在服务器上显示收到的消息及其回复(实际上是相反的) - 以及消息“已发送消息”。
在客户端,我显示输入消息和服务器回复:“消息得到”
但是我的代码中存在一个问题:当我尝试发送多条消息时,我在服务器端得到了正确的输出,但在客户端却没有,这只是“得到消息”,但之后 puts 函数它不会打印任何输出到标准输出。
我已经尝试了很多事情,但我还没有找到办法。
是否有任何冲洗到标准输出或我错过了什么?
请告诉我。
server code::::::
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
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( 42969 );
//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);
while(1)
{
//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");
pid_t p=fork();
if(p==0)
{
close(socket_desc);
//Receive a message from client
while( (read_size = recv(client_sock , client_message , sizeof(client_message) , 0)) > 0 )
{
puts(client_message);
char message[20000];
int i,j=0;
for(i=strlen(client_message)-1;i>=0;i--)
message[j++]=client_message[i];
message[j]='\0';
puts(message);
//Send the message back to client
if( send(client_sock , message , sizeof(message),0)>0)
puts("message sent");
// memset(client_message,'\0',sizeof(client_message));
//memset(message,'\0',sizeof(message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
close(client_sock);
exit(1);
}
}
close(socket_desc);
return 0;
}
client code::::
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
// char message[1000] ,
char 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 (42969 );
//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 : ");
char message[1000];
scanf("%s" , message);
//Send some data
if( send(sock , message , sizeof(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if( recv(sock , server_reply , sizeof(server_reply) , 0) > 0)
{
puts("message got");
}
puts("Server reply :");
puts(server_reply);
fflush(stdout);
//memset(server_reply,'\0',sizeof(server_reply));
// memset(message,'\0',sizeof(message));
}
close(sock);
return 0;
}