Thread: Why does server receive wrong message from client?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    Why does server receive wrong message from client?

    I successfully opened a server and client socket in non-blocking mode.

    Why if I call the client send function twice and then the server receive function once, the server only receives the first client message, but says it has received a total number of bytes equal to message 1 and message 2?

    Thanks.

    Main
    Code:
    // Send Client
    strcpy(Cbuffer, "Client says hello server!!!");
    n = sendIPv6J(Csockfd, Cbuffer, &errnoIn);
    printf("\nClient: SENT MSG : %s : of size %d - errno = %i\n", Cbuffer, n, errnoIn);
    
    // Send Client
    strcpy(Cbuffer, "Client says hello server2!!");
    n = sendIPv6J(Csockfd, Cbuffer, &errnoIn);
    printf("\nClient: SENT MSG : %s : of size %d - errno = %i\n", Cbuffer, n, errnoIn);
    
    // Recv Server
    strcpy(buffer, "default server recv msg");
    n = recvIPv6J(newsockfd, buffer, 255, &errnoIn);
    printf("\nServer: RECEIVED MSG : %s : of size %d - errno = %i\n", buffer, n, errnoIn);

    Functions
    Code:
    // Send Data
    
    int sendIPv6J(int sockfd, char buffer[], int *errnoOut) {
    
    
     int n;
    
     n = send(sockfd, buffer, strlen(buffer) + 1, 0);
    
    if(n == -1) {
    
       *errnoOut = errno;
    
        return(-1);
    
    
       } else {
    
       *errnoOut = 0;
    
        return(n);
    
          }
    
    }
    
    // END
    
    
    // Recv Data
    int recvIPv6J(int sockfd, char buffer[], int maxBufferLength, int *errnoOut) {
    
      int n;
    
          n = recv(sockfd, buffer, maxBufferLength, 0);
    
      if(n == -1) {
    
       *errnoOut = errno;
    
        return(-1);
    
    
      } else {
    
       *errnoOut = 0;
    
        return(n);
    
          }
    
    }
    // END
    
    Last edited by JHugh; 08-11-2017 at 08:40 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Why can't you post code properly? It's not difficult. Just make sure you post it as plain text.

    And the whole "errnoOut" thing seems pointless to me. What's wrong with:
    Code:
    int sendIPv6J(int sockfd, char buffer[]) {
        return send(sockfd, buffer, strlen(buffer) + 1, 0);
    }
    // ...
        strcpy(Cbuffer, "Client says hello server!!!");
        n = sendIPv6J(Csockfd, Cbuffer);
        if (n < 0) {
            perror("sendIPv6J");
            exit(EXIT_FAILURE); // or whatever
        } else {
            printf("Client: SENT MSG : %s : of size %d\n",
                   Cbuffer, n);
        }
    (Notice how the code looks better when posted properly?)

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Quote Originally Posted by algorism View Post
    Why can't you post code properly? It's not difficult. Just make sure you post it as plain text.

    And the whole "errnoOut" thing seems pointless to me. What's wrong with:
    Code:
    int sendIPv6J(int sockfd, char buffer[]) {
        return send(sockfd, buffer, strlen(buffer) + 1, 0);
    }
    // ...
        strcpy(Cbuffer, "Client says hello server!!!");
        n = sendIPv6J(Csockfd, Cbuffer);
        if (n < 0) {
            perror("sendIPv6J");
            exit(EXIT_FAILURE); // or whatever
        } else {
            printf("Client: SENT MSG : %s : of size %d\n",
                   Cbuffer, n);
        }
    (Notice how the code looks better when posted properly?)
    Sorry; however, I can't post code properly because I'm on a work computer. It has a bunch of restrictions and I'm locked into only using an old version of IE with not updated plugins.

    I'd format my code by hand, but on top of that my internet is snail slow, so if I want to type any message on this forum, I need to hold the key down for a while or it doesn't type. And, this textbox refuses to scroll with the mouse wheel. It's very frustrating for me also.

    The *errnoOut thing is something that I have to do to get my code to work with an external program.

    Any idea why my main program is acting the way it is?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Well at least there's a possible explanation. That makes me feel a bit better about it. One thing you might try is to first copy/paste your text into notepad, then copy/paste it from there to the forum. That may be useless, but depending on exactly where your code is coming from in the first place it might help.

    About the errno thing, have you tried it the other way? Every process will have it's own errno, and shared object code is mapped into each process separately. Even if your program is multi-threaded, errno is thread local. I don't see the problem. It just makes your code more cumbersome to use since the user needs to define an extra int to hold the error code (which will usually not be needed).

    As for the problem you're having, how would you know it hasn't received both messages? Presumably it has since it's reporting the total length. However, both messages end with a '\0', so simply printing the buffer will only print the first message. Try:
    Code:
    printf("\nServer: RECEIVED MSG: %s\n", buffer);
    printf("\nServer: RECEIVED MSG: %s\n", buffer + strlen(buffer) + 1);

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Thanks!

    That makes sense.

    Do you know of a quick/elegant way to remove all null characters from a string like buffer except the last one?

    E.g. buffer = "hi\0bye\0" becomes "hibye\0"
    Last edited by JHugh; 08-11-2017 at 10:44 AM.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Elegance is in the eye of the beholder.

    One possibility is to not send the '\0' chars in the first place. You could use '/' to indicate the end of the separate strings and then if you wanted you could add a final '\0' at position n (the value returned by recv function).

    If you don't want to do that, do you really want to remove them or might it be better to replace them with a char such as '/'.
    Code:
    // len includes the final '\0' (i.e., it is the value returned by the recv function)
    void replace_null_bytes(char *s, int len) {
        for (int i = 0; i < len - 1; i++)
            if (s[i] == '\0')
                s[i] = '/';
    }
    If you really need to remove all but the last one, then something like:
    Code:
    void remove_null_bytes(char *s, int len) {
        for (int i = 0; i < len - 1; i++)
            if (s[i] == '\0') {
                // must use memmove (not memcpy) because we
                // are moving bytes within the same object
                len--;
                memmove(s + i, s + i + 1, len - i);
            }
    }
    It would be possible to optimize this somewhat, but you would only do that if necessary.

  7. #7
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Thanks a lot! It worked perfectly and elegantly.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by JHugh View Post
    Thanks a lot! It worked perfectly and elegantly.
    Actually running it shows that the original code above won't work for two '\0' chars in a row. This fixes it.
    Code:
    void remove_null_bytes(char *s, int len) {
        for (int i = 0; i < len - 1; )
            if (s[i] == '\0') {
                len--;
                memmove(s + i, s + i + 1, len - i);
            }
            else
                i++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-19-2011, 10:14 PM
  2. Client to Server Communication using a Message Queue
    By cr80expert5 in forum C Programming
    Replies: 2
    Last Post: 05-09-2011, 11:35 PM
  3. IPv6 multicast client fails to receive server response
    By perfect in forum Networking/Device Communication
    Replies: 10
    Last Post: 06-21-2009, 11:30 PM
  4. Receive multiple data- client- server
    By Micko in forum Networking/Device Communication
    Replies: 7
    Last Post: 08-10-2004, 12:17 PM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM

Tags for this Thread