0

I have 2 files Client Side & Server Side

I send a string over the socket from client to server. I have to execute this string as one does in a terminal. The output of the command is to be displayed on the client side.

Server Side Code : this loop runs on each thread created by pthread_create

  while((n=recv(sock,client_message,2000,0))>0)
  {

    send(sock,server_out,n,0);
  }

I need to run the string i recieve in client_message as a terminal command and fetch the output of the command and send it back via the server_out string buffer.

How do i go about this ?

Manmeet S. Oberoi
  • 943
  • 1
  • 12
  • 28

1 Answers1

0

So - you have two or three different tasks to accomplish.

The first one is to run the command line you received on the server. For that, you can start reading the system() function. It's very straightforward to use.

But then you need to get it's output. You can read about this two points in this question.

Lastly, send that data back to the server - once you have the output stream, it's just send()ing that via the socket. You can implement some mini-protocol for telling the other side how many bytes to expect, some error detection/correction if you want, etc.

Once the data arrives the client, then you can do whatever you want with it - print it on the screen, save to a file, you name it.

Read about this things, take your chances, and come back to continue asking if you need it - good luck!

Community
  • 1
  • 1
mgarciaisaia
  • 12,099
  • 8
  • 48
  • 73