Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Serialize a Socket Object

  Asked By: Richard    Date: Jun 08    Category: Java    Views: 2254
  

I have a short question.

How to serialize a socket object?

Looking for the answer as early as possible.

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Jenny Lopez     Answered On: Jun 08

Socket echoSocket = new
Socket("my_machine_name",3000);
FileOutputStream fout = new
FileOutputStream("name.out");
// ObjectInputStream ois = new
ObjectInputStream(fout);

ObjectOutputStream dat = new
ObjectOutputStream(fis);
dat.writeObject(echoSocket);

The socket  will be serialized and written into a file
named name.out.

 
Answer #2    Answered By: Aiko Suzuki     Answered On: Jun 08

But i think there is an error in the code.

where is 'fis' created?

 
Answer #3    Answered By: Ellen Simpson     Answered On: Jun 08

Replace
ObjectOutputStream dat = new ObjectOutputStream(fis);

with
ObjectOutputStream dat = new ObjectOutputStream(fos);

 
Answer #4    Answered By: Patricia Johnson     Answered On: Jun 08

Have you compiled and run the code? Or have thought and wrote it here
only. Cause I think it still contains errors. Besides of doing all
this stuff, there will be again the well known
Exception "NotSerializableException" will welcome you.

 
Answer #5    Answered By: Calandre Bernard     Answered On: Jun 08

-----------------------MySocket.java-----------------
import java.net.*;
import java.io.*;
public class MySocket extends Socket implements
Serializable{
MySocket(String host, int port) throws Exception{
super(host,port);
}
}

-----------------TestSocket.java-----------------

import java.io.*;
import java.net.*;
public class TestSocket{
public static void main(String[] args){
try{
MySocket echoSocket = new MySocket("localhost",80);
FileOutputStream fout = new
FileOutputStream("name.out");
ObjectOutputStream dat = new
ObjectOutputStream(fout);
dat.writeObject(echoSocket);
}catch(Exception lvObjEx){
lvObjEx.printStackTrace();
}
}
}

 
Didn't find what you were looking for? Find more on Serialize a Socket Object Or get search suggestion and latest updates.




Tagged: