The following program sets up a single client/server connection. When the client connects, the server reads any incoming data as text and prints it out one line at a time. If the server receives the string “shutdown”, then it closes the socket and exits. Download this program Download this program into GCC and run the code. Then update your code to allow for the server to manage concurrent connections on the same port. Use IP address 127.0.0.1. Page of 3 ZOOM /* Server code */ #define BUF_SIZE 1024 #define LISTEN_PORT 8080 int main(int argc, char *argv[]) { int sock_listen, sock_recv; int i, addr_size, bytes_received; int incoming_len; int recv_msg_size; struct sockaddr_in my_addr, recv_addr; int select_ret; fd_set readfds; struct timeval timeout={0,0}; struct sockaddr remote_addr; char buf[BUF_SIZE]; /* create socket for listening */ sock_listen = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock_listen < 0) { printf("socket() failed\n"); exit(0); } /* make local address structure */ memset(&my_addr, 0 sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons((unsigned short)LISTEN_PORT); /* bind socket to the local address */ i = bind(sock_listen, (struct sockaddr *) &my_addr, sizeof(my_addr)); if (i < 0) { printf("bind() failed\n"); exit(0); } /* listen */ i = listen(sock_listen, 5); if (i < 0) { printf("listen() failed\n"); exit(0); } /* get new socket to receive data on */ addr_size = sizeof(recv_addr); sock_recv = accept(sock_listen, (struct sockaddr *) &recv_addr, &addr_size); while (1) { bytes_received = recv(sock_recv, buf, BUF_SIZE, 0); buf[bytes_received] = 0; printf("Received: %s\n", buf); if (strcmp(buf, "shutdown") == 0) break; } close(sock_recv); close(sock_listen); } /* Client code */ #define BUF_SIZE 1024 #define SERVER_IP "192.168.0.1" #define SERVER_PORT 8080 int main(int argc, char *argv[]) { int sock_send; int i; int send_len, bytes_sent; char text[80], buf[BUF_SIZE]; struct sockaddr_in addr_send; /* create socket for sending data */ sock_send = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock_send < 0) { printf("socket() failed\n"); exit(0); } /* create socket address structure to connect to */ memset(&addr_send, 0, sizeof(addr_send)); addr_send.sin_family = AF_INET; addr_send.sin_addr.s_addr = inet_addr(SERVER_IP); addr_send.sin_port = htons((unsigned short) SERVER_PORT); /* connect to server */ i = connect(sock_send, (struct sockaddr *) &addr_send, sizeof(addr_send)); if (i < 0) { printf("connect() failed\n"); exit(0); } while (1) { /* send some data */ printf("Send? "); scanf("%s", text); if (strcmp(text, "quit") == 0) break; strcpy(buf, text); send_len = strlen(text); bytes_sent = send(sock_send, buf, send_len, 0); } close(sock_send); } Please provide the complete code for server as well as client side
The following program sets up a single client/server connection. When the client connects, the server reads any incoming data as text and prints it out one line at a time. If the server receives the string “shutdown”, then it closes the socket and exits. Download this program Download this program into GCC and run the code. Then update your code to allow for the server to manage concurrent connections on the same port. Use IP address 127.0.0.1. Page of 3 ZOOM /* Server code */ #define BUF_SIZE 1024 #define LISTEN_PORT 8080 int main(int argc, char *argv[]) { int sock_listen, sock_recv; int i, addr_size, bytes_received; int incoming_len; int recv_msg_size; struct sockaddr_in my_addr, recv_addr; int select_ret; fd_set readfds; struct timeval timeout={0,0}; struct sockaddr remote_addr; char buf[BUF_SIZE]; /* create socket for listening */ sock_listen = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock_listen < 0) { printf("socket() failed\n"); exit(0); } /* make local address structure */ memset(&my_addr, 0 sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons((unsigned short)LISTEN_PORT); /* bind socket to the local address */ i = bind(sock_listen, (struct sockaddr *) &my_addr, sizeof(my_addr)); if (i < 0) { printf("bind() failed\n"); exit(0); } /* listen */ i = listen(sock_listen, 5); if (i < 0) { printf("listen() failed\n"); exit(0); } /* get new socket to receive data on */ addr_size = sizeof(recv_addr); sock_recv = accept(sock_listen, (struct sockaddr *) &recv_addr, &addr_size); while (1) { bytes_received = recv(sock_recv, buf, BUF_SIZE, 0); buf[bytes_received] = 0; printf("Received: %s\n", buf); if (strcmp(buf, "shutdown") == 0) break; } close(sock_recv); close(sock_listen); } /* Client code */ #define BUF_SIZE 1024 #define SERVER_IP "192.168.0.1" #define SERVER_PORT 8080 int main(int argc, char *argv[]) { int sock_send; int i; int send_len, bytes_sent; char text[80], buf[BUF_SIZE]; struct sockaddr_in addr_send; /* create socket for sending data */ sock_send = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock_send < 0) { printf("socket() failed\n"); exit(0); } /* create socket address structure to connect to */ memset(&addr_send, 0, sizeof(addr_send)); addr_send.sin_family = AF_INET; addr_send.sin_addr.s_addr = inet_addr(SERVER_IP); addr_send.sin_port = htons((unsigned short) SERVER_PORT); /* connect to server */ i = connect(sock_send, (struct sockaddr *) &addr_send, sizeof(addr_send)); if (i < 0) { printf("connect() failed\n"); exit(0); } while (1) { /* send some data */ printf("Send? "); scanf("%s", text); if (strcmp(text, "quit") == 0) break; strcpy(buf, text); send_len = strlen(text); bytes_sent = send(sock_send, buf, send_len, 0); } close(sock_send); } Please provide the complete code for server as well as client side in C
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images