aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Sending an integer as first character of a String through network Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending an integer as first character of a String through network" Watch "Sending an integer as first character of a String through network" New topic
Author

Sending an integer as first character of a String through network

Tamas Gergely
Greenhorn

Joined: Oct 21, 2012
Posts: 2
Hello,

I started to write a client-server application, where the server reads data through a BufferedReader, and client sends data through PrintWriter.
The idea was to make the first character of the String a special one. This should be a control character, and so its value shouldn't be treated as a character, instead a control function.
During some tests I discovered if I set the value of the char 255 or less, then I can send this through the network, however if I set a greater value to it, then some strange value will
be seen on the other side.
An int is 4 bytes (signed), a char is 2 bytes (unsigned), I decided to use the 2 last bytes of the int only.

To illustrate the problem I wrote a test app:
Server:


Client:


The output of the Server is:
tamas@myhost:~/java/server_client_proba$ java TestServer
Character from client: A
Character from client as an int: 65
Character from client (char->int->char): A
Character from client: A
Character from client as an int: 65
Character from client (char->int->char): A
Character from client: B
Character from client as an int: 66
Character from client (char->int->char): B
Character from client: +
Character from client as an int: 43
Character from client (char->int->char): +
Character from client: ~
Character from client as an int: 126
Character from client (char->int->char): ~
Character from client: 
Character from client as an int: 127
Character from client (char->int->char): 
Character from client: ?
Character from client as an int: 63
Character from client (char->int->char): ?
Character from client: €
Character from client as an int: 128
Character from client (char->int->char): €
Character from client: ÿ
Character from client as an int: 255
Character from client (char->int->char): ÿ
Character from client: ?
Character from client as an int: 63
Character from client (char->int->char): ?
Character from client: ?
Character from client as an int: 63
Character from client (char->int->char): ?

When I send char with value 255 or less, then the other side (server) receives it well (first 9 cases in the test aplication),
however if I send a char with greater value (last 2 cases) then the server received as an (int)63 (wich is hexa 3F, wich is the question mark.)

My questions:
1) Why this strange behavior happens?
2) What do you think of the main idea? (Sending 1 special character before normal characters)

Thanks for any help.





Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It looks like the receiver is converting from bytes to chars. (That would be the InputStreamReader, I think.) And when you send a byte which doesn't normally represent text in whatever the default charset is in the receiver's environment, it converts that byte to "?".

So I don't recommend sending binary data and then treating it as text, which is what your current scenario does. Sending an int value as control data is a perfectly normal thing to do when you're sending data over a network, but if you're going to do it, then don't treat everything as text. Use a DataOutputStream to send data and a DataInputStream to receive it. Check out the documentation and you'll see they have methods to send and receive bytes, ints, and so on.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And welcome to the Ranch, Tamas Gergely!
Tamas Gergely
Greenhorn

Joined: Oct 21, 2012
Posts: 2
Thanks for your answer!
On the server side I am using InputStreamReader, so that may be the problem. I will check out the DataStreams as you have suggested.

"And welcome to the Ranch, Tamas Gergely!"
Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending an integer as first character of a String through network