For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > 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, 6:59 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.

Barry Margolin

2006-07-24, 6:59 pm

In article <1153780023.057251.219430@i3g2000cwc.googlegroups.com>,
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.


How are you printing the byte? The byte value you're sending is 4,
which is a control character, not a graphic character, so you won't see
anything if you do

cout << byte;

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
csogono@gmail.com

2006-07-25, 4:00 am

Don't print the values. Compare them via a simple == condition. I'm not
really sure what the Java type 'byte' reall is (sorry I'm a C/C++
programmer), but I assume that 4 is assigned to byte as 0x04?? If so
then C++ won't be able to print that as a character 4. You should read
up on ASCII.

Carlo

Nils O. SelÄsdal

2006-07-26, 4:01 am

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.

HOW do you try to print it ?
Try
printf("%d\n",byte);
csogono@gmail.com

2006-07-26, 4:01 am

Nils O. Sel=E5sdal wrote:
> HOW do you try to print it ?
> Try
> printf("%d\n",byte);


printf("%x\n", byte);

that would show you the raw byte in hex.

Carlo

Sponsored Links







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

Copyright 2008 codecomments.com