This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes java.net.SocketException: Connection reset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "java.net.SocketException: Connection reset" Watch "java.net.SocketException: Connection reset" New topic
Author

java.net.SocketException: Connection reset

harry ganguly
Greenhorn

Joined: Dec 14, 2010
Posts: 13
Ive requirement to send some command line argument to server. So Ive written server code which opens a port in server:
Server:
public class FTPServer {

private ServerSocket serverSocket;
FTPServer(int port) {

try
{
serverSocket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
int i=0;

while(true)
{
i=i+1;
Socket socket = serverSocket.accept(); // accept connection
System.out.println("New client asked for a connection");

TcpThread t = new TcpThread(socket,i); // make a thread of it
System.out.println("Starting a thread for a new Client");
t.start();
}
}
catch (IOException e) {
System.out.println("Exception on new ServerSocket: " + e);
}
}

public static void main(String[] args) {
int port=0;
for (String s: args)
{
port=Integer.parseInt(args[0]);
}
System.out.println("Port: "+port);
new FTPServer(port);
}

/** One instance of this thread will run for each client */
class TcpThread extends Thread {
Socket socket;
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
String command=null;
int j=0;

TcpThread(Socket socket,int i) {
this.socket = socket;
j=i;
}
public void run() {
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output Streams");
try
{
Soutput = new ObjectOutputStream(socket.getOutputStream());
Soutput.reset();
Sinput = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
System.out.println("Thread waiting for a String from the Client");
try {
String str = (String) Sinput.readObject();

Runtime.getRuntime().exec(str);
command="Server replying back for the "+j+"th time";
Soutput.writeObject(command);
Soutput.flush();
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
catch (ClassNotFoundException o) {
}
finally {
try {
Soutput.close();
Sinput.close();
}
catch (Exception e) {
}
}
}
}
}
Client:
ObjectOutputStream Soutput; // towrite on the socket
ObjectInputStream Sinput; // to read the socket
Socket socket;

try {
socket = new Socket(ip, port);
} catch (Exception e) {
fail("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " + socket.getInetAddress() + ":" + socket.getPort() + "\n");
/* Creating both Data Streams */
try {
Soutput = new ObjectOutputStream(socket.getOutputStream());
Soutput.reset();
System.out.println(socket.getInputStream());
Sinput = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
return;
}
// send the command (String) to the server
info("Client sending \"" + command + "\" to server\n");
try {
Soutput.writeObject(command);
Soutput.flush();
} catch (IOException e) {
e.printStackTrace();
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
} catch (Exception e) {
e.printStackTrace();
}
try {
Sinput.close();
Soutput.close();
} catch (Exception e) {
}
harry ganguly
Greenhorn

Joined: Dec 14, 2010
Posts: 13
Server opens a port in server machine... and client send a command to server to execute...
example: cmd /c notepad.exe.
This will open notepad in server.

The server code is working fine and is opening a port as required. But when same client code is throwing "java.net.SocketException: Connection reset" exception when I run. Please note:
Client machine frm which Im running the client code is the same. But the server machine changes. In one server machine, it's working fine, but in another server machine it's not... what might be the issue here???
could someone please suggest.
 
Don't get me started about those stupid light bulbs.
 
subject: java.net.SocketException: Connection reset
 
Similar Threads
java.io.StreamCorruptedException : invalid stream header: 4348414C
Establishing two-way buffered streams
Creating a Application that runs both a client and a server (p2p)
Can I sit on ois.readObject() and wait for more packets? More questions
Server / Client with Threads