Im trying to send a custom-made object over a socket connection. The class implements serializable but the constructor still throws a NotSerializableException
when im trying to write the object to the socket. I'll post relevent code below:
public class serilizableArrayHolder implements Serializable {
private ArrayList<Client> array= null;
public serilizableArrayHolder(ArrayList<Client> array) {
this.array=array;
}
public ArrayList<Client> getArray() {
return array;
}
}
This is my custom-made class. For now im trying to send an arraylist from the server to the client but i'll add additional info in a later stage. The send method is posted below in my server class is posted below:
public void sendData(Socket clientSocket){
ObjectOutputStream out;
try {
serilizableArrayHolder temp = new serilizableArrayHolder(clientCollection);
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(temp); <---This line generates the error.
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
This is my send-method from the server. clientCollection
is the arrayList that im trying to send.
The whole Client class:
public class Client implements Runnable, Serializable{
public Thread thread = new Thread(this);
private Socket s;
private DataInputStream in;
private DataOutputStream out;
private ObjectOutputStream objOut;
private ServerMain server=null;
private String host=null;
private Client c;
private String userName;
public Client(Socket s, String host, ServerMain server) throws IOException{
c=this;
this.host=host;
this.s=s;
this.server=server;
this.userName=userName;
in= new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
objOut=new ObjectOutputStream(s.getOutputStream());
thread.start();
}
public String getClientInfo(){
return host;
}
public String getUserName(){
return userName;
}
public void send(String s){
try {
out.writeUTF(s);
}
catch (IOException e){
}
}
public void run() {
while(true){
try {
String temp = in.readUTF();
if(temp.equals("sendOnline")){
sendOnline();
}
String tempHost=s.getInetAddress().getHostAddress();
server.appendString(tempHost+" Skickade: "+temp+"\n");
}
catch (IOException e) {
String str = s.getInetAddress().getHostName();
server.clientDisconnect(str);
break;
}
}
try {
s.close();
}
catch (IOException e) {
}
}
public void sendOnline(){
serilizableArrayHolder temp = new serilizableArrayHolder(server.getClients());
try {
objOut.writeObject(temp);
objOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Metoden anropas");
}
}
The new stacktrace:
java.io.NotSerializableException: java.io.DataInputStream
Metoden anropas
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at java.util.ArrayList.writeObject(ArrayList.java:710)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at Gesäll.Client.sendOnline(Client.java:83)
at Gesäll.Client.run(Client.java:58)
at java.lang.Thread.run(Thread.java:722)