Results 1 to 4 of 4

Thread: Socket reading in defined thread

  1. #1
    Join Date
    Nov 2009
    Posts
    333

    Socket reading in defined thread

    Hi
    I am creating a Client application and have come through a problem in the code. I think the problem is concerned to reading the socket in a thread, which I used for better efficiency. When i start the client application a message is sent to the server application and the server application should send a message in reply to the client. I have thrown an exception with a pointer, the code is below
    Code:
    while ((response = socketReader.readLine()) != null) // <- an exception is thrown here 
    	System.out.println(response);                // and subsequent running is stopped!
    I do not know were I am wrong. The server application works fine, I think the problem is in client application

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Socket reading in defined thread

    Hello
    You have not posted the code, it would be better to identify the problem if the code is present. Ok, forget it what exception is the stack trace is giving. Please paste the exception in the next post so that we can tell you what is the problem. Any more problem do post again.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Socket reading in defined thread

    Hi
    Sorry for not posting the code. The code is too big so I am posting the error reported by the stack trace.
    Code:
    OutputHandler, wakeup and die!
    java.net.SocketException: socket closed
    	at java.net.SocketInputStream.socketRead0(Native Method)
    	at java.net.SocketInputStream.read(Unknown Source)
    	at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    	at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    	at sun.nio.cs.StreamDecoder.read(Unknown Source)
    	at java.io.InputStreamReader.read(Unknown Source)
    	at java.io.BufferedReader.fill(Unknown Source)
    	at java.io.BufferedReader.readLine(Unknown Source)
    	at java.io.BufferedReader.readLine(Unknown Source)
    	at vyuka.Net2Output.run(Client.java:16)
    Any ides or solution for this. Do post back. Thanks in advance.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Socket reading in defined thread

    Hello
    I think the socket is getting closed before it has finished reading the line. I am posting you a sample program. just go through it and see if it helps you.
    Code:
    class net2op extends Thread {
    	BufferedReader sr;
    	
    	public net2op(BufferedReader sr) {
    		this.sr = sr;
    	}
    
    	public void run() {
    		try {
    			String response;
    			while ((response = sr.readLine()) != null)
    				System.out.println(response);
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    I any problem do post back. Best of luck.

Similar Threads

  1. Replies: 6
    Last Post: 22-07-2011, 10:24 PM
  2. Synchronize a producer thread and a consumer thread in C#
    By Ayuka in forum Software Development
    Replies: 6
    Last Post: 10-11-2010, 04:04 AM
  3. What is socket and server socket in java?
    By Truster in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 02:08 PM
  4. Winsock Socket error 10048 socket bind
    By Theophilus in forum Operating Systems
    Replies: 4
    Last Post: 16-02-2009, 08:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,821,544.14182 seconds with 16 queries