Question: Below is my code and output I have an error in my client portion of the code : char *hello = ps ; can you

Below is my code and output

I have an error in my client portion of the code : char *hello = "ps ";

can you please help me fix this error so my code runs properly

===================================s.cpp(server)==========================================

#include

#include

#include

#include

#include

#include

#include

#include

#define PORT 8080

using namespace std;

int main(int argc, char const *argv[])

{

int sock_fd, n_sock; //server_fd and new socket

struct sockaddr_in addr;

int opt = 1;

int addrlen = sizeof(addr);

char my_buff[1024] = {0};

char add[] = " >> myfile";

int fd;

// Creating socket fd

if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)

{

cout<<"socket failed"<

exit(EXIT_FAILURE);

}

// Forcefully attach socket to the port 8080

if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,

&opt, sizeof(opt)))

{

cout<<"setsockopt error"<

exit(EXIT_FAILURE);

}

// setting port no and inet addr to any

addr.sin_family = AF_INET;

addr.sin_addr.s_addr = INADDR_ANY;

addr.sin_port = htons( PORT );

// Forcefully attaching socket to the port 8080

if (bind(sock_fd, (struct sockaddr *)&addr,

sizeof(addr))<0)

{

cout<<"bind failed";

exit(EXIT_FAILURE);

}

if (listen(sock_fd, 3) < 0)

{

cout<<"listen error"<

exit(EXIT_FAILURE);

}

if ((n_sock = accept(sock_fd, (struct sockaddr *)&addr,

(socklen_t*)&addrlen))<0)

{

cout<<"accept error"<

exit(EXIT_FAILURE);

}

close(1);

while(1)

{

/*

Logic: we will redirect the output of executable command to one file and thern read that file using read

so here, output is redirected to myfile and then reading that file using read()call. again send that dayta in buffer via socket

*/

memset(my_buff,'\0',1024);

read( n_sock , my_buff, 1024);

// printf("my_buff:%s ",my_buff );

strcat(my_buff,add);

fd = open("myfile",O_RDWR|O_TRUNC,0666);

// printf("%s ",my_buff);

system(my_buff);

// memsetting buffer to nill

memset(my_buff,'\0',1024);

// reading data

read(fd,my_buff,sizeof(my_buff));

close(fd);

// sending data

send(n_sock , my_buff ,sizeof(my_buff) , 0 );

}

return 0;

}

=======================================================

==========================c.cpp (client. file)===============================================

#include #include #include #include #include #include #include using namespace std; #define PORT 8080

int main(int argc, char const *argv[]) { struct sockaddr_in address; int sock = 0 ; struct sockaddr_in serv_addr; char *hello = "ps "; // command to send option as of noew it is ps "it can be set to command line also" (LINE WITH THE ERROR) char buffer[1024] = {0}; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { cout<<" Socket creation error "; return -1; }

memset(&serv_addr, '0', sizeof(serv_addr));

serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { cout<<" Invalid address/ Address not supported "; return -1; }

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { cout<<" Connection Failed "; return -1; } send(sock , hello , strlen(hello) , 0 ); // sending command cout<<"Client: message sent ";

// reading command read( sock , buffer, 1024); cout<<"Client output: "<

/* compilation and output compile file server by c++ s.cpp -o s

compile client file by c++ c.cpp -o c

shell:~$ ./s & [1] 5974 shell~$ ./c Client: message sent Client output: PID TTY TIME CMD 5151 pts/0 00:00:00 bash 5974 pts/0 00:00:00 s 5975 pts/0 00:00:00 c 5976 pts/0 00:00:00 sh 5977 pts/0 00:00:00 ps

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!