Results 1 to 7 of 7
  1. #1
    Dodo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default Problem with sockets reading in defined thread.

    Hello,

    During my programming of the client application a problem has occurred concerning the reading of sockets in a thread that I used here for better efficiency. When I start up the client application, writer a message and send it to the server application that should send this message back to my client, then an exception is thrown with a pointer to the following part of my source code:

    Java Code:
    while ((response = socketReader.readLine()) != null) // <- an exception is thrown here 
    	System.out.println(response);                // and subsequent running is stopped!

    I don't know, where is the problem, is anything wrong there? The server application works properly, it was tested before my usage.

    There is shown the whole source code below for better review :

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.io.IOException;
    
    class Net2Output extends Thread {
    	
    	BufferedReader socketReader;
    	
    	public Net2Output(BufferedReader socketReader) {
    		this.socketReader = socketReader;
    	}
    	
    	public void run() {
    		try {
    			String response;
    			while ((response = socketReader.readLine()) != null)
    				System.out.println(response);
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    public class Client {
    	public static void main(String[] args) {
    		
    		int port = 81;
    		String inputLine = new String("");
    		String ip_addr = new String("127.0.0.1");
    		InputStreamReader inputString = new InputStreamReader(System.in);
    		BufferedReader readerString = new BufferedReader(inputString);
    		
    		if (args.length > 0) {
    			if (args[0].startsWith("-help")) {
    				System.out.printf("Usage: Client [IP_ADDR] [PORT]\n" +
    						"If IP_ADDR is not specified, default address %s is used\n"+
    						"If PORT is not specified, default port %d is used\n", ip_addr, port);
    				return;
    			}
    			
    			ip_addr = args[0];
    			
    			try {
    				if (args.length > 1)
    					port = Integer.decode(args[1]);
    			} catch (NumberFormatException e) {
    				System.err.printf("Argument %s is not integer, using default port %d\n", args[0], port);
    			}
    		}
    		
    		System.err.printf("Connecting to server %s, port %d...\n", ip_addr, port);
    		
    		Net2Output net2out=null;
    		
    		try {
    			Socket socket = new Socket(ip_addr, port);
    			socket.setKeepAlive(true);
    			OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
    			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
    			
    			net2out = new Net2Output(reader);
    			net2out.start();
    			
    			try
    			{
    				System.out.print("\nType a message for different client.\n ");
    				inputLine = readerString.readLine();
    			}
    			catch(Exception e){
    				e.printStackTrace();
    			}
    			
    			writer.write(inputLine);
    			writer.flush();
    			
    			writer.close();
    			reader.close();
    			socket.close();
    			readerString.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

    Thanks for your possible help.

    Dodo
    Last edited by Dodo; 11-17-2009 at 10:58 PM.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    What exception is the stacktrace giving?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Dodo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default Problem with sockets reading in defined thread.

    Hello,

    There is shown the following stacktrace:

    Java 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)

    Dodo

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    It looks like the socket is closed before you finish reading a line.
    My Hobby Project: LegacyClone

  5. #5
    Dodo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default Problem with sockets reading in defined thread.

    Hello,

    What can I change or add in the Net2Output class to avoid the socket closing?

    Java Code:
    class Net2Output extends Thread {
    	
    	BufferedReader socketReader;
    	
    	public Net2Output(BufferedReader socketReader) {
    		this.socketReader = socketReader;
    	}
    	
    	public void run() {
    		try {
    			String response;
    			while ((response = socketReader.readLine()) != null)
    				System.out.println(response);
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

    Dodo
    Last edited by Dodo; 11-18-2009 at 09:43 AM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    You have two Threads running: your main thread and your Net2Output thread. That second thread is happily reading while that first thread closes the socket. Don't do that.

    kind regards,

    Jos

  7. #7
    Dodo is offline Member
    Join Date
    Nov 2009
    Posts
    10
    Rep Power
    0

    Default Problem with sockets reading in defined thread.

    Hello,

    How can I change my source code or what to add there to run the program correctly? I suppose, I should modify the Net2Output class.

    Please, advise me.

    Michal

Similar Threads

  1. Replies: 11
    Last Post: 11-03-2009, 05:32 PM
  2. TCP sockets problem
    By duffman.sk in forum Networking
    Replies: 1
    Last Post: 10-30-2009, 05:06 AM
  3. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  4. Problem (sockets)
    By Blacknight962 in forum New To Java
    Replies: 3
    Last Post: 08-16-2009, 12:56 AM
  5. Sockets and JFrames problem
    By marcg11 in forum Advanced Java
    Replies: 6
    Last Post: 08-15-2009, 01:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •