C客户端服务器echo程序在客户端中显示无效消息

时间:2013-03-28 02:21:11

标签: c sockets gcc tcp client-server

我也是socket编程和C的新手。我正在创建一个简单的echo客户端服务器应用程序,它在教程后使用线程支持多个客户端。

在应用程序中一切正常,但客户端回声无效消息 它似乎取代了我输入的最长消息的第一个字符

我正在使用Windows 7上的vmware开发Fedora 14终端

这是我编译和运行客户端和服务器的方式

服务器

[sachith@localhost ~]$ gcc -pthread -o s server.c
[sachith@localhost ~]$ ./s
Socket created
bind done
Waiting for incoming connections...
Connection accepted

* 客户端//这是问题*

[sachith@localhost ~]$ gcc client.c -o c
[sachith@localhost ~]$ ./c
Socket created
Connected

Enter message : Hello
Server reply :
Hello
Enter message : 123
Server reply :
123lo
Enter message : x
Server reply :
x23lo
Enter message : y
Server reply :
y23lo
Enter message : 

以下是我的代码

//服务器代码

#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
    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));
    }

    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);

        //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);
    }

    close(sock);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

您必须设置接收缓冲区或在单词的最后位置添加“\ 0”。例如:

这是你的代码:

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, 0x00, 2000);
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));
}

或修改:

while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
    *(client_message + read_size) = '\0';
    //Send the message back to client
    write(client_sock , client_message , strlen(client_message));
}

答案 1 :(得分:0)

我知道这是一个老问题。 我确实看到了一个稍微简单的解决方案。

没有必要做strlen。 由于已知数据的长度,只需重用read_size。

while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
  //Send the message back to client
  write(client_sock , client_message , read_size);
}