[java] Java and Sockets

Started by
18 comments, last by jolyqr 17 years, 7 months ago
Basically I would like to create applications that comunicate by using sockets. I would like to know how I to send an object instead of a text message as those shown in sun tutorials by using sockets ? Cheers !!
Advertisement
If you're using a PrintWriter to send the data it has a print method which allows objects as a parameter.
actually, i saw this method, but i didn't tested it because i don't know how the reciever would decode it...
DISCLAIMER: I haven't done this in a long time so I could be off a bit

if you're using a BufferedReader you could use the read() method of it and make it like
in.read((Char [])obj,NULL,sizeof(obj));
here is the way i read the recieved message :

while ((inputLine = in.readLine()) != null)
{
recieved.setText(inputLine);
if (inputLine.equals("Bye."))
break;
}

here is the way I was used to sending the text message.

out.println(sendtext.getText());
Yet another disclamier from me, but shouldnt you be able to wrap the input/output streams of the socket in ObjectInputStream and ObjectOutputStreams ?

Something like
Socket socket = getSocket();ObjectInputStream objectsIn = new ObjectInputStream( socket.getInputStream() );ObjectOutputStream objectsOut = new ObjectOutputStream( socket.getOutputStream() );objectsOut.writeObject( someObject ); // should appear at other end...?


The Objects you send might have to be serializable or something, my java is a bit rusty...
I'm quite new in Java. What do you mean by "serializable "? Indeed I have met this word lots of time, but I have never got its meaning...
Quote:Original post by jolyqr
I'm quite new in Java. What do you mean by "serializable "? Indeed I have met this word lots of time, but I have never got its meaning...


If the class implements the interface Serializable then it can be written using ObjectInputStreams to various input streams ( network sockets, files, etc ) and can be retrieved via ObjectOutputStreams. Thats what I remember anyway...
thanks for your help

This topic is closed to new replies.

Advertisement