Thread: sending byte <= -112

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    104

    sending byte <= -112

    Hello,

    I have a simple networking application. Everything runs perfectly well. The server is programmed in C, while the client is programmed in Java.

    The only thing is that when the server tries to send a byte <= then -112, the client receives it as 63. Any byte greater than -112 sends successfully.

    Is there a specific reason as to why this is occurring?

    Thank you,
    abraham2119

    EDIT: I was during some tesing and it turns out to be that it only sends the bytes: -112, -113, -115 incorrectly. The client receives them as 63.
    Last edited by abraham2119; 07-24-2009 at 10:13 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Is there a specific reason as to why this is occurring?
    Either a bug in the send code, or a bug in the receive code. Without seeing the code though, we can't give much help.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    I doubt it is a bug of either the receiving or sending code since when the client writes either one of the bytes (-112, -113, -115) the server receives it as 63, and the same happens vice versa (server sends one of those bytes to the client, and client receives it as 63).

    Anyways, here is my server's send code:
    Code:
    int writef(sock *s, char *buff, int tosend)
    {
        int sent = 0;
        while (sent != tosend) {
            int _sent = send((SOCKET) *s, buff+sent, tosend-sent, 0);
            if (_sent >= 0)
                sent += _sent;
            else
                return SOCKET_ERROR;
        }
    
        return 0;
    }
    I invoke the method as follows:
    Code:
    char buff[2];
    buff[0] = -112;
    buff[1] = 5;
    
    writef(&conn, buff, 2);
    Here is my server's receive code:
    Code:
    char buff[512];
    recv(conn, buff, 512, 0);
    I am also inclined into thinking that this bug has to do with the server since when setting up a server written in Java, it all works well. I also do not believe it has anything to do with the byte's signedness since it receives other negative numbers perfectly fine.

    Thanks again,
    abraham2119
    Last edited by abraham2119; 07-24-2009 at 01:26 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I don't see how the code you posted can send a byte with a value of 63.

    and client receives it as 63
    Can you post the Java code that receives 63 when that code you posted above is executed?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    I wrote a sample application that illustrates my problem.

    Run the server and then the client, and the client will print out a question mark character, which has the value of 63.

    Here is the sample server and client: abraham2119 private pastebin - collaborative debugging tool

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by abraham2119 View Post
    I wrote a sample application that illustrates my problem.

    Run the server and then the client, and the client will print out a question mark character, which has the value of 63.

    Here is the sample server and client: abraham2119 private pastebin - collaborative debugging tool
    Well now, that's an interesting statement right there. Is ? being printed because you are receiving a byte of 63, or because you are asking the terminal to print an unprintable character? Can you check whether value==63 after receiving?

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    How do you expect the while loop you have send() defined within to terminate? From what I can tell your sent variable will just keep growing. Unless you are forcing a socket error on your client side to close the connection.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah, change your print statement to:
    Code:
    System.out.println((int)line.charAt(0));
    I'm willing to bet you get 144 which is the 2s compliment of -112.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by slingerland3g View Post
    How do you expect the while loop you have send() defined within to terminate? From what I can tell your sent variable will just keep growing. Unless you are forcing a socket error on your client side to close the connection.
    The while loop terminates when sent == tosend.

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by bithub View Post
    The while loop terminates when sent == tosend.
    True, but that is under the assumption...

    Code:
      sent += _sent   /* Must equal to 2 to stop */
    Are we sure this will happen?

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are we sure this will happen?
    If there is no socket error, then this will happen.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by slingerland3g View Post
    How do you expect the while loop you have send() defined within to terminate? From what I can tell your sent variable will just keep growing. Unless you are forcing a socket error on your client side to close the connection.
    Look closer. The condition in the while loop will become true and the fucntion will return zero. It should work just fine.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by bithub View Post
    If there is no socket error, then this will happen.
    As far as the logic goes with verifying the amount of bytes sent for _sent, the else statement for SOCKET_ERROR will never get reached because of that. My assumption is that this socket is continuing to write data. I would simply check that sent is eventually to equal 2. I would add a printf in there to double check, unless I am not seeing(understanding) something here. I guess that buff is one byte here being sent. And the second time around another byte. Then this may be feasible and thus will terminate as expected.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If one was less trusting of the API, then it might be considered safer for the while loop condition to have been:
    Code:
    while (sent <= tosend)
    But it should work as-is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    the else statement for SOCKET_ERROR will never get reached because of that.
    Yes it will. If send returns -1 (which it does on error), then the SOCKET_ERROR statement will be reached.

    My assumption is that this socket is continuing to write data.
    It continues to write data until the correct amount of bytes has been sent or send() has returned an error.

    I would simply check that sent is eventually to equal 2
    If by "2" you mean tosend, then that's what he is doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a byte down serial pipe
    By blackcell in forum C Programming
    Replies: 3
    Last Post: 05-16-2008, 02:20 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM