Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-11-2009
Member
 
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
Reply With Quote
  #2  
Old 19-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
  #3  
Old 19-11-2009
Member
 
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.
Reply With Quote
  #4  
Old 19-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Socket reading in defined thread"
Thread Thread Starter Forum Replies Last Post
Does LGA1155 socket core i5 processor support MSI motherboard LGA 1366 socket Firoja Motherboard Processor & RAM 6 22-07-2011 10:24 PM
Synchronize a producer thread and a consumer thread in C# Ayuka Software Development 6 10-11-2010 04:04 AM
What is socket and server socket in java? Truster Software Development 5 01-02-2010 02:08 PM
Winsock Socket error 10048 socket bind Theophilus Operating Systems 4 16-02-2009 08:07 PM


All times are GMT +5.5. The time now is 05:02 PM.