connection reset exception in line input.readLine()
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sam_G
Posted On:   Monday, February 19, 2007 04:34 PM

I am getting connection reset exception in line input.readLine() as shown below. Any help will be appreciated. Code is shown below Socket socket = new Socket(); socket.connect(new InetSocketAddress("myMachine", 4444), 0); DataOutputStream output = new DataOutputStream(outputStream); DataInputStream input = new DataInputStream(socket.getInputStream()); String request = "abcd" // abcd is the string passed to server output.write(request.getBytes()); String responseLine; while (true) { responseLine = input.readLine(); os.println(responseLine); } On input.readLine() I get the exception as s   More>>

I am getting connection reset exception in line input.readLine() as shown below. Any help will be appreciated.



Code is shown below


			
Socket socket = new Socket();
socket.connect(new InetSocketAddress("myMachine", 4444), 0);

DataOutputStream output = new DataOutputStream(outputStream);
DataInputStream input = new DataInputStream(socket.getInputStream());
String request = "abcd" // abcd is the string passed to server
output.write(request.getBytes());

String responseLine;
while (true) {
responseLine = input.readLine();
os.println(responseLine);
}

On input.readLine() I get the exception as shown below:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:136)
at java.net.SocketInputStream.read(SocketInputStream.java:208)
at java.io.DataInputStream.readLine(DataInputStream
   <<Less

Re: connection reset exception in line input.readLine()

Posted By:   Jani_Kaarela  
Posted On:   Thursday, February 22, 2007 04:34 AM


Check the API - the API is your friend! More accurately, you should read what the "contract" for readLine() is, specifically the Returns part:


the next line of text from the input stream, or null if the end of file is encountered before a byte can be read



Thus, your code should read something like:


do {
responseLine = input.readLine();
if (responseLine != null) {
os.println(responseLine);
}
} while (responseLine != null);

Be warned, I did not invest much time & effort in this, nor did I check if it actually worked...



Also, while reading the API, pay attention to the fact that readLine() is deprecated. Consider converting your code to use non-deprecated methods!

About | Sitemap | Contact