2

I debugged the program and observed that it is stopped when it wants to get input stream from socket:

public Chat(Socket s) throws IOException {
        input = new ObjectInputStream(s.getInputStream()); // stopped here 
        output = new ObjectOutputStream(s.getOutputStream());
        initComponents();
    }

I have closed the open streams and the socket before calling above constructor here:

Socket socket = listeningSocket.accept();
disconnect();
Chat c = new Chat(socket);

and here is the disconnect method:

private void disconnect() throws IOException {
        input.close();
        output.close();
        client.close();
    }

input, output and client are initiated here:

    client = new Socket(chatServer, chatPort);
    input = new ObjectInputStream(client.getInputStream());
    output = new ObjectOutputStream(client.getOutputStream());

this is the stack trace when the program is suspended:

Thread [main] (Suspended)   
    SocketInputStream.socketRead0(FileDescriptor, byte[], int, int, int) line: not available [native method]    
    SocketInputStream.read(byte[], int, int) line: 146  
    ObjectInputStream$PeekInputStream.read(byte[], int, int) line: 2282 
    ObjectInputStream$PeekInputStream.readFully(byte[], int, int) line: 2295    
    ObjectInputStream$BlockDataInputStream.readShort() line: 2766   
    ObjectInputStream.readStreamHeader() line: 797  
    ObjectInputStream.<init>(InputStream) line: 297 
    Chat.<init>(Socket) line: 20    
    Client$5.run() line: 310    
    Client.clientListen() line: 320 
    Client.access$7(Client) line: 302   
    Client$6.run() line: 350    
    Client.main(String[]) line: 352 

please help thanks :)

9
  • 4
    What is your stackTrace? Commented Aug 5, 2011 at 19:44
  • there's no exception. Just nothing is happened after that. It seems that something is wrong with closing the streams or socket. and so the program is blocked there. Commented Aug 5, 2011 at 19:50
  • Did you try to debug your program in order to determine exactly what line of your code is causing the freeze? Commented Aug 5, 2011 at 19:54
  • Yes. I debugged it and observed that. Commented Aug 5, 2011 at 19:57
  • 1
    @atreys thanks for your suggestion, i added the stack trace. Commented Aug 5, 2011 at 20:17

2 Answers 2

2

From ObjectInputStream's constructor's documentation:

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Is anything being written to the socket from the other end? Are you flushing the stream from the other end?

The ObjectOutputStream's constructor has documentation saying that users may wish to flush to the stream so that inputstreams don't block.

Sign up to request clarification or add additional context in comments.

4 Comments

Your were faster - in addition: I think the server and client code runs in the same thread. Hence the thread stops when the client tries to read bytes from the stream. But it can't since the server code is never reached.
@Roland, even if the server code ran in a separate thread, if the server doesn't flush to the output stream, the chat client won't be able to get past the constructor.
Yes, you are right. I didn't say anything against it, did I? ;)
I'm really thankful of both of you guys ... flush worked!! :)
0

Is it stopping on that line specifically or possibly inside the constructor, or even inside the s.getInputStream() call? Both the constructor and getInputStream() can throw IOException.

Not to belabor the point, but did you jump into the constructor call and the getInputStream() method when you debugged it?

Like Atreys said above it would probably be useful to see the stack trace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.