Communication is one of the features provided and managed by operating system. T
ID: 3705308 • Letter: C
Question
Communication is one of the features provided and managed by operating system. To let two nodes in a network communicate each other, we can try socket programming. "One socket (node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection." http://www.binarytides.com/socket-programming-c-linux tutorial/ Nowadays, there are a series of software and apps allowing you to chat with your friends. In future, you may also become one of the engineers developing such software in the company. But before that, please implement a simple version of chat room in C in Linux. Suggested features: . Client can log in to the system by entering a name. .Ask server to display a list of available chat room and then client can select a room. . Client can also create a room. e Server delete the chat room when no users in it . Client can send message to all the users in the same chat room. Hint: The sever side may need to maintain two files. One file is for the chat room. The other file is for the online users. Chat room file stores the names of the chat rooms and messages sent among the users in the same rooms. Online user file stores the room the user is staying at and the remained time the user can stay in the chat room Note: 1) The online user names and chat rooms cannot be duplicated. 2) The server can monitor how long the user has been connected to the server 3) If the user has been on line longer than the maximum time set in the server, the user would be automatically disconnected and removed from the on-line user information file 4) You can more credits if you add more features 5) You may need to use threads to implement it. 5) If you are unclear about the subject of this topic, please feel free to contact your instructorExplanation / Answer
SERVER
#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
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("From client: %s To client : ",buff);
bzero(buff,MAX);
n=0;
while((buff[n++]=getchar())!=' ');
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{
printf("Server Exit... ");
break;
}
}
}
int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed... ");
exit(0);
}
else
printf("Socket successfully created.. ");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr, sizeof(servaddr)))!=0)
{
printf("socket bind failed... ");
exit(0);
}
else
printf("Socket successfully binded.. ");
if((listen(sockfd,5))!=0)
{
printf("Listen failed... ");
exit(0);
}
else
printf("Server listening.. ");
len=sizeof(cli);
connfd=accept(sockfd,(SA *)&cli,&len);
if(connfd<0)
{
printf("server acccept failed... ");
exit(0);
}
else
printf("server acccept the client... ");
func(connfd);
close(sockfd);
}
CLIENT
#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 sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("Enter the string : ");
n=0;
while((buff[n++]=getchar())!=' ');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("From Server : %s",buff);
if((strncmp(buff,"exit",4))==0)
{
printf("Client Exit... ");
break;
}
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed... ");
exit(0);
}
else
printf("Socket successfully created.. ");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with the server failed... ");
exit(0);
}
else
printf("connected to the server.. ");
func(sockfd);
close(sockfd);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.