Question Description

this project has to be written

// forwarder.c by Dr. Junwan Kim // This program MUST be used for only csci315 // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define PORTNUM1 2300 #define PORTNUM2 2400 #define MAXRCVLEN 500 int main(int argc, char *argv[]) { char* msg = “HI CSCI315 from forwardern”; char buffer[MAXRCVLEN + 1]; int len, consocket; struct sockaddr_in dest; struct sockaddr_in serv; struct sockaddr_in client; int server_socket; int client_socket; // For server socklen_t socksize = sizeof(struct sockaddr_in); memset(&serv, 0, sizeof(serv)); serv.sin_family = AF_INET; serv.sin_addr.s_addr = htonl(INADDR_ANY); serv.sin_port = htons(PORTNUM1); server_socket = socket(AF_INET, SOCK_STREAM, 0); bind(server_socket, (struct sockaddr *)&serv, sizeof(struct sockaddr)); // For client client_socket = socket(AF_INET, SOCK_STREAM, 0); memset(&client, 0, sizeof(client)); client.sin_family = AF_INET; client.sin_addr.s_addr = htonl(INADDR_ANY); client.sin_port = htons(PORTNUM2); connect(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr)); len=recv(client_socket, buffer, MAXRCVLEN, 0); if(len <0){ close(client_socket); perror(“Reciver failed”); return EXIT_FAILURE; } buffer[len]=’ ‘; printf(“As a client, I Received %s n”, buffer); memset(buffer, 0, strlen(buffer)); printf(“Server starts…n”); do{ listen(server_socket, 1); consocket = accept(server_socket, (struct sockaddr *)&dest, &socksize); send(consocket, msg, strlen(msg), 0); while(len=recv(consocket, buffer, MAXRCVLEN, 0)>0){ printf(“Received: %s “, buffer); // As a client, send “buffer” here if(strncmp(buffer, “exit”, 4) == 0){ break; } memset(buffer, 0, strlen(buffer)); } printf(“Goodbye !n”); close(consocket); }while(consocket); close(client_socket); close(server_socket); return EXIT_SUCCESS; }2// //TCP_Client.c by Dr. Junwhan Kim //This MUST be used for only CSCI315 at UDC // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define BUFLEN 512 //Max length of buffer #define MAXRCVLEN 500 #define PORTNUM 2300 #define TCP_ServerPORT 1234 void DieWithError(char *s) { perror(s); exit(1); } int main(int argc, char *argv[]) { char buffer[MAXRCVLEN + 1]; // for receiving int len, mysocket; struct sockaddr_in dest; char msg[MAXRCVLEN + 1]; // for sending mysocket = socket(AF_INET, SOCK_STREAM, 0); memset(&dest, 0, sizeof(dest)); dest.sin_family = AF_INET; dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK); dest.sin_port = htons(PORTNUM); // TCP server ******************* // TCP server code must be here // ****************************** connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr)); len = recv(mysocket, buffer, MAXRCVLEN, 0); if(len < 0){ perror(“Reciver failed”); close(mysocket); return EXIT_FAILURE; } else{ buffer[len] = ‘ ‘; printf(“I Received %s n”, buffer); } memset(buffer, 0, strlen(buffer)); // Create a socket, bind, listne, and sccept code for TCP_server **************** // Your code must be here. // ************************************************* while(1){ printf(“Enter a character “); fgets(msg, sizeof(msg), stdin); if(strncmp(msg, “exit”, 4) == 0) break; if(send(mysocket, msg, strlen(msg), 0) < 0){ perror(“Send failed”); close(mysocket); return EXIT_FAILURE; } memset(msg, 0, strlen(msg)); printf(“Waiting for data from a server…”); fflush(stdout); // As a server, you must receive somthing here. printf(“Recieved Data: %sn” , buffer); memset(buffer, 0, strlen(buffer)); } close(mysocket); return EXIT_SUCCESS; }

3

// TCP_Server.c by Dr. Junwan Kim // This program MUST be used for only csci315 // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define PORTNUM 2400 #define TCP_ClientPORT 1234 #define MAXRCVLEN 500 #define SERVER “127.0.0.1” // local server #define BUFLEN 512 //Max length of buffer void DieWithError(char *s) { perror(s); exit(1); } int main(int argc, char *argv[]) { char* msg = “HI from Servern”; char buffer[MAXRCVLEN + 1]; int len, consocket; struct sockaddr_in dest; struct sockaddr_in serv; int mysocket; socklen_t socksize = sizeof(struct sockaddr_in); char test[100]; memset(&serv, 0, sizeof(serv)); serv.sin_family = AF_INET; serv.sin_addr.s_addr = htonl(INADDR_ANY); serv.sin_port = htons(PORTNUM); // ***** TCP Client ************** // Your code must be here // // ************************** mysocket = socket(AF_INET, SOCK_STREAM, 0); bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr)); do{ listen(mysocket, 1); consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize); send(consocket, msg, strlen(msg), 0); if(flag == 0) { fgets(test, sizeof(test), stdin); connect(client_socket, (struct sockaddr *)&client_dest, sizeof(struct sockaddr)); flag =1; } while(len=recv(consocket, buffer, MAXRCVLEN, 0)>0){ if(strncmp(buffer, “exit”, 4) == 0){ break; } printf(“Server Received: %s “, buffer); // TCP client code must be here memset(buffer, 0, strlen(buffer)); } printf(“Goodbye !n”); close(consocket); }while(consocket); close(mysocket); return EXIT_SUCCESS; }