DEV Community

Noah11012
Noah11012

Posted on • Updated on

Socket Programming in C: What is a Socket?

This is the post that marks the beginning of this series. A journey, when arrived at the end, will give you control over sockets. No longer will they be disorderly or out of your reach because you will be the one to have dominion over them. Bending their will to yours and serving your every whim.

People dream to have this kind of power and you can be the one to achieve it only if you read this series to its conclusion.

Of course, you can still get this kind of power from other great tutorials out on the internet but it would be appreciated if you stuck till the very end.

Let us begin this journey!

What is a socket? Simply put, a socket is an endpoint for where the data will be sent to and received. Sockets are used as a generalized means of communication between different processes on the same computer or one on the same network. Because everything in a Unix based OSes is a file or can be represented as one, processes identify sockets by their file descriptor.

Sockets come in different flavors. Each flavor specifies the guarantees and leniency that is given. From now on this will be known as communication styles. Next, what protocol is used? This is where the low-level details and mechanisms tell how the data is transmitted and received.

Communication styles can be thought of as a series of questions that are given a different answer depending on the socket's communication style.

  • Units used?

Some styles regard data as simply bytes with no structure whatsoever. Others think data as being grouped into a structure determined by the style.

  • Loss during transmission?

Will the occasional drop of a packet matter overall? Will the packets received on the other end be in the same order as it was sent? Some styles guarantee that all packets transmitted will be retrieved in the same order and that if a packet is dropped, it is resent. Other styles of communication may allow some packets to drop. Packets may also arrive more than once and out of order. Programmers who use such sockets must place safeguards in their code to accommodate for the unreliability of this communication style.

  • Partner?

The traditional way of thinking a socket is similar to the client-server relationship, where both parties can freely exchange data with each other. There is also a one-sided version: stamp the address and send the data on its way.

How do we set the endpoint for a socket? Each socket is paired with a socket address, or in other words, the IP address and a port number. Setting a socket's address is called binding.

Next

For the next article, we will discuss the different kinds of communication styles that we can use.

Top comments (0)