Possible Duplicate:
Java socket sends some data during connection to server
I have two very very simple java programs – client and server.They establish connection through sockets.
Server reads characters from stream and prints them to console.
import java.net.*;
import java.io.*;
import java.util.Date;
public class DataServerSIMS {
static final int LISTENING_PORT = 2002;
public static void main(String[] args) {
ServerSocket listener; //Checks the connection requests
Socket connection; //To interact with other progs
Reader incoming; //Stream
try {
listener = new ServerSocket(LISTENING_PORT);
TextIO.putln("Listening on port "+LISTENING_PORT);
while (true) {
connection = listener.accept();
try {
incoming = new InputStreamReader(connection.getInputStream());
while (true) {
int ch = incoming.read();
if (ch == 1 || ch == '\r')
break;
System.out.print((char)ch);
}
System.out.println();
incoming.close();
}
catch (IOException e) {
TextIO.putln("Error: "+e);
}//end try recieving data
}//end while
}
catch (Exception e) {
TextIO.putln("Sorry, the server has shut down.");
TextIO.putln("Error: "+e);
return;
}
}
}
Client only establishes socket-connection.
import java.net.*;
import java.io.*;
import java.util.Date;
public class TempClientSIMS {
//static final int LISTENING_PORT = 32007;
static final int LISTENING_PORT = 2002;
public static void main(String[] args) {
ServerSocket listener; //Checsk the connection requests
Socket connection; //To interact with other progs
Socket connectionToServer = null; //Socket
Reader incoming; //Stream
try {
TextIO.putln("Localhost connects to:"+LISTENING_PORT);
connectionToServer = new Socket ("localhost", 2002);
TextIO.putln("Connected ");
long i=0;
while (i<2000000000) {
i++;
}
}
catch (Exception e) {
TextIO.putln("Sorry, the server has shut down.");
TextIO.putln("Error: "+e);
return;
}
}
}
Client doesn’t send any data to server.
I launched both programs on my PC.
Client printed:
Localhost connects to:2002
Connected
Press any key to continue . . .
Server printed:
Listening on port 2002
FdsfsdfsError: java.net.SocketException: Connection reset
It is clear why this string “Error: java.net.SocketException: Connection reset” is printed on server’s console.
But I haven’t any idea what does it mean: “Fdsfsdfs“?
And can I prevent the printing of this string “Fdsfsdfs“?
You don’t check for End of Stream using Reader. Add that check to your code.
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/Reader.html#read%28%29