Java serialization over network

E

elbaid

Just want to know if there's a tutorial or an how-to for serializing
object, put it in a stream over network, and deserialize it on the
other point. I understand the principles of serialization, I/O,
streams, sockets and so on, just want an example (client sending object
to a server) to start with.

Thank you.
 
A

Arne Vajhøj

elbaid said:
Just want to know if there's a tutorial or an how-to for serializing
object, put it in a stream over network, and deserialize it on the other
point. I understand the principles of serialization, I/O, streams,
sockets and so on, just want an example (client sending object to a
server) to start with.

Just wrap the InputStream/OutputStream from the Socket in
a ObjectInputStream/ObjectOutputStrem and it should be very easy.

Code snippets:

Socket s = new Socket("localhost", 12345);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(someobject);
oos.close();
s.close();

ServerSocket ss = new ServerSocket(12345);
Socket s = ss.accept();
ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
SomeClass someobject = (SomeClass)ios.readObject();
ios.close();
s.close();
ss.close();

Arne
 
E

elbaid

Thank you very much!


Just wrap the InputStream/OutputStream from the Socket in
a ObjectInputStream/ObjectOutputStrem and it should be very easy.

Code snippets:

Socket s = new Socket("localhost", 12345);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(someobject);
oos.close();
s.close();

ServerSocket ss = new ServerSocket(12345);
Socket s = ss.accept();
ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
SomeClass someobject = (SomeClass)ios.readObject();
ios.close();
s.close();
ss.close();

Arne
 
R

Roedy Green

Just want to know if there's a tutorial or an how-to for serializing
object, put it in a stream over network, and deserialize it on the
other point. I understand the principles of serialization, I/O,
streams, sockets and so on, just want an example (client sending object
to a server) to start with.

You normally would use RMI for that.

There is nothing to it conceptually. Both ends must have the code for
all the classes used. It is so different that saving to disk and
restoring.

You probably want to GZIP the stream.

See http://mindprod.com/applet/fileio.html for the code.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Danish studies of 10,000 birds killed revealed that almost all died in
collisions with buildings, cars and wires; only 10 were killed by windmills.
Alternative energy sources are absolutely necessary.
Global warming will kill birds and bats, as well as other species,
in much greater numbers than wind power."
~ Dr. David Suzuki
 
R

Roedy Green

You normally would use RMI for that.

see http://mindprod.com/jgloss/rmi.html
http://mindprod.com/jgloss/serialization.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Danish studies of 10,000 birds killed revealed that almost all died in
collisions with buildings, cars and wires; only 10 were killed by windmills.
Alternative energy sources are absolutely necessary.
Global warming will kill birds and bats, as well as other species,
in much greater numbers than wind power."
~ Dr. David Suzuki
 
T

Tom Anderson

You normally would use RMI for that.

There is nothing to it conceptually. Both ends must have the code for
all the classes used. It is so different that saving to disk and
restoring.

You probably want to GZIP the stream.

Good idea. And make sure it's buffered - i think GZIPOutputStream does
some buffering internally, but ObjectOutputStream doesn't (i think!), so
if you're using the latter, add a BufferedOutputStream to the stack.

tom
 
N

Nilshan

Just want to know if there's a tutorial or an how-to for serializing
object, put it in a stream over network, and deserialize it on the
other point. I understand the principles of serialization, I/O,
streams, sockets and so on, just want an example (client sending object
to a server) to start with.

Thank you.

-----------------------------------------------------------------------------

1. Example class to be serialized.

public class RuleInfo implements Serializable
{

private String data="";

public RuleInfo() {
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

}

2. User servlet to serialize...

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

try {
RuleInfo info = new RuleInfo();
info.setData("1");

RuleInfo info1 = new RuleInfo();
info1.setData("2");

List<RuleInfo> list = new ArrayList<RuleInfo>();
list.add(info);
list.add(info1);

ObjectOutputStream out1 = new ObjectOutputStream
(response.getOutputStream());
out1.writeObject(list);
out1.flush();
out1.close();

System.out.println("This is done");

} finally {
}
}

3. Get Serialized Object at another place using ..

public void test() throws ClassNotFoundException
{

String urlString = "http://localhost:8084/WebApplication1/
BinaryPkgServlet";

try
{
InputStream stream = null;
URL url = new URL(urlString);

HttpURLConnection httpURLConnection = (HttpURLConnection)
url.openConnection();

/**
* If Response Code is 200 [OK] then get InputStream of
* Compiled binary Package remotely and store in local
file
* system.
*/

int httpResponseCode = httpURLConnection.getResponseCode
();


if (httpResponseCode == 200)
{
stream = httpURLConnection.getInputStream();

ObjectInputStream st = new ObjectInputStream(stream);
List<RuleInfo> list = (List<RuleInfo>)st.readObject();

System.out.println("List "+list +"and size "+list.size
());

for(RuleInfo info : list){
System.out.println("Info "+info.getData());
}
}

}
catch (IOException e)
{
}

}


Thanks,
Nilshan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top