Research the Java network API for UPD and re-implement the client/server program in Java. You do not need to fork two processesyou can write two separate programs, one for the client, one for the server. Have the server support multiple clients.
#include /* for fork() */ #include /* for socket() */ #include /* for errno */ #include /* for printf() */ #include /* for exit() */ #include /* for strcpy() */ #include /* for errno and strerror() */ #include /* for gethostent() */ #define PORT_CLIENT 3490 #define PORT_SERVER 3491 #define MAXLINE 1024 void Print(struct sockaddr_in *sa, char *which) { printf("%s: ", which); printf("Socket address family: %d. ", sa->sin_family); printf("Socket address port: %hu. ", sa->sin_port); printf("Socket address IP: %u. ", sa->sin_addr.s_addr); } Server Client System Call Meaning System Call Meaning socket() create socket and attach host and port information socket() create socket and attach host and port information bind() bind socket to a filename sendto() send, on its own fd, message to server, passing the servers sendto() info as an argument send, on its own fd socket, message to client recvfrom() receive message from client, and capture client IP/port information in fill-in argument socketaddr recvfrom() receive message from server, and capture client IP/port information in fill-in argument socketaddr int main(int argc, char **argv) { /* Allocate memory for struct sockaddr */ int fd_client, fd_server1; char buffer[MAXLINE]; /* this will receive message */ char *fromServer="Hello from server!"; char *fromClient="Hello from client!"; struct sockaddr_in servaddr, cliaddr; /* child=client */ if (fork()==0) { /* Client side */ /* Creating socket file descriptor */ if ((fd_client=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0) { perror("Socket creation failed.); exit(EXIT_FAILURE); } /* The equivalent of malloc() for non-pointers */ memset(&cliaddr, 0, sizeof(cliaddr)); /* Fill in struct sockaddr with client information */ cliaddr.sin_family=AF_INET; cliaddr.sin_addr.s_addr=INADDR_ANY; cliaddr.sin_port=htons(PORT_CLIENT); Print(&cliaddr, CLIENT"); /* Filling server information */ servaddr.sin_family=AF_INET; struct hostent *hp=gethostbyname("localhost"); hp=gethostbyname("localhost"); memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length); servaddr.sin_port=htons(PORT_SERVER); Print(&servaddr, "SERVER"); unsigned int n, bytes_sent, bytes_rcvd; /* Call sendto() to send client message to server */ printf("About to send from CLIENT... "); bytes_sent=sendto(fd_client, (const char *)fromClient, strlen(fromClient), MSG_WAITALL, (const struct sockaddr *)&servaddr, sizeof(servaddr)); printf("%s ", strerror(errno)); printf("Sent %d bytes to server. ", bytes_sent); printf("Sent from CLIENT: %s ", fromClient); /* Allocate another struct sockaddr variable for servers just to illustrate that recvfrom() fills it in with incoming info */ struct sockaddr_in server_addr; memset(&server_addr, 0, sizeof(server_addr)); n=sizeof(server_addr); /* Call recvfrom() to get server message and info */ bytes_rcvd=recvfrom(fd_client, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *)&server_addr, &n); Print(&server_addr, ###SERVER"); printf("Received %d bytes from server. ", bytes_rcvd); printf("Received at CLIENT: %s ", buffer); close(fd_client); } /* parent=server */ else { /* Server side */ /* Creating socket file descriptor */ if ((fd_server1=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0) { perror("socket creation failed"); exit(EXIT_FAILURE); } memset(&servaddr, 0, sizeof(servaddr)); /* Filling server (self) information */ servaddr.sin_family=AF_INET; /* IPv4 */ struct hostent *hp=gethostbyname(localhost"); memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length); servaddr.sin_port=htons(PORT_SERVER); Print(&servaddr, SERVER"); /* Bind the socket with the server address */ if (bind(fd_server1, (const struct sockaddr *)&servaddr, sizeof(servaddr))<0) { perror("Bind failed.); exit(EXIT_FAILURE); } sleep(5); unsigned int len; int bytes_sent, bytes_rcvd; /* Declare a new struct sockaddr for the client just to illustrate that recvfrom() fills it in again */ struct sockaddr client_addr; memset(&client_addr, 0, sizeof(client_addr)); len=sizeof(client_addr); /* Call recvfrom() to get client message and info */ bytes_rcvd=recvfrom(fd_server1, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *)&client_addr, &len); Print((struct sockaddr_in *)&client_addr, "###CLIENT"); printf("Received %d bytes from client. ", bytes_rcvd); printf("Received at SERVER: %s ", buffer); /* Call sendto() to send client message w. self info */ bytes_sent=sendto(fd_server1, (const char *)fromServer, strlen(fromServer), 0, (const struct sockaddr *)&client_addr, sizeof(client_addr)); printf("***%s ", strerror(errno)); printf("Sent from SERVER: %s ", fromServer); printf("Sent %d bytes to client. ", bytes_sent); } return 0; }