Socket programming in c using TCP/IP

socket programming in c

Before you start learning socket programming in c, you should basic knowledge of IP address, TCP, UDP. In this article, I shall describe TCP/IP and write a socket program using the TCP/IP API.

TCP (Transmission control protocol)

A TCP (transmission control protocol) is a connection-oriented communication. It is an intermediate layer of the application layer and internet protocol layer in OSI model. TCP is designed to send the data packets over the network. It ensures that data is delivered to the correct destination.

TCP creates a connection between the source and destination node before transmitting the data and keep the connection alive until the communication is active.

In TCP before sending the data it breaks the large data into smaller packets and cares the integrity of the data at the time of reassembling at the destination node. Major Internet applications such as the World Wide Web, email, remote administration, and file transfer rely on TCP.

TCP also offers the facility of retransmission, when a TCP client sends data to the server, it requires an acknowledgment in return. If an acknowledgment is not received, after a certain amount of time transmitted data will be the loss and TCP automatically retransmits the data.

The communication over the network in TCP/IP model takes place in form of a client-server architecture. ie, the client begins the communication and establish a connection with a server.

For more understanding lets create a server which continuously runs and establish the connection after getting a request from the client.

Note: here I am creating the server and client for Linux.

In this example, After the connection with a client, the server will wait for a message from the client. After getting the message server will check the received message and send a proper response as per the received message.

Sequence of function for client-server communication

 

Steps to create a client using TCP/IP  API

  • Create a socket using the socket() function in c.
  • Initialize the socket address structure  as per the server and connect the socket to the address of the server using the connect();
  • Receive and send the data using the recv() and send() functions.
    Close the connection by calling the close() function.

Steps to create a server using TCP/IP API

  • Create a socket using the socket() function in c.
  • Initialize the socket address structure and bind the socket to an address using the bind() function.
  • Listen for connections with the listen() function.
  • Accept a connection with the accept() function system call. This call typically blocks until a client connects to the server.
  • Receive and send data by using the recv() and send() function in c.
  • Close the connection by using the close() function.

If you want to learn more about the TCP/IP, here 10 Free days (up to 200 minutes) TCP/IP video course for you.

Your free trial is waiting.

 

Example Source code for TCP/IP client




Example Source code for TCP/IP server

OutPut 1.

OutPut 2.


8 comments

  1. You should not be using gets() as it can lead to buffer overflow (unsafe/security exploit). C11 has deprecated this API and replaced it with fgets().

  2. I am running socket server and client as a web browser.using local host(127.0.0.1) able to connect the server. unable to connect the server using with different local host ip(Ex:192.168.X.X).

Leave a Reply