C socket : how Execute shell program from server, and passing the output to client?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C socket : how Execute shell program from server, and passing the output to client?
Dear,
I am a new member of this forum.
I'm learning about Linux C socket programming right now and got problem about command line execution from C program.
I have one server daemon (server.c) and client program (client.c). When server running, client try to execute a command line that describe in argument of client command line.
Is there any method how to make server wait for command line result output, and then send all of result to client? Please let me know.
If I understand you correctly, you want the server to wait till the client has processed the command and dumped its output.
If so, I do not really understand the wait part. What do you want to happen after the wait? And what happens now? And how do you want it to work when client and server run on different computers.
Usually servers run forever. A client connects to the server, the server accepts the connection, communication happens, and when the client is done, it disconnects. The server will keep on running, waiting for new connections.
Normally, you will define a commnication protocol. In your case, the client will send a command (or reply) to the server that it's done with what it's doing.
Last edited by Wim Sturkenboom; 10-24-2007 at 11:15 PM.
1. enter a 'shell type' command in the client
2. client will send the request to the server via a socket
3. the server will execute the command
4. results sent back over a socket to the client
5. display the results on the client?
Is this correct?
I'm assuming this is some sort of exercise since there are tools that will do this for you already, (rsh). Assuming that you have the sockets down, it sounds like you are seeking the popen command to execute and save the resutls, when then can be sent back to the client.
If this will be used for anything other than an exercise or perhaps a school project (yes it reeks), please think of the security implications here. It really exposes your system.
To elaborate a bit more on the communication protocol. Usually clients instruct servers to do thing and the servers reply.
e.g below is how an ftp server and ftp client communicate (not 100% accurate)
Code:
server waits for connection fro client
client connects and waits for welcome message
server sends welcome message and waits for command
client receives reply
client sends command and waits for reply (e.g ls)
server receives and processes command (e.g. creates a list of files)
server sends reply (e.g list of files) and waits for new command
client receives reply
client sends command and waits for reply (e.g del abc.txt)
server receives and processes command (deletes file abc.txt)
server sends reply (e.g delete pass or fail) and waits for new command
etc
etc
etc
client sends command (e.g. bye), terminates connection and ends
server receives and processes command
server sees that connection is terminated and cleans up it's connection
In your case the client does the work. We can work out a similar protocol based on linux command
Code:
server waits for connection from client
server sends command 'ls' and waits for reply
client receives command and processes it (dump directory listing on screen)
client sends reply that it's done (e.g. 'OK' or 'FAIL')
if FAIL, client terminates connection
if OK, client waits for next command
server receives reply
if reply is FAIL, does something
if reply is OK, server sends 'cat abc.txt' and waits for reply
client receives command and processes it (dump file content)
client sends reply that it's done (e.g. 'OK' or 'FAIL')
if FAIL, client terminates connection
if OK, client waits for next command
etc
etc
etc
server sends command 'quit'
client receives command and terminates connection
Hope that this helps you on the way.
Last edited by Wim Sturkenboom; 10-25-2007 at 11:03 PM.
Reason: added missing code tag
1. enter a 'shell type' command in the client
2. client will send the request to the server via a socket
3. the server will execute the command
4. results sent back over a socket to the client
5. display the results on the client?
Is this correct?
Yes, that is what I mean.
Quote:
Originally Posted by crabboy
I'm assuming this is some sort of exercise since there are tools that will do this for you already, (rsh). Assuming that you have the sockets down, it sounds like you are seeking the popen command to execute and save the resutls, when then can be sent back to the client.
Your steps explanation are match with my scenario. But I got problem: how to make server pass the execution results to the client. (Step number 4)
Quote:
Originally Posted by crabboy
If this will be used for anything other than an exercise or perhaps a school project (yes it reeks), please think of the security implications here. It really exposes your system.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.