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) {
}