Sockets

Amilcar


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.

 

Sockets programming
A socket is connected by configuring the following:

  1. Host: is an address only within the network where is located the process to connect.
  2. 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.
  3. 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.
  4. 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

Server

A socket that serves as server operate in the following way:

  1. 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.
  2. 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.
  3. When is the communication with the client socket completed. the connection is closed.
  4. The server socket still waiting for another client socket connections.

Generally the waiting is performed within a cycle infinite.

 

Customer

 

A socket client function of the following form:

  1. The process creates a socket client configuring the address IP and the port where the socket server expected by connections.
  2. The client socket enters a communication with the server socket.
  3. 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.

 

amilcar01

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.

amilcar02

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):

amilcar03

In this link you can download the sockets in C language programming examples # and Java:

http://tinyurl.com/jfnnh55 

Sockets

2 thoughts on “Sockets

  • October 16, 2017 at 11:59 am
    Permalink

    My spouse and I love your blog and find almost all of your post’s to be just what I’m looking for. can you offer guest writers to write content for you? I wouldn’t mind producing a post or elaborating on some the subjects you write concerning here. Again, awesome weblog!

    Reply
  • November 9, 2017 at 8:21 pm
    Permalink

    Our way of telling the whole thing in this post is actually pleasant, every one be capable of without difficulty understand it, Thanks a lot.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *