Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java client and C server socket

 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can anyone please help me on this query?
I have a server written in C and client in Java. I need to establish a tcp/ip connection between them.
Right now I am able to set a socket connection from my Java client program and created an input stream which is continuously reading data from the server.
I need to read based on the server's protocol. The server has a message header and few other information as a C program. Basically all information are being sent as packets.
I know the number of bytes for each data structure.
Now how can I read that from my java client in the same order? Do I need to wrap the io stream object to accept bytes?
All the data in the server is like raw data. Can anyone guide me how to receive data from server?

Thanks
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an InputStream, then you can use that to read single or multiple bytes, can't you?
 
Akil Kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I will be able to read multiple bytes. But how can I format my data while reading? Currently when I run my program it connects to the server and keep reading continuously.

while(true){

System.out.println(in.read());

}

But I need to have the data in the console in an ordered / formatted manner. Do I need to loop through the data and split it based on how the data is laid out in server protocol?

Thanks
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read the bytes into a stack and extract the information from that. Or just write directly into the datastructure that you expected.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic