Question: #include / / socket ( ) , bind ( ) , listen ( ) , accept ( ) #include / / exit ( ) ,
#include socket bind listen accept
#include exit rand srand
#include send close read write
#include htons
#include memset
#include printf
#include time
#include pthreadcreate pthreadjoin
#define PORT
#define MAXCLIENTS
#define BUFFERSIZE
struct clientinfo
int fd;
char usernameBUFFERSIZE;
;
void clienthandlervoid arg
struct clientinfo client struct clientinfo arg;
char bufferBUFFERSIZE;
ssizet rcount;
Receive the username from the client
if rcount readclientfd clientusername, sizeofclientusername
perrorread;
closeclientfd;
pthreadexitNULL;
clientusernamercount; Null terminate the username
printfNew client connected: s
clientusername;
Receive messages from client and broadcast
while rcount readclientfd buffer, sizeofbuffer
Prepend username to the message
snprintfbuffer strlenclientusername BUFFERSIZE strlenclientusernames buffer;
Broadcast message to other clients
for int i ; i MAXCLIENTS; i
if clientifd && clientifd clientfd
writeclientsifd buffer, strlenbuffer;
Close the client socket
closeclientfd;
clientfd ; Mark the client slot as available
printfClient disconnected: s
clientusername;
pthreadexitNULL;
struct clientinfo clientsMAXCLIENTS; Array to store client information
int mainvoid
int listenfd clientfd opt, err;
struct sockaddrin addr;
socklent addrlen sizeofaddr;
pthreadt threadsMAXCLIENTS;
int threadcount ;
Seed random generator
srandtimeNULL;
Create a new TCP socket
listenfd socketAFINET, SOCKSTREAM, ;
if listenfd
perrorsocket;
exitEXITFAILURE;
Set socket options to allow quick rebind
opt ;
if setsockoptlistenfd SOLSOCKET, SOREUSEADDR, &opt, sizeofopt
perrorsetsockopt;
exitEXITFAILURE;
Initialize address structure
memset&addr, sizeofaddr;
addr.sinfamily AFINET;
addr.sinport htonsPORT;
addr.sinaddr.saddr INADDRANY;
Bind socket
if bindlistenfdstruct sockaddr &addr, addrlen
perrorbind;
exitEXITFAILURE;
Start listening for incoming connections
if listenlistenfd MAXCLIENTS
perrorlisten;
exitEXITFAILURE;
printfServer listening on port d
PORT;
Initialize client array
for int i ; i MAXCLIENTS; i
clientsifd ; Mark slot as available
Accept incoming connections and handle them in separate threads
while
Accept a client connection
clientfd acceptlistenfdstruct sockaddr &addr, &addrlen;
if clientfd
perroraccept;
continue;
Find an available slot for the client
int slot;
for slot ; slot MAXCLIENTS; slot
if clientsslotfd
clientsslotfd clientfd;
break;
if slot MAXCLIENTS
printfMaximum number of clients reached. No longer accepting connections.
;
closeclientfd;
continue;
Create a thread to handle the client
if pthreadcreate&threadsthreadcount NULL, clienthandler, &clientsslot
perrorpthreadcreate";
closeclientfd;
continue;
Increment thread count
threadcount;
Join all threads
for int i ; i threadcount; i
pthreadjointhreadsi NULL;
Close the listening socket
closelistenfd;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
