![]() |
VOOZH | about |
Socket programming is a method of enabling communication between two nodes over a network using sockets.
Example: In this C program, we are exchanging a "hello" message between server and client to demonstrate the client/server model.
Compiling
gcc client.c -o clientgcc server.c -o server
Output
Client:Hello message sentHello from serverServer:Hello from clientHello message sent
Sockets are one of the core components used by the program to access the network to communicate with other processes/nodes over the network. It is simply a combination of an IP address and a port number that acts as an endpoint for Communication.
Example: 192.168.1.1:8080, where the two parts separated by the colon represent the IP address( 192.168.1.1 ) and the port number( 8080 ).
Socket Types:
The client-server model refers to the architecture used in socket programming, where a client and a server to interact with each other to exchange information or services. This architecture allows client to send service requests and the server to process and send response to those service requests.
State Diagram for Server and Client Model
Socket programming in C is a powerful way to handle network communication.
The server is created using the following steps:
This step involves the creation of the socket using the socket() function.
Parameters:
This helps in manipulating options for the socket referred by the file descriptor sockfd. This is completely optional, but it helps in reuse of address and port. Prevents error such as: “address already in use”.
After the creation of the socket, the bind() function binds the socket to the address and port number specified in addr(custom data structure). In the example code, we bind the server to the localhost, hence we use INADDR_ANY to specify the IP address.
Parameters:
In this step the server uses the listen() function that puts the server socket in a passive mode, where it waits for the client to approach the server to make a connection. The backlog, defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED.
Parameters:
In this step the server extracts the first connection request from the queue of pending connections for the listening socket, sockfd, creates a new connected socket using the accept() function, and returns a new file descriptor referring to that socket. At this point, the connection is established between client and server, and they are ready to transfer data.
Parameters:
In this step the server can send or receive data from the client .
Send(): to send data to the client
Parameters:
recv() : to recieve the data from the client.
Parameters:
After the exchange of information is complete, the server closes the socket using the close() function and releases the system resources.
Parameters:
Follow the below steps for creating a client-side process:
This step involves the creation of the socket which is done in the same way as that of server’s socket creation
The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. Server’s address and port is specified in addr.
Parameters
In this step the client can send or receive data from the server which is done using the send() and recv() functions similar to how the server sends/receives data from the client.
Once the exchange of information is complete, the client also needs to close the created socket and releases the system resources using the close() function in the same way as the server does.