Consider the following programs: -server.c ( https://ufile.io/r6578 ) -client.c
ID: 3851377 • Letter: C
Question
Consider the following programs:
-server.c ( https://ufile.io/r6578 )
-client.c ( https://ufile.io/l2jna )
Server can connect with multiple clients.
The goal is to extend the client / server application and convert it into a chat room using shared memory.
- Server Process
Expand the function of the server program so that the affiliate processes use shared memory to create a shared circular buffer where the messages will be placed.
Shared memory will be generated by the parent process and will be accessible from affiliate processes.
Each affiliate process expects a message from the customer with a timeout of i (timeout) for 1 second. If it receives a message, it will place it in the buffer. When the end of time occurs, it will send the client all the cached messages that have not yet been sent to the client.
- Client Process
Modify the client code so that it waits either for user input or for a message from the server. When the user enters some text it will be sent to the server. If the server sends a message, it will appear on the client screen. The process can use multiplex (call select) to determine whether a message has been sent from the server or the user.
Note: Server-client communication becomes asynchronous in this way.
The answer should be based to the above programs (sever.c / client.c).
The links are working. Ignore the parenthesis.
Copy-paste the links to your browser. Then wait 5sec and select slow download.
Explanation / Answer
/****************** SERVER CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(welcomeSocket,5)==0)
printf("Listening ");
else
printf("Error ");
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(buffer,"Hello World ");
send(newSocket,buffer,13,0);
return 0;
}
/****************** CLIENT CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
recv(clientSocket, buffer, 1024, 0);
printf("Data received: %s",buffer);
return 0;
}