How To: Create a Basic Client-Server Connection in C Part-1

Create a Basic Client-Server Connection in C Part-1

How to Create a Basic Client-Server Connection in C Part-1

Hello, I'm a new guy around here and since I'm not seeing much tutorials in C i thought of contributing with something relatively simple. This will be a short series.

This tutorial is for those who already know how to program in C and have a basic understanding how networks work...but i will be explaining it step-by-step nevertheless.

This series is for use in Linux or other Unix-like systems.

In this part i will not be presenting any code because first i will need to talk about some key concepts. In the second part we will begin to build our basic server in C and in a third or fourth part the client.

Network Socket

So what is a network-socket? Network-Socket is the endpoint of the client-server communication or vice-versa. Basically is what we use to send data to a server or a client or in other words is a two-way communication link between two programs. The socket is bound to a port so the TCP layer can determine the application that data is destined to.

A server, generally waits for connections from a client. They do that listening to a socket.

When a client makes a connection request (knowing the hostname and the port on which the server is listening) he needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is assigned by the system.

If everything goes well the server accepts his connection and creates a new socket to the same port. Also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the client.

If the connection is accepted, on the client side a socket is created so he can communicate with the server.

Therefore both client and server can comunicate reading or writing to their sockets.

Types of Sockets

Stream Sockets: This one uses the TCP protocol to communicate. If we want to send a message with this socket, the said message will be sent to the server one character of the message at a time. This is what we will use in this series.

Datagram Sockets:This socket uses the UDP protocol to communicate and sends the entire message at once to the server.


A descriptor is a low-level integer to identify an opened socket or other input/output sources (files,etc) at kernel level in Linux and other Unix-like systems. This descriptors are held in the structure of a process.


If we open a file, the OS will create an entry in a table to represent that file and store is content. This entry are represented by integers (1,2,3,4,...). This integer is the descriptor, in this case, the file descriptor. In sockets is the same thing. When we open a socket there is a entry that is created, the content is stored and we have a socket descriptor.

Ending Part 1

For now that's all folks. Hope you enjoyed my first part of this series, it's my first time writing something like this so if i made a mistake somewhere feel free to correct me, it will be much appreciated.


Thanks for the tutorial,
It's a small step but i'll watch over new content!

great ! i was looking here and there on internet about the same thing.. how server client works and its coding too.. keep going..

Ya write more about C.. its practical applications ..

Don't worry! I'm going to start a few projects with C in the future!

~ Jinx

I'm planning to do so Akash, I'm just trying to fit the projects i have thought for null byte in my schedule but i will do more for sure.

Share Your Thoughts

  • Hot
  • Latest