Question
This is for a Unix class.
Please help me out.
I am attaching a skeletal code of the program below, it just needs ti be filled in.
Write a chatting program using Message Queue Unix IPC in C/C++ meeting the following requirements: a Two instances of the sam
d) How to chat? . Chatting between two users running from the same server i. Messages should be displayed asynchronously, whi
Output example i. User 1 S mychat -k 5673423-m 5 -r 9-u John John>> Other>> Hi John! JohnHey Mary! John>>Whats up? John>> Ot
i. User 2 S mychat -u Mary -m 9-r5 -k 5673423 Mary>>Hi John! Marv>> Other>> Hey Mary! Marv>> Other>> Whats up? Mary>> Nothin

Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously.

/* *************************************************************
* Utility functions
* ************************************************************** */
static void
usageError(const char * progName, const char *msg)
{

if (msg != NULL)  fprintf(stderr, "%s", msg);
fprintf(stderr, "Usage: %s {-k key | -m my_msg_type | -r rcvr_msg_type | -u user name}\n", progName);
fprintf(stderr, " -k: key \n");
fprintf(stderr, " -m: my message type\n");
fprintf(stderr, " -r: receiver message type\n");
fprintf(stderr, " -u: user name\n");

exit(-1);

}

[Some other utility functions may come here, if any ]
....

// handles SIGINT - quitting the chatting. Sends the other end a 'bye' message
// then remove its own message queue and terminates itself.
static void sig_int_handler(int signo)
{
....
}

main () {

// Set the default values for the user name and message type.
// Must be replaced by the run time arguments
....

// Handling command line options. See usageError() for supported options
while ((opt = getopt(argc, argv, "k:m:r:u:")) != -1)
{
.....
}

// Setting SIGINT signal handler
.....

// Setting the sleep time (0.01 second)
.....

// create a new message queue or open it if already exists.
if((qid = msgget(Key, 0644 | IPC_CREAT)) < 0)
{
...
}

// Create a new process to receive the messages from the queue
// Parent: sending messages
// Child: receiving the messages
pid = fork();
if (pid > 0) { //parent - sender
while(!done)
{

// clean up the message text buffer
......
// read a message to send from the command line
if(fgets(m.mtext,MAXLINE, stdin) == NULL)
{
// if fails, let's end the session. Sending the 'bye' msg to
// the other end }
...
// the user entered a 'bye' message. Let's quit
...
// Otherwise, send the entered message to the other end
...

} /* end of while */

} else if (pid == 0) { //it's the child process (Receiver)

// clean up the message text buffer
....
while(!done) // run until the message queue closes
{

// handling the reception of multiple accumulated messages
while(msgrcv(qid, (void *)&r, sizeof(r.mtext), rcvr_msg_type,
IPC_NOWAIT|MSG_NOERROR)>0)
{
//print the received message
}

// Some new messages have been received. Now prompt the user
// to send some messages
....

// If the other end closed the message queue, let's send a
// signal to the parent process to terminate then this process also exits.
....
// else sleep 0.01 second before receiving next messages
nanosleep(&sleepTime, NULL);

}

} else if (pid < 0) {
fprintf(stderr, "Can't fofk!\n");
exit(2);
}

// done - close its own message queue
msgctl(qid, IPC_RMID, NULL);

// terminates the child process then the process exits
if (getpid() != pid) ..... /* sends a signal to pid (child) to terminate */

exit(0);

} /* the end of main() */


Write a chatting program using Message Queue Unix IPC in C/C++ meeting the following requirements: a Two instances of the same program (mychat) are talking to each other over a UNIX message queue. They are peer-to- peer messaging. b It takes following optional arguments: From usageErrorO Bad option Usage: mychat -k key -m my msg type l -r revr msg type l-u user name k: key -m: my message type -r: receiver message type u:user name o You must use getopt function to handle the command line options. Here are some hints and the default values of options. while ((opt getopt(argc, argv, "k:m:r.u:"))!-1) switch (opt) case 'k': I/ Setting own key overriding your default value { your code ) break case 'm': // Setting my message type (Default 1) t your code) break; case 'r': I/ Setting the receiver's message type (Default: 2) (your code break; case u': I/ Setting the user name. (Default: Me) f your code) break default usageError(argv[0], "Bad optionn") /see above b)
d) How to chat? . Chatting between two users running from the same server i. Messages should be displayed asynchronously, which means each user's message should be displayed immediately without being blocked or delayed. ii. Display the user name or "Other" for the other party before the message entered/received. See below for a sample output. ii. You may consider running two processes for each user, one for sending your own message and the other for receiving messages from the other end e) How to stop the processes? . From the terminal, if a user enters a "bye" or "quit" message, then both processes stop their execution. Or i. If a process receives an interrupt signal (C) from a terminal, then both processes stop their execution. The program should provide the SIGINT signal handler and sends a 'bye' message to the other user. ii.When the messaging session ends, all the processes involved should be terminated and the message queue and its data should be removed by the kernel. (run "Sipcs -q")
Output example i. User 1 S mychat -k 5673423-m 5 -r 9-u John John>> Other>> Hi John! JohnHey Mary! John>>What's up? John>> Other>>Nothing much. A quick question for you... John>> Sure go ahead. John>> Other>> How difficult is it to learn Unix? John>> Other>> Is it hard? John>> Well John>>Depending on John>>bye S ps -u USER PID %)CPU %MEM VSZ RSS TTY STAT START TIME COMMAND namsu 2271 0.0 0.0 159740 2016 pts/7 R+ 22:30 0:00 ps -u namsu 15459 0.0 0.0 124116 2344 pts/7 Ss 08:58 0:00 -tcslh namsu 15706 0.0 0.0 119880 2292 pts/7 S 08:58 0:00 bashh S ipcs -q Message Queues - keymsqid owner used-bytes perms messages
i. User 2 S mychat -u Mary -m 9-r5 -k 5673423 Mary>>Hi John! Marv>> Other>> Hey Mary! Marv>> Other>> What's up? Mary>> Nothing much. A quick question for you.. Mary>> Other>> Sure go ahead Mary>> How difficult is it to learn Unix? Mary>>Is t hard? Mary>> Other>> Well... Mary>> Other>> Depending on. Mary>> bye can't send message
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I've created a server & client script for chatting between two users.

/**************************************************SERVER Side Script*****************************************************************/
#include<stdio.h>

#include<netinet/in.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netdb.h>

#include<stdlib.h>

#include<string.h>

#define MAX 80

# define PORT 43454

# define SA struct sockaddr //Our main socket variable.
void func(int fd) //Socket file descriptor for identify the socket.
{
char message[MAX]; //created array to store the messages that are sent from Client side.
int n;
for (;;) {
    bzero(message, MAX);
    read(fd, message, sizeof(message));
    printf("From client: %s\t To client : ", message);
    bzero(message, MAX);
    n = 0;
    while ((message[n++] = getchar()) != '\n');
    write(fd, message, sizeof(message));
    if (strncmp("exit", message, 4) == 0) {
      printf("Server Exit...\n");
      break;
    }
}
}
int main() {
int fd, connfd, len; //Connection file descriptor that will be used to distinguish client connections.
struct sockaddr_in servaddr, cli;
fd = socket(AF_INET, SOCK_STREAM, 0); //Created new socket to return the identifier of socket into fd.
if (fd == -1) {
    printf("socket creation failed...\n");
    exit(0);
} else
    printf("Socket successfully created..\n");
bzero( & servaddr, sizeof(servaddr)); //Assigns the address specified by servaddr to the socket.
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if ((bind(fd, (SA * ) & servaddr, sizeof(servaddr))) != 0) //Handling errors.
{
    printf("socket bind failed...\n");
    exit(0);
} else
    printf("Socket successfully binded..\n");
if ((listen(fd, 5)) != 0) //Listen for client connections.
{
    printf("Listen failed...\n");
    exit(0);
} else
    printf("Server listening..\n");
len = sizeof(cli);
connfd = accept(fd, (SA * ) & cli, & len);
if (connfd < 0) {
    printf("server acccept failed...\n");
    exit(0);
} else
    printf("server acccept the client...\n"); //Receive message and send to Client
func(connfd);
close(fd);
}

/******************************************************CLIENT Side Script************************************************************/ #include<stdio.h>

#include<netinet/in.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netdb.h>

#include<string.h>

#include<stdlib.h>

#define MAX 80

# define PORT 43454

# define SA struct sockaddr
void func(int fd) {
char message[MAX]; //created array to store the messages that are sent from server side.
int n;
for (;;) {
    bzero(message, sizeof(message));
    printf("Enter the string : ");
    n = 0;
    while ((message[n++] = getchar()) != '\n');
    write(fd, message, sizeof(message));
    bzero(message, sizeof(message));
    read(fd, message, sizeof(message));
    printf("From Server : %s", message);
    if ((strncmp(message, "exit", 4)) == 0) {
      printf("Client Exit...\n");
      break;
    }
}
}

int main() {
int fd, connfd;
struct sockaddr_in servaddr, cli;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
    printf("socket creation failed...\n");
    exit(0);
} else
    printf("Socket successfully created..\n");
bzero( & servaddr, sizeof(servaddr)); //Connecting client to the server.
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Binding client to localhost.      
servaddr.sin_port = htons(PORT);
if (connect(fd, (SA * ) & servaddr, sizeof(servaddr)) != 0) {
    printf("connection with the server failed...\n");
    exit(0);
} else
    printf("connected to the server..\n"); //receive message and send to Server.
func(fd);
close(fd);
}

SERVER SIDE OUTPUT

$ cc Server.c

$ ./a.out

Socket successfully created..

Socket successfully binded..

Server listening..

server acccept the client...

From client: Hi there.

To client : Hello I'm BATMAN.

From client: exit

To client : exit

Server Exit...

CLIENT SIDE OUTPUT

$ cc Client.c

$ ./a.out

Socket successfully created..

connected to the server..

Enter the string : Hi there.

From Server : Hello I'm BATMAN.

Enter the string : exit

From Server : exit

Client Exit...

Add a comment
Know the answer?
Add Answer to:
This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT