socket message sending and receiving problem

This is a discussion on socket message sending and receiving problem within the C Programming forums, part of the General Programming Boards category; hi all~ i made a simple socket test and it worked. but it is weired that the message changes both ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    socket message sending and receiving problem

    hi all~

    i made a simple socket test and it worked. but it is weired that the message changes both from client to server and from server to client. for example i send a message "hi" to server and server will get something like "hiU" and if i send this message back to client side i will get a string like "hiU#e" etc, i dont know why and have no idea how to sort it, any help will be grateful, thanks.

    here's my code for message sending and receiving.

    // client side code
    Code:
    ......
    // Send input message to server~
    int len = write(socketId, message, strlen(message));
    if (len < 0)
    {
    printf("Error: Can not send messages to server~\n");
    return -1;
    }
    printf("Message send to server: %s\n", message);
    printf("Message length: %d\n", strlen(message));
    // Blocks until client gets a message back~
    int msgMaxSize = 128;
    char messageBack[msgMaxSize];
    len = read(socketId, messageBack, msgMaxSize);
    if (len < 0)
    {
    printf("Error: Can not get message from server~\n");
    return -1;
    }
    printf("Message back from server: %s\n", messageBack);
    printf("Message length: %d\n", strlen(messageBack));
    ......
    // server side code:
    Code:
    ......
    // Handle message from the client~
    int msgMaxSize = 128;
    char message[msgMaxSize];
    len = read(clientSocket, message, msgMaxSize);
    if (len < 0)
    {
    printf("Error: Can not read data from client socket~\n");
    return -1;
    }
    printf("Message from client: %s\n", message);
    printf("Message length: %d\n", strlen(message));
    char messageTo[strlen(message)];
    strcpy(messageTo, message);
    len = write(clientSocket, messageTo, strlen(messageTo));
    if (len < 0)
    {
    printf("Error: Can not write data to client socket~\n");
    return -1;
    }
    printf("Message to client: %s\n", messageTo);
    printf("Message length: %d\n", strlen(messageTo));
    ......
    Never end on learning~

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Include room for the null character. Most garbled strings are because you forgot it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    weird. it is not correct even we count null chars. i sent a message "hello" which lengths should be 5(or 6 if a null char considered) but actually server got a string with 7 chars, and if we send those 7 chars back to client which may result in a string of 9 chars. is there any special character appended to the message automatically ?
    Never end on learning~

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    How are you getting the original message, user input?

    Is your message actually "hello" + LF + NULL = 7?

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    from command line arguments, say if i start server like this:
    serversocket.c 12345
    it means server may start with local address and listen at port 12345;

    and client side may type a command as follows:
    clientsocket.c 192.168.1.20 12345 hi
    which means client side would like to send a message "hi" to server which address is 192.168.1.20 and listens at port 12345;

    and i use several printf to see message sending and receiving details, finally i found there's something wrong with message length, for example:
    1. client side sends a "hello" to server;
    2. server actually gets a "helloU " (the last char is one could not be displayed which asc code is -86);
    3. server send that string back to client;
    4. client gets a string like "helloU %" (with two invisible chars in the middle, which asc code are -86 and -65 respectively).

    any help ?
    Last edited by black; 01-15-2007 at 04:38 AM.
    Never end on learning~

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    When you send the message you are using strlen(msg) rather than strlen(msg) + 1 (to include the terminatingNULL). You use strcpy() to move the message, but the "sender" didn't put a NULL on the end of the message, so the strcpy() will read the "receive" buffer until it finds a NULL (or causes a memory fault). The spurious characters you are reading are garbage from the receive buffer, not actually sent by the "sender". (Which is what quzah was hinting at in #2)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending / Receiving Info Via TCP or UDP
    By bengreenwood in forum C Programming
    Replies: 0
    Last Post: 03-24-2009, 03:17 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. socket error?
    By yahn in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2006, 10:08 PM
  5. CSocket MFC Problem
    By nvoigt in forum Windows Programming
    Replies: 9
    Last Post: 11-15-2005, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21