• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.net.SocketException: Connection reset

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic