Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket Closed Error

Status
Not open for further replies.

mackey333

Technical User
May 10, 2001
563
0
0
US
Assuming that socket is Socket which may or may not be closed or connected and inputStream = socket.getInputStream().

Code:
if (socket.isClosed() || !socket.isConnected())
{
	return null;
}
			
try
{
	i = inputStream.read();
}
catch (Exception e)
{
	System.out.println(e.printStackTrace());
				
	return null;
}

How is it possible for inputStream.read() to throw this error?

Code:
java.net.SocketException: Socket closed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.net.SocketInputStream.read(SocketInputStream.java:182)

Shouldn't the if statement catch this?

-Greg :-Q

flaga.gif
 
inputStream = socket.getInputStream() may throw an error, if the socket is closed.

So you should check for (socket.isClosed() || !socket.isConnectd()) before doing the inputStream = socket.getInputStream()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top