• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java client and C server socket

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
That feels good. Thanks. Here's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic