Consider the following programs: -server.c (https://ufile.io/r6578) -client.c (h
ID: 3851169 • 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.
Explanation / Answer
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <string>
#include <sys/wait.h>
using namespace std;
enum e {Server,Client};
struct chat
{ bool ifread;
e who;
char message[50];
int msgl;
};
int main(void)
{
string temp;
int shmid;
key_t key=33;
if ((shmid = shmget(key, sizeof(chat), IPC_CREAT | 0666)) < 0)
{
cout<<"shmget"<<endl;
return(1);
}
chat *str ;
str = (chat *)shmat(shmid, NULL, 0);
pid_t pid;
pid=fork();
str->ifread==true;
str->who=Server;
if(pid==0)
{
while(temp!="bye")
{
if(str->ifread==false && str->who==Client)
{
for(int i=0;i<str->msgl;i++)
cout<<str->message[i];
str->ifread==true;
}
}
}
else if (pid>0)
{
while(temp!="bye")
{
getline(cin,temp);
str->msgl=temp.length();
if(str->ifread)
{
str->who=Server;
for(int i=0;i<str->msgl;i++)
{
str->message[i]=temp.at(i);
}
str->who=Server;
str->ifread==false;
}
else if (!str->ifread && str->who==Client)
{
sleep(1);
waitpid(pid,NULL,0);
}
}
}
shmctl (shmid, IPC_RMID, NULL);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <string>
#include <sys/wait.h>
using namespace std;
enum e {Server,Client};
struct chat
{
bool ifread;
e who;
char message[50];
int msgl;
};
int main(void)
{
string temp;
int shmid;
key_t key=433;
if ((shmid = s`hmget(key, sizeof(chat), 0666)) < 0)
{
cout<<"shmget"<<endl;
return(1);
}
chat *str ;
str = (chat *)shmat(shmid, NULL, 0);
pid_t pid;
pid=fork();
if(pid==0)
{
while(temp!="bye")
{
if(str->ifread==false && str->who==Server)
{
for(int i=0;i<str->msgl;i++)
cout<<str->message[i];
str->ifread==true;
}
}
}
else if (pid>0)
{
while(temp!="bye")
{
getline(cin,temp);
str->msgl=temp.length();
if(str->ifread)
{
str->who=Client;
for(int i=0;i<str->msgl;i++)
{
str->message[i]=temp.at(i);
}
str->ifread=false;
}
else if (!str->ifread && str->who==Server)
{
sleep(1);
}
}
}
shmctl (shmid, IPC_RMID, NULL);
}