A socket is a concept of programming implemented within a system operating (OS), usually supported by a language of programming of high level (C#, Java, Python etc.),
that allows the communication between different processes. We understand by communication between processes on the task of exchanging information between different applications running within a single computer or on different computers.
A socket uses TCP (transmission control protocol) Protocol to carry out the connection and communication between its terminals; A terminal is the point end in the communication where it is located the process to connect.
The sockets are designed under an architecture known as client / server.
What is the importance of the sockets?
Currently many applications are implemented using sockets internally. For example: the managers of databases relational, chats, tools for the control of versions, services in the cloud, e even them pages that request in the internet is sent and receive in them browser through sockets.
Programming sockets have great importance due to its widespread use. Through them and in conjunction with a design pattern we can implement a robust and extensible web service as it is the case of a Broker.
To simplify this reading, we will make reference to a process such as an application or program that will be executed on a computer.
Differences between Sockets and pipes
A pipe is basically a space of shared memory between processes which can be accessed orderly within a same computer. Them sockets to difference of them pipes (Pipes) transmit information without import if them processes in which is housed is found or not within the same computer.
A socket is connected by configuring the following:
- Host: is an address only within the network where is located the process to connect.
- Number of port: number that identifies to the application within a computer connected to the network. Two sockets may not send or receive information through the same port at the same time within the same OS, without being one client and the other server.
- Flow of input and output: via these flows are can send and receive information as a sequence of bytes or characters once the socket is connected and has been open.
- Blocking / not blocking: the sockets by default are blocking, i.e. block the process where is are hosted until the information is sent or received by full. In many occasions is possible configure this feature, however this not guarantee that the information is sent or received completely. For example if a socket is configured to not be blocking and attempts to send many bytes of information. Socket writes as many bytes as possible without waiting for that information is transmitted to the output stream and immediately returns control to the process that I invoke it.
Process for communication between sockets
A socket that serves as server operate in the following way:
- The process creates a server socket to listen on a specific port and awaits incoming connections from any client hosted on some other process socket.
- Once a connection has been established is create a socket client that engage in the communication with the socket that request the connection in the other process.
- When is the communication with the client socket completed. the connection is closed.
- The server socket still waiting for another client socket connections.
Generally the waiting is performed within a cycle infinite.
A socket client function of the following form:
- The process creates a socket client configuring the address IP and the port where the socket server expected by connections.
- The client socket enters a communication with the server socket.
- Once the communication has terminated the socket is closed.
Example of programming
As an example for this reading, we will create 2 sockets. The first will be as following the steps described above and the second server will act as a client. The chosen programming language is C#. Other 2 sockets will be programmed in JAVA to illustrate the sockets programming as a concept.
The details of the implementation of the socket them can review in them codes attached to this reading.
The code of the server socket Description:
We then describe the steps taken in the code for the communication with a socket from the server.
Line 61: is creates an object of the type T:System.NET.Sockets.TcpListener which accepts connections to engage in communication with some other socket customer. This object serves as a server socket.
Line 62: start the server using the method Start() to wait by connections inbound.
Line 68: creates an infinite loop so that the server will wait for incoming connections.
Line 71: the server can receive connections of socket in another process using the AcceptSocket() method.
Line 72: receive a socket with which communication is filed. Note that for each connection incoming is create a socket for the communication.
Line 73: the received message is sent back to the socket to start the connection making the message received in capitals.
Online: 74: it closes finally it connection with the socket client.
The client socket code description:
We then describe the steps taken in the code for the communication with the server socket from the client.
Line 53: is creates a socket client and is open the connection to engage the connection with the socket server.
Line 54: the "Hello World" message is sent to the server using the created socket.
Line 55: the response received from the server is received and stored in a string of characters. In this case the string will contain the same sentence sent to the server only as uppercase.
Line 56: finally closes the connection with the server.
He result of run both programs would be the following (remember run first the server and then the client):
In this link you can download the sockets in C language programming examples # and Java: