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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SocketException - Connection reset by peer : JVM_recv in socket in inp

Status
Not open for further replies.

sedj

Programmer
Aug 6, 2002
5,610
Hi,

have been getting a message :

SocketException - Connection reset by peer : JVM_recv in socket in input stream

which I cannot work out ... Have skinned my code back to two simple test classes - one which handles a ServerSocket, and passes Sockets to a thread, and one which opens a socket and trys to pass a string. Below is the code. The exception is thrown when the thread in class Test trys to create the BufferedReader ...
Curiously though, the error is intermittent.
Hope someone can help !

Regards,

Ben


/////////////the serversocket class /////////////

import java.net.*;
import java.io.*;

public class Test {
static Thread thread;

public static void main(String args[]) {

try {
ServerSocket ss = new ServerSocket(1234);
System.out.println("Listening on port ...");
while (true) {
processClient(ss.accept());
thread.start();
}
}
catch (IOException e) {
System.out.println(e.toString());
}

}


public static void processClient(Socket s) {
final Socket client = s;
System.out.println("Recieved Socket connection ...");
thread = new Thread(new Runnable() {
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String message = in.readLine();
System.out.println("Message was " +message);
}
catch (IOException e) {
System.out.println(e.toString());
}

}
});
}


}

/////////////////////////////////////////////////////
///////// and the Socket test class /////////////////
import java.net.*;
import java.io.*;

public class Test1 {

public static void main(String args[]) {

try {
Socket s = new Socket("localhost", 1234);
System.out.println("opened socket");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("Hello");
System.out.println("sent message");
}
catch (IOException e) {
System.out.println(e.toString());
}

}
}
 
Hi Ben,

you might want to change to the red pieces of code below in your Socket Test Class (Test1):

public class Test1
{
public static void main(String args[])
{
try
{
Socket s = new Socket("localhost", 1234);
System.out.println("opened socket");
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.println("Hello");
out.flush();
System.out.println("sent message");
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}

Your Server socket class is using in.readline() which expects a carriage return character at the end of a single message. out.write() doesnt provide a carriage return character at the end, but if you still want to use it i suggest you explicitly specify the carriage return in the print string.

The readline() is waiting forever for the carriage return, and since there is none coming in from the client, when the client finished execution it will throw the "Connection Reset by Peer" error.

hope this helps,
shahid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top