Hi everbody i have to write a server which communicate over a socket connection.
The client sends Objects to the server and the server prints it to console.
public class ConnectionListener {
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
Object message;
void runListener()
{
try{
providerSocket = new ServerSocket(2004, 10);
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
in = new ObjectInputStream(connection.getInputStream());
out = new ObjectOutputStream(connection.getOutputStream());
do{
message = in.readObject();
System.out.println("client>" + message);
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
}
But i always become a StreamCorruptedException at this line:
in = new ObjectInputStream(connection.getInputStream());
Can Anyone help me?
Thank you
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…