Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2006

Tip: Looking for answers? Try searching our database.

how to send 1 byte to C++ server

Thread view: 
msosno01@gmail.com - 24 Jul 2006 23:25 GMT
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 - 25 Jul 2006 03:42 GMT
> 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/

Paul Cager - 26 Jul 2006 23:24 GMT
> 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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.