You are here

TutorialLibraryTest ApplicationDownload
[Tutorial]LibraryTest ApplicationDownload

Socket Tutorial

Chapter 1 - Before you start

What is a socket ?

A socket is simply an interface between a PC application to another PC application on a network. This interface allows you to send and receive data, and also specifies the protocol to rely on for the communication. Protocols can be various, as the type of network on which they apply. However, these protocols are standardized and are integrated into the operating system. As already mentioned, we will focus on the TCP/IP protocol, which we will detail some characteristics.

What is the TCP/IP protocol ?

The term IP stands for Internet Protocol and specifies how 2 computers on the same network (itself made of several sub heterogeneous networks) are able to communicate. This protocol takes care of routing, that is the algorithm in charge of building a path (a sequence of machines) along which data from computer A and B will be transited. Each machine on the network is uniquely identified by an IP address (e.g. 192.168.0.15).

The term TCP stands for Transmission Control Protocol, and is often associated with IP. This protocol specifies how two machines can exchange reliably (i.e. without loss or mixing of data) information through a network that is not reliable. This notion of reliability is important because the IP protocol does not take care whether a packet finally reach its destination or not, or if the reception order of multiple packets from the same message is the same that the issue order.

What is a port number ?

A computer can run multiple applications in parallel, and each one may legitimately need to exchange data over the network. A port number is therefore needed to differentiate multiple lines of communication reaching the same machine. Then, various applications that run on a computer will not mix their data between them. So, when you need to connect to another computer, you must not only know the address of the machine you want to reach, but also the port number of the target application. Some port numbers are reserved for certain types of applications (FTP Server: 21, HTTP Server: 80, etc.). The others can be used at the convenience of the user.

Socket initialization in your application

1) The first thing to do when using sockets is to include header files in which related functions are declared.

Under Windows :
The functions are declared in the following header file: "Winsock2.h".

Under Linux :
The following header files need to be included:
"sys/socket.h", "sys/types.h", "netinet/in.h", "arpa/inet.h", "unistd.h", "fcntl.h", "netdb.h"

2) Then, it is necessary to include the library implementing the socket functions in your project:

Under Windows (Visual Studio) : you need to add the following library to your project: ws2_32.lib ; which can be done (among others) by including the following compiler directive at the beginning of your code :

#pragma comment(lib, "ws2_32.lib")

Under Linux : you don't need to link any particular library.

3) The final step is to initialize the socket library. This operation is only needed under Windows and requires to specify the version of the library you want to work with. In our case we take the latest (2.0).

WSADATA WSAData;

WSAStartup(MAKEWORD(2,0), &WSAData);

Calling this function is mandatory for the library to work. Similarly, do not forget to release resources used by the library when your application is terminated:

WSACleanup ();