top of page
  • Writer's pictureDheeraj Jha

Socket Programming: Where is my server?

Welcome Folks...

If you want to achieve something, start working on that. In the beginning, it may look/feel silly and slow as tortoise speed but that steady tortoise speed will take you to your goal.

Let's not waste our time and start.


The socket is a way to communicate between two processes on the same or different machines.

You can google for more information. There are tons of material. Let me know if you want it in my way. I will try to write simply.

Let's create a server which will open a socket and receive/send message to connected clients.

Create a Server: Server.cpp

We will start in main().

Let's create our socket with a function call socket().

int serverSocket = socket(AF_INET,SOCK_STREAM,0);

socket() function takes 3 arguments.

The first argument specifies a communication domain. This selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>.

AF_INET is the address family if IPV4.

The second argument specifies the communication semantics. It can be

SOCK_STREAM - Provides sequenced, reliable, two-way, connection-based byte streams.

SOCK_DGRAM - Supports datagrams (connectionless, unreliable messages of a fixed maximum length).

The third argument specifies a particular protocol to be used with the socket. Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0.

After creating a socket we need to create an object of sockaddr_in which is defined as a combination of an IP interface address and a 16-bit port number.

sockaddr_in serverSocketAdd;
serverSocketAdd.sin_family= AF_INET;
serverSocketAdd.sin_port= htons(PortNumber);
inet_pton(AF_INET, "", &serverSocketAdd.sin_addr);

Here you provide a portnumber where the socket will be listening for any client message.

Once you populated serverSockAdd next step is to bind your socket with this address.

Hey, wait... What is this htons() and inet_ptons().

Do you know this? Let's check it out...

htons() - Host to Network Short

Many devices store numbers in little-endian format. The htons()function makes sure that numbers are stored in memory in network byte order, which is with the most significant byte first.

inet_pton() - The inet_pton() function converts an Internet address in its standard text format into its numeric binary form.

Now, let's bind our server socket to the port.

bind(serverSocket, (sockaddr*)&serverSocketAdd, sizeof(serverSocketAdd));

Once the bind() is successful, we will tell our socket to start listening.

listen(serverSocket, SOMAXCONN);

The first parameter is a serverSocket here and the second parameter defines how many numbers of client connections are accepted before start denying client connection. I mean to say maximum allowed client connection.

SOMAXCONN – Maximum queue length specified. By default is 128. If you want to increase the number of client connections. You will have to check this. Will see later in another article how to increase the allowed number of connections to more than 128.

Hurry... our socket is now listening on the specified port.

But wait, it is still not accepting connections from clients. To do this will call a function accept().

int clientSocket = accept(serverSocket, (sockaddr*)&clientSocketAdd, &clientSockerLen);

Accepts an incoming connection on a bound socket. The address information from the remote host is written into the clientSocketAdd structure and the actual size of the address structure is written into clientSocketLen. In other words, this accept() function will write the connecting client's address info into the address structure.

Now our socket is ready to accept a connection request from the client.

To receive a message from the client we have to call recv() function.

int SizeOfMsgRecvFromClient = recv(clientSocket, buffer, 1024, 0);

recv() function takes clientSocket as the first parameter and buffer as a second parameter. A buffer contains the message sent from the client. The maximum size of data that can be received is passed in 3rd parameter. 4th parameter specifies the type of message reception. More details

This function returns the number of bytes received or -1 on error.

If all goes well, let's send a message to the client.

int SizeOfMsgSentToClient = send(clientSocket, buffer, SizeOfMsgRecvFromClient + 1, 0);

send() function takes clientSocket as the first parameter and buffer as a second parameter. Buffer will have the message which we want to send to the client. The next parameter is the size of the message. 4th parameter specifies the type of message transmission.

You will notice send() and recv() functions are in an infinite while loop, so that server will be ready and available for receiving messages from clients.

Now at the end close all sockets.


Hey, our server is ready. It’s time to write a client which will connect to this server.

We will see that in the next article.

55 views0 comments
Post: Blog2 Post
bottom of page