Sockets in C – an Introduction

The story of Internet

The comic says it all.

Le expertly made ragecomic

internet troll

Internet has transformed the human civilization. It has made computers more powerful and wide-reaching than ever. So much so that we have computers made for just the internet like “Netbooks” or a “Chromebook”

Sockets, what are they ?

Sockets are the primary way to communicate between two computers/devices. They can be though of as a long invisible pipe between 2 computers through which all of their messages are exchanged. Needless to say, to communicate , both computers need to have a socket opened.

Linux has 3 types of sockets

SOCK_STREAM

Corresponds to a TCP connection. Basically we get sure shot message delivery , without errors and we have to establish a connection before we can communicate. Incase some error occurs ( like connection is lost or if one device can’t find the other ), devices are notified with errors. The main idea is, if we send something without getting an error , we can be reasonably sure that out message has reached. The whole process is similar to making a phone call.It’s good for exchanging data like email,text or programs.

SOCK_DGRAM

Corresponds to a UDP connection. In this case nothing is sure . After sending a message the best we can do is pray that the message reaches, the socket does nothing to ensure a sure shot delivery.Even if it does, data might be out-of-order or incorrect. Although we might discover sometimes that most messages do make it, nobody guarantees that. For eg, a router can start discarding packets if it senses congestion, and the sending party won’t make an attempt to send it back.It’s like a couple having a break-up through Postal Service. Messages may not reach, moreover nobody cares 😛 . Good for large data, like audio/video. A video may contain millions of pixels, nobody would mind a few misplaced ones, our eyes would hardly notice

SOCK_SEQPACKET

Similar to SOCK_STREAM , but the only major difference is the message size is preserved. If a SOCK_STREAM socket gets 2 messaged of 10 and 15 bytes, when the receiver tries to read it, he will get 25 bytes together. In case of SOCK_SEQPACKET the receiver will get 10 bytes the first time he reads and even though 15 bytes have arrived, they will only be available to him after he has read the first 10 bytes. In simple terms , message lengths are preserved.

IP Address and Port Numbers

Since Computers don’t speak any human language they never call each other as how we know them. The Internet Protocol ( IP ) requires each computer on the planet ( or wherever ) to have a unique address known as the IP address ( eg 234.15.67.211 ).

One computer can be connected to many other computers. To give an example , you might be using Facebook , chatting on Skype and playing DOTA at the same time. In addition to an IP Address, a connection is identified by a port number .

Don’t believe me ?

Try connecting to Port 80 on Computer 173.194.38.146

173.194.38.146:80
 

To get started with coding ,go to my post here

.

Advertisements

One thought on “Sockets in C – an Introduction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s