Hey I'm trying to debug this program from last 2-3 days . Here is the problem ,I'm building a client server architecture and all clients ping server(with their information) after certain interval of time using Socket connection.So at server side when i try to construct a ObjectOutputStream the program get stuck. Here is code of client.
public void pingUpdate(){
Thread pingThread = new Thread() {
public void run() {
while(true) {
try {
ping_socket = new Socket( "localhost", 11111 );
ObjectOutputStream ping_objectOutputStream = new ObjectOutputStream( ping_socket.getOutputStream( ) );
ping_objectOutputStream.flush();
ping_objectOutputStream.writeObject( user );
ping_objectOutputStream.close();
ping_socket.close( );
}catch (Exception exception) {
exception.printStackTrace();
}
}
};
pingThread.start();
}
And here is the code of server
public void run() {
while ( true ) {
try {
System.out.println("Server Listening" );
Socket client = null;
client = serverSock.accept();
System.out.println("Accepted" );
InputStream inputStream = client.getInputStream();
System.out.println("Input stream established" );
ObjectInputStream ois = new ObjectInputStream( inputStream );
System.out.println("Object streams established" );
User user = ( User ) ois.readObject( );
System.out.println("Object read" );
ois.close( );
client.close( );
}
catch (Exception e){
e.printStackTrace();
}
}
}
The server program prints till "Input streams established" and get stuck. I don't know why this happens although i flushed the output stream at client side.Thanks.
pingThreadcreates a newObjectOutputStreamon every loop. You might want to go through some networking tutorials too, otherwise you'll be debugging for weeks.