//
// q1_client.c
//
// Question:
/*
Using sockets to write a server/client program as described below. The program does a very simple task. Here is what the program does.
a) The client connects to the server. Once successfully, it sends a simple Unix/Linux command. For simplicity, the command sent is a simple command without any command line arguments, such as ls, pwd, and ps, etc.
b) The server receives the command sent by the client, executes the command, and sends the output of the command back to the client. The client then displays the output. Hint: you could redirect the output of the server running the command to the connected socket to send the output from running the Linux command to the client.
Your program only needs to support one client. For simplicity, the client is only required to send one command. You could use any allowed port number. You could assume the output from the command is less than 40960 bytes.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[]){
char buffer[40960];
int csd;
socklen_t len;
if(argc<2){ printf("hint: Enter a command to run this program properly.\n"); exit(1); } strcpy(buffer, argv[1]); struct sockaddr_in servAdd; servAdd.sin_family = AF_INET; servAdd.sin_addr.s_addr = inet_addr("192.168.0.102"); servAdd.sin_port = 7777; csd = socket(AF_INET, SOCK_STREAM, 0); connect(csd, (struct sockaddr *) &servAdd, sizeof(servAdd)); write(csd, buffer, strlen(buffer) + 1); read(csd, buffer, 40960); printf("result of command \"%s\" received from server: \n%s\n", argv[1], buffer); close(csd); return 0; }