Reputation: 48
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
Upvotes: 0
Views: 816
Reputation: 310859
I´m connecting to the server via telnet and sending normal text input.
So the other end isn't using ObjectOutputStream
at all, so using ObjectInputStream
is just nonsense.
If you want to just read text, use BufferedReader.readLine()
.
Upvotes: 1