C sockets: why is my server appending extra characters in the end?

穿精又带淫゛_ 提交于 2019-12-08 05:20:44

问题


I am writing a simple server/client socket in C. The client asks the user to input a message and the server responds by repeating the same. The problem is when I send a message from the client the server responds back by appending extra character. How do I fix this.

This is my Client code

while(1) {
    bzero(buffer, BUFSIZE);
    printf("Enter Message: ");
    scanf("%s", buffer);
    //send some data
    if(send(socket_fd, buffer, strlen(buffer), 0) <0) {
        fprintf(stderr,"sending failed\n");
        return 1;
    }
//receive a reply from the server
if(recv(socket_fd, server_reply, BUFSIZE,0)<0)
{
    fprintf(stderr,"failed to reply. \n");
    break;
}
fprintf(stdout, "Reply: %s\n ", servreply);

}

This is my server code

 int read_size;
    while((read_size = recv(client_socket_fd, buffer, BUFSIZE,0))>0)
    {

        // Reply back to the client
        if (0 > write(client_socket_fd, buffer, strlen(buffer))) {
            fprintf(stderr, "could not write back to socket\n");
        } else {
            fprintf(stdout, "message back to client\n");
        }
    }
    if(read_size==0)
    {
        fprintf(stderr,"disconnected.\n");
        fflush(stdout);
    }
    else if(read_size==-1){
        fprintf(stderr, "error.\n");
    }

This is the output

 Enter Message: welcome
Reply: welcome
 Enter Message: hello
Reply: hellome
 Enter Message: hi
Reply: hillome

回答1:


You need a string in order to use strlen(). Your arrays are not strings, rely on read_size instead for the length of the buffer.

Strings in c are just a sequence of printable characters followed by a '\0', and none of your arrays has any '\0' so strlen() is causing undefined behavior. The strlen() function actually scans the string until it finds the '\0' and in the process it counts how many characters were there.




回答2:


@Iharob's answer is correct. Basically, change the line:

write(client_socket_fd, buffer, strlen(buffer))

to:

write(client_socket_fd, buffer, read_size)



回答3:


It isn't. You are printing junk at the end of your buffer. You're also ignoring end of stream.

if(recv(socket_fd, server_reply, BUFSIZE,0)<0) {
    fprintf(stderr,"failed to reply. \n");
    break;
}
fprintf(stdout, "Reply: %s\n ", servreply);

should be

int count;
if((count = recv(socket_fd, server_reply, BUFSIZE,0))<0) {
    fprintf(stderr,"failed to reply. \n");
    break;
}
else if (count == 0) {
    // EOS
    fprintf(stderr, "peer has disconnected.\n");
    break;
} else {  
    fprintf(stdout, "Reply: %.*s\n ", count, servreply);
}

Your 'write back to the client' is also incorrect:

if (0 > write(client_socket_fd, buffer, strlen(buffer))) {

should be

if (0 > write(client_socket_fd, buffer, read_size)) {


来源:https://stackoverflow.com/questions/45950202/c-sockets-why-is-my-server-appending-extra-characters-in-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!