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() */
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...
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...