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!
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).
Using simple socket programming, this is not a problem at all:
InputStream and OutputStreamNote 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With