 Hi everyone in this video, we will learn about socket programming So I have written this server.c and client.c programs. Let's open them in visual code Alright, so let's have a look at the server code first. So here's the main program First of all we use this socket system call to create a new socket and Its first argument is the domain which specifies that this is an internet socket The second argument specifies what is the type of socket? So we have mentioned so extreme which means that it will be a connection based socket and the last argument Specifies the protocol we have used zero here. So it will automatically decide the protocol type based on the so extreme So this returns a socket file descriptor and we store it in the server file descriptor variable Then we check if it has returned a negative value in that case. There's an error and we exit the program We now bind this Socket file descriptor with an address So in server program first we create a socket and then we bind it with an address So that clients can connect to this socket using that address So we declare a socket address struck and we call it server address We define the family as a finite as we are using the internet sockets Then we define the address As one twenty seven point zero point zero point one So this is the address of the local host because we will run both client and server on the same machine So we will use the local host IP address and then we also need to specify the port number Here we are using 8080 as the port So now that we have specified both the IP address and the port we will bind the socket that we have created with this address, so we'll use the bind system call it takes the socket file descriptor and It also takes in the address struck as the second argument and the size of address struck as the third argument And if it returns negative value, that means there is some error Otherwise the socket is successfully bound to the address mentioned here So after we have assigned the address to this socket Now we can use the listen system call to listen for incoming connection request to this socket This listen system call takes two arguments. First is a socket file descriptor and second is the number of connections Which will be waiting in a queue So this system call also if it returns a negative value, that means there is some error otherwise Now we accept the connections on this socket So we use this accept system call to accept incoming connections. This accept system call will take in the first client request which is waiting in the queue and Connect the socket to that client So this accept system call takes in three arguments. First is the socket file descriptor and second is a client address struct which Initially is empty. We have just declared a structure and we give its address as an argument to accept system call And this will fill in the client details in this struct also it takes in address to this int variable and it will store the size of this struct in the address length variable and this accept system call returns a new socket file descriptor which is specific to this client So if this new socket is less than zero, which means there is some error Otherwise, we print out that we have connected to the client and we take the client address from this struct And we use this INET N2A to convert this to the ASCII format. So after we have successfully connected with the client We will communicate with the client So we define two buffers one is for the client messages and another is for the server messages Then we have a while loop In the while loop first we use the read system call to read the data which the client is sending in the location pointed to by this message from client address and We read in a one zero two four bytes Then we check if the message is empty then we break from this loop Otherwise, we print out the message that we receive from the client and we Re-initialize this message from client to all zeros then we print this sort of prompt so that the user can enter From the server side some message Which then we can send to the client using the send system call. This send system call takes in the connected socket file descriptor The message that we want to send and the size of the message and the last argument represents the flags After sending this message to the client, we reset this message from server variable to all zeros and this loop runs again and again Finally, we close this server file descriptor and return from the program So that is the server.c program Let's have a look at the client.c in the client.c program again we use this socket system call to create a new socket and Here we do not need to bind this socket to any address But we need servers address so that this socket can connect to the server So we define this socket address struct and name it server address and here we add the server's IP address and the port then we use this connect system call to connect this particular client socket with the server which is given by the IP address and the port and Here the third argument is the size of this server address struct and if it returns an active value Which means there is some error. Otherwise, the socket is successfully connected to the server Now here also we define two buffers one for client and another for server and We take an input from the user on the client side and store it in message from client we run a loop until this message from client is not equal to buy and Send this message to the server using the send system call and this send system call takes in the socket file descriptor the message from client and the length of the message and finally if we want to mention any flags Then we reset this message from client to all zeros and then we use the read system call to read the reply Which comes from the server? so we read that reply in the message from server and we print out that we have received this message from the server and We reset this message from server variable and again We take in message from client from the user and this whole thing repeats in a while loop Finally, we close the socket using the close system call So that's the overall client to see program Let's have a look at them side-by-side So what will happen is? The server and client will establish connected sockets and once they have Connected sockets then client will send a message to the server and Server will use this read system call to read that message It will print out that message and then it will send some message from the server side And then the client reads that message and again it sends some other message to the server and that's how they communicate with each other So let's compile and run these programs So I will open a terminal Let's increase the font size and I'll open one more terminal so that we have one terminal for server and another for client All right, so let's go to the desktop and I'll compile server dot C and I'll name the executable as server and also compile client dot C and Name this executable as client Now what I'll do is I'll first run the server and then I'll run the client so that it can establish connections So once I run the server now the server is running and it is listening for incoming connections Now when I run client here it uses that connect system call to connect with the server and we have this connected to client with address 1 to 7.0.0.1 which is just the local host IP address on the server side So let's send some message to the server Hello from client So you can see that it shows up here on the server side that there is a message sent from the client and Let's reply back Hello from server and We receive from the server on this side. So that's how two processes can communicate with each other So here we have used local host IP address but in general if the processes are on different machines then they will have different IP addresses and a Specific port using which the server and client can connect to each other So let's type in by and that will end the connection So that's it for this video. Thanks and have a nice day