dimanche 8 mars 2015

How to write object to a socket in Java



I have a very simple class Message. I want to write its object to the socket in java.



class Message implements Serializable{
String id;
String data;
Type dataType;
double priority;
Message(String data, Type dataType,int port,double priority ){//creates message with certain priority
this.data = data;
this.dataType = dataType;
this.priority = priority+getPortValue(port);
this.id = data+dataType+this.priority;
}

}


The code to write object to stream.



Message m;
m = new Message(msgToSend,Type.message_no_priority,myPort,count);
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),Integer.parseInt(REMOTE_PORT_ARR[i]));

ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(m);


This does not work and gives me socket IOException.


I have seen the following questions


Link 1 Link 2 Link 3




Aucun commentaire:

Enregistrer un commentaire