Question

Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

Run the code in Linux and provide the screenshot of the output and input

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

static void cleanup();
static void docleanup(int signum);

static const char *SERVER_ADDR = "127.0.0.1";
static const int SERVER_PORT = 61234;

static int cfd = -1;

int main(int argc, char *argv[]) {
struct sockaddr_in saddr;
char buf[128];
int bufsize = 128,
bytesread;
struct sigaction sigact;

printf("client starts running ...\n");

atexit(cleanup);
sigact.sa_handler = docleanup;
sigaction(SIGINT, &sigact, NULL);

cfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == cfd) {
perror("socket");
exit(EXIT_FAILURE);
}

saddr.sin_family = AF_INET;
saddr.sin_port = htons(SERVER_PORT);
if (0 == inet_pton(AF_INET, SERVER_ADDR, &saddr.sin_addr)) {
fprintf(stderr, "cannot convert %s to IP address\n", SERVER_ADDR);
exit(EXIT_FAILURE);
}
if (-1 == connect(cfd, (struct sockaddr *)&saddr, sizeof(saddr))) {
perror("connect");
exit(EXIT_FAILURE);
}

while (1) {
printf("Enter message: ");

if (NULL == fgets(buf, bufsize-1, stdin)) {
fprintf(stderr, "failed to read stdin\n");
exit(EXIT_FAILURE);
}
buf[strcspn(buf, "\r\n")] = '\0';
printf("user entered: %s\n", buf);

if (-1 == send(cfd, buf, bufsize-1, MSG_NOSIGNAL)) {
perror("send");
exit(EXIT_FAILURE);
}
printf("client sent: %s\n", buf);

bytesread = recv(cfd, buf, bufsize, 0);
if (-1 == bytesread) {
perror("recv");
exit(EXIT_FAILURE);
}
buf[bytesread] = '\0';
printf("client received: %s\n", buf);
if (strncasecmp(buf, "Bye!", 4) == 0) {
printf("server echoed 'Bye!', and exiting ...\n");
break;
}
}

return 0;
}

static void cleanup() {
if (cfd >= 0) close(cfd);
printf("client cleaned up and exiting ...\n");
}

static void docleanup(int signum) {
if (SIGINT == signum) printf("User pressed CTRL-C\n");
exit(signum);
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

The provided code is executed in linux environment.

Screenshots:

Error Statement: The provided code is a client to interact with a socket server. But the provided code is only client code. So it is not possible to connect the server. The client outputs connection is refused and started cleaning up the connection.

Requirements: Server program should be provided to run the server and to interact with it using client program provided.

Add a comment
Know the answer?
Add Answer to:
Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). Y...

    Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to send messages from one to the other and terminate them by entering "end" //msgl.cpp / Here's the receiver program. / #include #include #include #1nclude #include <stdlib.h> <stdio.h> <string.h> <errno.h> <unistd.h> #include <sys/types.h> #include <sys/ipc.h> Winclude <sys/msg.h> struct my msg st f long int...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT