G
Guest
Hi !
I would like to know, how to trap an event, that the remote peer has
closed the connection.
Here is a simple script which sends a line, and receives one.
Socket socket ;
OutputStream outputStream = null ;
BufferedReader inputStream = null;
try {
socket = new Socket("mypeer", 5555) ;
socket.setKeepAlive(true) ;
socket.setSoTimeout(5000) ;
outputStream = new PrintStream(socket.getOutputStream());
inputStream = new BufferedReader(new InputStreamReader
(socket.getInputStream()));
String s = new String("This is to be sent\n") ;
outputStream.write(s.getBytes()) ;
outputStream.flush() ;
String r ;
if ((r = inputStream.readLine()) == null) {
System.out.println("NOTHING READ") ;
} else {
System.out.println("This was read: " + r) ;
}
outputStream.flush() ;
outputStream.close() ;
inputStream.close() ;
socket.close() ;
} catch (Exception e) {
e.printStackTrace() ;
}
Is the NULL from readLine() 100% sign that socket was closed by remote
peer, or may be just lack of data remote did not send ?
Should I wait for stream write() and catch exception ?
Any help would be appreciated.
I would like to know, how to trap an event, that the remote peer has
closed the connection.
Here is a simple script which sends a line, and receives one.
Socket socket ;
OutputStream outputStream = null ;
BufferedReader inputStream = null;
try {
socket = new Socket("mypeer", 5555) ;
socket.setKeepAlive(true) ;
socket.setSoTimeout(5000) ;
outputStream = new PrintStream(socket.getOutputStream());
inputStream = new BufferedReader(new InputStreamReader
(socket.getInputStream()));
String s = new String("This is to be sent\n") ;
outputStream.write(s.getBytes()) ;
outputStream.flush() ;
String r ;
if ((r = inputStream.readLine()) == null) {
System.out.println("NOTHING READ") ;
} else {
System.out.println("This was read: " + r) ;
}
outputStream.flush() ;
outputStream.close() ;
inputStream.close() ;
socket.close() ;
} catch (Exception e) {
e.printStackTrace() ;
}
Is the NULL from readLine() 100% sign that socket was closed by remote
peer, or may be just lack of data remote did not send ?
Should I wait for stream write() and catch exception ?
Any help would be appreciated.