*You can write this in either C, C++, OR JAVA* Build a client and server that pr
ID: 3594354 • Letter: #
Question
*You can write this in either C, C++, OR JAVA*
Build a client and server that provide a distributed chat service. Users (clients) should be allowed to connect to your server and specify a nickname to be used as an alias. Duplicates are not allowed. After connecting, all messages sent by any user should be broadcast to all other users currently connected. In addition, the server should inform all clients whenever a new user logs in. For example, suppose that "Anne", "Beth", and "Claire" are currently logged in. If Anne enters "Hi Beth", then the server should broadcast "Anne: Hi Beth" to all other users, namely Beth and Claire. If another user, say "Dan", joins the chat session, then the server should inform Anne, Beth, and Claire by broadcasting the message "Dan has joined the chat". The first user to login should receive the message "You are the first user to join the chat". The chat service should be implemented using sockets and TCP. Don't worry about implementing client or server shutdown.
Explanation / Answer
Source File:
client.c
-----------------------------------
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int clientSocket;
char response_buffer[100];
char request_message[]="Hi please give Terminal Details!";
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*Create The Socket Connection*/
if(!(clientSocket = socket(PF_INET, SOCK_STREAM, 1)))
{
printf("Failed to create a Scoket: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
/*Connecting To The Server*/
if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
{
printf("Connect Error: ");
}
/*Send Request Communication Message to The Server*/
send(clientSocket,request_message,sizeof(request_message),0);
printf("Request Successfully sent to the server: ");
recv(clientSocket, response_buffer, sizeof(response_buffer), 0);
printf("Received Response from the Server is: %s ",response_buffer);
return 0;
}
Server.c
---------------------------------------------------
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int Server_Socket, newSocket;
char response_buffer[500];
char request_buffer[1000];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
/*creating The Socket Connection*/
socklen_t addr_size;
if(!(Server_Socket = socket(PF_INET, SOCK_STREAM, 0))<0)
{
printf("Socket Error: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
/*Bind The IpAddress */
bind(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*Listening at Particular Port NO*/
if(listen(Server_Socket,5)==0)
{
printf("Listening ");
}
else
{
printf("Error ");
}
addr_size = sizeof serverStorage;
/*Accepting Socket Connection*/
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(response_buffer," Hello Welcome Please wait!: ");
/*Receives Request from The Client*/
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" Received Request from the Client is %s ",request_buffer);
/*Send Response Back to The Client*/
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
return 0;
}
First We Need To Run The Server
./server.c
Additionally , if you want to Get Details From The Database via The Server
Here I Provided Proc Sample Code To Connect With Database And Give Response Back To Client
Sample.pc
------------------------------------------
#include<stdio.h>
#include<strings.h>
#include<string.h>
#include<sqlca.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
struct POS_Download
{
char Terminal_name[50];
char Terminal_id[50];
char Merchant_id[50];
};
struct POS_Download obj[100];
int count;
int Server_Socket, newSocket;
char response_buffer[500];
void main()
{
// int Server_Socket, newSocket;
// char response_buffer[500];
char request_buffer[1000];
int i=0;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
if(!(Server_Socket = socket(PF_INET, SOCK_STREAM, 0))<0)
{
printf("Socket Connection Error: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
bind(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(Server_Socket,5)==0)
{
printf("Listening ");
}
else
{
printf("Error ");
}
addr_size = sizeof serverStorage;
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
memset(response_buffer,0,sizeof(response_buffer));
//strcpy(response_buffer," Hello Welcome Please wait!: ");
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" **Received Request** %s ",request_buffer);
//Database Connection Statements
/* Connection Parameters*/
char *usrname="azizi_test@orcl194";
char *pwd="azizi_test";
/*Declaration Section */
EXEC SQL BEGIN DECLARE SECTION;
char hostvar1[50];
char hostvar2[50];
EXEC SQL END DECLARE SECTION;
/* Connection Steps to Connect with Data base */
EXEC SQL CONNECT :usrname IDENTIFIED BY :pwd;
if(sqlca.sqlcode==0)
{
printf(" *** Successfully Connected to the Database *** ");
}
else
{
printf(" Unable to CONNECTED to the Database ");
strcpy(response_buffer," Database Error! ");
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
}
/* Display Records from The Database */
EXEC SQL SELECT count(*) into :count from POS_Connection;
if(sqlca.sqlcode!=0)
printf(" *** sql Error [%s] ",sqlca.sqlcode);
else if (count == 0)
{
printf(" *** Table is Empty No Records Available **** ");
strcpy(response_buffer," Table have No Record! ");
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
}
else
{
display(count);
}
}
void display(int count)
{
system("clear");
int i=0;
/* Declaration Section */
EXEC SQL BEGIN DECLARE SECTION;
varchar Terminal_id[50];
varchar Terminal_name[50];
varchar Merchant_id[50];
EXEC SQL END DECLARE SECTION;
printf(" The Total Number of records in These Table is : %d ",count);
/* Fetching Details from The Database*/
EXEC SQL DECLARE ven cursor for select Terminal_id,Terminal_name,Merchant_id from POS_Connection;
if(sqlca.sqlcode==0)
{
EXEC SQL OPEN ven;
if(sqlca.sqlcode==0)
{
// printf(" Terminal_Name Terminal_id Merchant_id");
while(1)
{
memset(Terminal_id.arr,0x00,sizeof(Terminal_id.arr));
memset(Terminal_name.arr,0x00,sizeof(Terminal_name.arr));
memset(Merchant_id.arr,0x00,sizeof(Merchant_id.arr));
EXEC SQL FETCH ven into :Terminal_id,:Terminal_name,:Merchant_id;
if(sqlca.sqlcode==0 && sqlca.sqlcode!=1403)
{
// printf(" %s %s %s ",Terminal_id.arr,Terminal_name.arr,Merchant_id.arr);
strcpy( obj[i].Terminal_id,Terminal_id.arr);
strcpy( obj[i].Terminal_name,Terminal_name.arr);
strcpy( obj[i].Merchant_id,Merchant_id.arr);
i++;
}
else if(sqlca.sqlcode==1403)
{
printf(" **** All Records Displayed **** ");
EXEC SQL CLOSE ven;
strcpy(response_buffer,obj[0].Terminal_id);
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
if(sqlca.sqlcode!=0)
{
printf(" Error in ven close [%d] ",sqlca.sqlcode);
break;
}
break;
}
else
printf(" Error in fetch Data [%d] ",sqlca.sqlcode);
}
}
}
else
{
printf(" **** Error in cursor **** ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.