Client Server Communication
Hi, im writting this application where the server can execute commands
on a remote client, commands like shutdown, restart and logoff.
But im having two problems, first, how can i send the command to a specific client (IP) and make sure that no oine else receives it, second, how can i fix the EOFException that occurs on the client application, the client is only listening and it does nothing but listening then executing the command using Runtime.getRuntime().exec(command); , but im having a problem with that exception.
the code is:
Server Side:
ServerSocket server;
Socket connection;
//
ObjectOutputStream output;
ObjectInputStream input;
//
......
......
//wait
connection = server.accept();
.......
.......
//streaming
// set up output stream for objects
output = new ObjectOutputStream(connection.getOutputStream());
// flush output buffer to send header information
output.flush();
// set up input stream for objects
input = new ObjectInputStream(connection.getInputStream());
......
......
//process
String command = "shutdown -r -f";
//
output.writeObject(command);
output.flush();
//
........
.......
Client Side:
Socket client;
ObjectInputStream in;
ObjectOutputStream out;
//
.....
.....
socket = new Socket(InetAddress.getByName(host), 6060);
//
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
//
..
..
//
String command = in.readObject().toString();
Runtime.getRuntime().exec(command);/*****the problem is here******/
Plz Help!!!!!
# 1
Your syntax both for exec() argument and for shutdown command is wrong. Also I doubt the client has the required permission for launching a sysadmin level command.
hiwaa at 2007-7-10 12:25:32 >

# 2
> But im having two problems, first, how can i send the> command to a specific client (IP) and make sure that> no oine else receives itTCP does that for you. If you write to a socket, only the peer at the other end will get that data.
ejpa at 2007-7-10 12:25:32 >

# 3
The code works fine, some problems occured when i tried to add actionListener so that when i click on the restart button and only when i press it the client restarts - the above code is for restart not shutdown - and EOFException occurs even though i tried sending blank or keepAlive messages to the clients, but without it the code is working, i tried it.
As for TCP, how? I mean i didnt specify a receipent IP address i just wrote the string to the socket, how can i specify a destination IP? Once i receive a connection from a client, its PC restarts without sending any command because i couldnt add the actionListener and without even specifying the desired IP, the server just writes it and the client receives it.
Regards.
# 4
Just write to the socket. You don't have to do anything special. If that client is still there it will get the data, and certainly nobody else can possibly even know it was sent let alone see it. The EOFException means that the peer has closed its end of the connection.
ejpa at 2007-7-10 12:25:32 >

# 5
hi omar
I am new to this socket programming and i am currently working on a networking project ...i just saw ur post of..the code of restarting the client system... i want the code of how u have established the connection between the client and the server....
if u can help me it wud be of great use to me and my partner .... if u can provide us the code... the last module of ur project ...which is shutting down of all the systems from a central server wud be done....
Hence i request u to kindly consider this ...and pls send us the code....
regards
Lalita.
# 6
hi hiwacan u pls tell us how to invoke a shutdown command in the java program if the client has got no system level permissions to shut down the system.... in that case what shud we do in the program to shutdown the system..regards Lalita
# 7
> if the client has got no system level permissions to shut down the systemJust give up, or, commit a crime called #$%&!!.
hiwaa at 2007-7-10 12:25:32 >

# 8
The code is simple, you shouldnt have any problems reading and understanding it.
you should start with the server side where you must specify a ServerSocket, and initializing it, it goes like:
ServerSocket server = new ServerSocket(...,...);
Then you must create a socket that both client and server can write to it:
Socket connection = server.accept();
(Put it in a loop)
After doing that you must specify an inputStream and an outputStream for the server to write into the socket, both must be initialized and ofcourse given a name.
Then simply process the connection by sending what ever you want and reading from the socket.
Finally close the connection by using a keyword or when the connection is terminated.
As for the client side, you must do the same as you did in the server side, except that here you dont have a ServerSocket, you only have a Socket that will connect to the desired server, for example, consider both server and client running on your machine (localhost), you must specify an IP address with 127.0.0.1, goes like:
Socket socketsocket = new Socket(InetAddress.getByName(host), 6060);
where host is string that equals 127.0.0.1.
After this, youll have to process the connection, get streams, and terminate the connection, just like in the server.
For further information on server/client programming, refer to:
www.java.sun.com/docs/books/tutorial/networking/sockets/index.html
www.slideshare.net/jignesh/socket-programming-tutorial/
>Just write to the socket. You don't have to do anything special. If that >client is still there it will get the data, and certainly nobody else can >possibly even know it was sent let alone see it. The EOFException >means that the peer has closed its end of the connection.
ejp thank you, but, What if i have multiple clients? like shown above, the server does not specify a target IP, it works properly in case of one client, but what if i have three clients and i want the command to be sent to only one client, and, how can i send blank or meaningless packets just to keep the line active to avoid EOFException? in my code im trying to develop it so that when the server requests a shutdown the remote command will be executed, and this is where the Exception occurs. if i remove the action listener from the code the command will be sent and executed once the connection is established, but no
Exception happens.
Regards.
# 9
> ejp thank you, but, What if i have multiple clients?
Then you will have multiple accepted sockets. Each one communicates with a single client.
> how can i send blank or meaningless packets just to keep the
> line active to avoid EOFException? in my code im
> trying to develop it so that when the server requests
> a shutdown the remote command will be executed, and
> this is where the Exception occurs.
EOFException means that somebody is trying to read something but the other end is already closed. It is an application protocol error, i.e. a bug in your code. You have too many reads and not enough writes.
ejpa at 2007-7-10 12:25:32 >

# 10
> Then you will have multiple accepted sockets. Each
> one communicates with a single client.
Okay, I guess that wouldnt be so hard, thank you!
> EOFException means that somebody is trying to read
> something but the other end is already closed. It is
> an application protocol error, i.e. a bug in your
> code. You have too many reads and not enough writes.
Okay, but why is it happening, I mean if you look at the code, the server is active, i know that the socket will stay empty untill that button -send command- is pressed, and i know that the client is trying to listen but nothing is there for it to read, but how can i fix this problem? is there any type of empty or null valued packets that will keep the client and the socket active untill the server makes the request, or at least make the socket wait for that request, i tried using wait() but its not supported
# 11
Problem solved, thanx a lot ejp.