Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send String array Object over Socket?

Tags:

java

sockets

I have String array object. Lets say

String[] names = new String[7];

And I am also making this object persistent by storing it into file using ObjectOutputStream on my client system. I am reading the stored object using ObjectInputStream. Upto this Okay. Now I want to send this object to another system over socket.

How to do it? Please help. Thanks.

like image 346
Winn Avatar asked Jan 11 '23 23:01

Winn


1 Answers

You should create instance of Socket
get output stream and write to it (e.g. with ObjectOutputStream).

Socket echoSocket = new Socket(hostName, portNumber);
ObjectOutputStream out = new ObjectOutputStream(echoSocket.getOutputStream());
out.writeObject(names);  

You can find an example in Oracle docs: example.
Also this answer should be helpful for you

like image 120
Ilya Avatar answered Jan 22 '23 15:01

Ilya