I have a Java client and a C++ server. How to send 1 byte to the
server?
This what I am doing in my Java client:
byte b = 4;
Socket s = new Socket(server, port);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
System.out.print("Connection established on port " + port);
out.writeByte(b);
This is how I am accepting this byte in C++ server:
char InputStream::read()//reads one byte at a time
{
char byte;
int size = 0;
while (size != 1) {
size = soc->recv(&byte,1);
}
return byte;
}
The server does read 1 byte, Howerver, when I try to print the byte
sent from the server, all I get is empty space.
I am assuming that the problem is in how I am sending this byte. Please
help.
> I have a Java client and a C++ server. How to send 1 byte to the
> server?
[quoted text clipped - 22 lines]
> I am assuming that the problem is in how I am sending this byte. Please
> help.
Lose the DataOutputStream and just write the byte to the plain OutputStream.

Signature
Knute Johnson
email s/nospam/knute/
> I have a Java client and a C++ server. How to send 1 byte to the
> server?
[quoted text clipped - 22 lines]
> I am assuming that the problem is in how I am sending this byte. Please
> help.
Java chars are _not_ the same as bytes - they are two bytes long. If you
are using ASCII characters, then the first byte of a character is
probably 0x00.
Guido Zijlstra - 27 Jul 2006 08:53 GMT
How are you printing the byte received. Ascii char 4 is not a printable
character as far as i know, it probably will give you an empty space.
> > This what I am doing in my Java client:
> >
> > byte b = 4;
> > Socket s = new Socket(server, port);
> The server does read 1 byte, Howerver, when I try to print the byte
> > sent from the server, all I get is empty space.
Paul Cager - 27 Jul 2006 20:42 GMT
>> I have a Java client and a C++ server. How to send 1 byte to the
>> server?
[quoted text clipped - 26 lines]
> are using ASCII characters, then the first byte of a character is
> probably 0x00.
Ignore what I have written above - you are, of course, writing a byte.
My brain doesn't seem to be working today. Sorry about that!