Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuously sending data through Java socket

Tags:

java

sockets

What is the best way to send a series of data (float array) from a server to a client continuously through Java socket? For example, I have a server that produces float data every 1ms, and I would like to send this data to a client. My plan is to put this process in a thread (server side) and send it to the client continuously. Is it possible to achieve this through Java socket? or should I write the data into a file first before sending it to the client?

Thanks!

like image 326
Faiz Avatar asked Nov 24 '25 09:11

Faiz


2 Answers

There should be no problem doing this via socket. Basically you set up a thread on the server side to send data whenever a new set is provided.

Then on the client side, set up a thread who constantly listens on the socket, reads in and unpacks the data whenever available, post it somewhere for the processing code of your client to use it and then go back to a poll/sleep cycle until the server sends more data.

Just make sure on the client side you provide a method kill the listener thread if the socket closes (Server shuts down, network hickup, etc).

like image 68
Mike Clark Avatar answered Nov 26 '25 22:11

Mike Clark


Using simple socket programming, this is not a problem at all:

  • Open a TCP connection between server and client
  • Send the data using the connections InputStream and OutputStream
  • Close the connection in case of any error/disconnection

Note that this solution is not very scalable, and if this needs to be scaled you will need to use non-blocking IO (so that your server does not choke on all the open connections and the running threads)

like image 40
Yuval Adam Avatar answered Nov 27 '25 00:11

Yuval Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!