For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2006 > how to send 1 byte to C++ server









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to send 1 byte to C++ server
msosno01@gmail.com

2006-07-24, 7:01 pm

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.

Knute Johnson

2006-07-24, 10:00 pm

msosno01@gmail.com wrote:
> 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.
>


Lose the DataOutputStream and just write the byte to the plain OutputStream.

--

Knute Johnson
email s/nospam/knute/
Paul Cager

2006-07-26, 7:03 pm

msosno01@gmail.com wrote:
> 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.
>


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

2006-07-27, 4:02 am

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.
[color=darkred]
[color=darkred]
> The server does read 1 byte, Howerver, when I try to print the byte


Paul Cager

2006-07-27, 7:01 pm

Paul Cager wrote:
> msosno01@gmail.com wrote:
>
> 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.


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!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com