In: Computer Science
Java or python. The clients need to be able to communicate with each other using the command line. The "local chat" program should be implemented using a client/Server model based on UDP protocol.
Requirements:
1. The chat is performed between 2 clients and not the server.
2. The server will first start up and choose a port number. Then the server prints out its IP address and port number.
3. The client then will start up and create a socket based on the information provided by the server.
4. Now the client and the server can chat with each other by sending messages over the network.
5. The client/server MUST be able to communicate with each other in a continuous manner. (e.g. One side can send multiple messages without any replies from the other side)
6. There is no requirement on what language you use to implement this project, but your program should call upon the socket API for UDP to realize the functionality.
7. You should demonstrate your project using two machines (one for the client and one for the server. Pick your virtual machine as the client to communicate with your actual machine, which is server in our project).
8. You need to open at least 2 cmd window in your virtual machine to demonstrate multiple machine communicate with your server machine (actual machine)
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0
) {
perror("socket creation
failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello,
strlen(hello),
MSG_CONFIRM, (const struct sockaddr
*) &servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *)
&servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
close(sockfd);
return 0;
}