00:01
In this question, we have to write a program that is that will be a client -server program.
00:07
So this program we have to write and see with a multi -threaded approach that will contain that will maintain a list of client connection.
00:27
So we have to use a multi -threaded approach.
00:35
We'll see that later in the code what it is.
00:41
And that will maintain a list of client connections that are there and this will be on the server side and broadcasts the arrival of new clients to the existing clients.
01:01
So we have to let us see how we will approach this in the basic compiler.
01:06
So here first thing that we will do is we have to input or we have to give the basic include the necessary header files.
01:20
So we'll first include these necessary header files that are there.
01:28
So as you can see the .htdio .h htdlib .h string .h all these provide functions and structures for network programming and multi -threading.
01:43
So then we have to define some constant values.
01:47
So they are such as value constant values including max clients which will represent the maximum number of clients and buffer size that is there.
02:00
So for that we have to structure to store the information of the client for that we have to give this.
02:12
Now as we can see we have to function the broadcast for client arrival to existing clients.
02:22
So for that the client info that is there this structure is defined to store information about each connected client including the client's port ip address and socket file descriptor in the global variables are declared including the array of client info objects to store the client information that is the client count to keep track of the number of connected clients and a mutex that is the pre -thread mutex .t to synchronize access to the shared data.
03:00
So now we'll include a function now we can function to broadcast new client arrival to existing clients.
03:08
So for that we will use these methods.
03:12
So what is being done here is that the broadcast new client function is defined this function takes the socket file descriptor of a new client as an argument.
03:26
It creates a message indicating the arrival of the new client and broadcasts it to all other connected clients including the new client itself.
03:35
So it uses a mutex that is there to ensure that only one thread can access the client list at a time at a single time.
03:45
So then we have to see a client handler function.
03:49
We have to define a client handler function next for that we will use the following thread.
03:57
So as you can see here the client handler function is defined this function is executed in a separate thread for each connected client.
04:08
So it receives the socket file descriptor of the client as an argument and inside the function it reads the as you can see here it inside the function.
04:20
It will read the client's name from the socket and print it along with the client's port number.
04:26
So then it will call the broadcast a new client function that is there to notify the other clients about the new client's arrival.
04:36
So finally it closes the connection with the client and exits the thread.
04:42
So for that we have to just give an exit thread command to it so that it will imprint the information on the server side.
04:56
So we give this and finally exit to exit the thread as we just mentioned...