2

我也是套接字编程和 C 的新手。我正在创建一个简单的回显客户端服务器应用程序,它的操作方式如下:**如果客户端发送“hi”----服务器接收“wellcome”
如果客户端发送“再见”-----服务器接收“见”

否则,如果我发送任何其他字符串,我将接收我发送的内容**

这是我的服务器:

#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];
   char wellcome[]="wellcome", seeyou[]="see you",hi[]="hi",bye[]="bye";

   //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( 8882 );

   //Bind
   if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
   {
       //print the error message
       printf("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)
   {
       printf("accept failed");
       return 1;
   }
   puts("Connection accepted");

   //Receive a message from client
   while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
   {
       //Send the message back to client
       *(client_message + read_size) = '\0';
    //*(wellcome + read_size) = '\0';
    //*(seeyou + read_size) = '\0';
    if(strcmp(client_message,hi)==0)
        write(client_sock, wellcome, strlen(wellcome));
    else
        if (strcmp(client_message,bye)==0)
            write(client_sock, seeyou, strlen(seeyou));


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

   if(read_size == 0)
   {
       puts("Client disconnected");
       fflush(stdout);
   }
   else if(read_size == -1)
   {
       printf("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("cound'n 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( 8792 );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        printf("connection error");
        return 1;
    }

    puts("Connected\n");

    //keep communicating with server
    while(1)
    {
        printf("write your message : ");
        gets(message);

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

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

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

    close(sock);
    return 0;
}

我运行它的服务器:

[root@localhost chat]# gcc -o newser newser.c
[root@localhost chat]# ./newser
Socket created
bind done
Waiting for incoming connections...
Connection accepted

这是客户的问题:

[root@localhost chat]# ./client 127.0.0.1
Socket created
Connected

write your message : hi
Server reply :
wellcome
write your message : bye
Server reply :
hi
write your message : bye
Server reply :
see youbye
write your message : hey buddy
Server reply :
see youbye
write your message : hey ya
Server reply :
hey buddy
write your message : 

你可以看到:如果我打招呼,服务器回复 wellcome。如果我说再见,服务器回复你好。如果我说再见增益,服务器回复见你(它不是我期待的)。

4

2 回答 2

2

我想,问题出在这里:

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

为什么要向客户端发送客户端消息?

另一个问题可能来自部分写入:检查写入调用的返回值。

请使用 fgets 而不是gets。请参见获取 (3)。

于 2013-10-15T10:32:49.927 回答
1

在服务器代码中:

if(strcmp(client_message,hi)==0)
    write(client_sock, wellcome, strlen(wellcome));
else if (strcmp(client_message,bye)==0)
    write(client_sock, seeyou, strlen(seeyou));
#You don't want to do this for above 2 cases so add else 
else 
    write(client_sock , client_message , strlen(client_message));
于 2013-10-15T10:38:10.080 回答