Thread: sending byte <= -112

  1. #16
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    In analysis

    First pass,

    send will return 1

    sent = 1

    Second pass

    send will return 2

    sent = 3


    I guess what is the array buff[] being sent in the first pass, if at all, if it is two bytes we are good to go with this and I just made this more complicated than it should have been.
    Last edited by slingerland3g; 07-24-2009 at 03:24 PM.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by slingerland3g View Post
    In analysis

    First pass,

    send will return 1

    sent = 1

    Second pass

    send will return 2

    sent = 3
    No, that cannot happen.
    If send() returns 1 on the first pass, then send() cannot return 2 the second pass. send() returns the number of bytes sent. On the second pass, the third parameter to send() will be 1, so the maximum number send() can return is 1 -- not 2.

  3. #18
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Ok, I was working from the logic that buff being passed was 1 byte per pass in the while loop. Send simply returned the number of bytes sent, so was working of that thought process. Geesh, I was not working in the logic for this for tosend-sent!
    Last edited by slingerland3g; 07-24-2009 at 03:29 PM.

  4. #19
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Hello everyone.

    I'm a bit rusty with java streams, but according to the doc, InputStreamReader is a bridge from byte streams to character streams. Since the sender is dealing with bytes, the receiver should do the same. This java receiver behaves as expected:

    Code:
    import java.net.Socket;
    import java.io.*;
    
    public class x {
    
        public static void main(String[] args) {
            try {
                Socket s = new Socket("127.0.0.1", 55555);
      
    	    InputStream isr = s.getInputStream();
    	    int r;
    	    while((r = isr.read()) != -1) {
    		if (r >= 128) {
    		    r |= 0xFFFFFF00;
    		}
    		System.out.println("read: "+ r);
    	    }
    
    
    
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    The input stream returns a value between 0 and 255 with -1 indicating end of stream. Anything over 128 needs to have the sign extended.

    Is there a better way? I don't know.


    On the sender:
    Code:
        SOCKET conn = accept(s, &saddr, &size);
        char wbuff[30];
    	int ix = 0;
    	wbuff[ix++] =  1;
    	wbuff[ix++] =  0;
    	wbuff[ix++] =  -1;
    	wbuff[ix++] =  -2;
    	wbuff[ix++] =  -3;
    	wbuff[ix++] = -111;
        wbuff[ix++] = -112;
        wbuff[ix++] = '\r';
        wbuff[ix++] = '\n';
        send(conn, wbuff, ix, 0);
    The receiver prints
    read: 1
    read: 0
    read: -1
    read: -2
    read: -3
    read: -111
    read: -112
    read: 13
    read: 10

  5. #20
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are correct Zlatko. Java uses 16 bit characters, and it expects it's strings to be serialized to UTF-8. This means that if you are sending bytes, you should receive bytes as Zlatko has done in his code example.

  6. #21
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by bithub View Post
    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.
    Actually, I get 65533. which makes sense as well. (I was thinking 144 too.) This:
    Run the server and then the client, and the client will print out a question mark character, which has the value of 63.
    Also never happened. My terminal did print a question mark, but it didn't have a value of 63. It was the unicode replacement character, U+FFFD ... or 65533. (And it looks completely different: ? and � .)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #22
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    @Cactus_Hugger: On my windows XP OS, it printed a question mark.

    Anyways, as to the thread, it is as Zlatko described it. The internal string conversions were causing the problem. Turns out to be, I was wrong after all; the problem lied within the client. :P

    Thanks again!

  8. #23
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by abraham2119 View Post
    @Cactus_Hugger: On my windows XP OS, it printed a question mark.

    Anyways, as to the thread, it is as Zlatko described it. The internal string conversions were causing the problem. Turns out to be, I was wrong after all; the problem lied within the client. :P
    Ah. Good job, XP.

    As for your second point -- I'm not convinced that the problem is the client, per se. Let's look at a larger picture: You're sending a negative number on one side, but you're treating it as a character on the other side. No character set that I have ever heard of has negative values assigned to characters, so what exactly are you trying to achieve?

    I also couldn't find anything in the Java docs as to what character set BufferedInput uses while reading the file.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #24
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    The problem was client-sided, actually. I just finished fixing it. I did not intend to treat the received byte(s) as characters, I simply wanted to read from the stream line-per-line. I did not know that this automatically converted the read bytes into characters.

    Anyways, to fix the problem, I made my own line-per-line reading method and treated each byte as a byte, and not as a character.

  10. #25
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I also couldn't find anything in the Java docs as to what character set BufferedInput uses while reading the file.
    You can specify the character set to expect in the InputStreamReader class. If this is not specified, it will use the platform's default character set (which is probably UTF-8).

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