What is a socket?

The word socket is used in a variety of different contexts, from hip sockets to socket wrenches to electrical sockets.  The common theme in each of these cases is that that the socket provides an interface to connect two items.  The hip socket connects the femur to the pelvis; the socket on a socket wrench connects the bolt to the wrench; an electrical socket connects the electric in the wall to the plug and ultimately to the lamp.   

A network socket provides a similar interface between two programs or devices.  In this case, the interface is used to transmit data between the two programs rather than transmitting electricity or mechanical force. 

IBM gives a good analogy to understand how socket communication works.  When making a phone call, one phone dials a number, and when the call is answered, a connection is made between the two phones that stays open until one of the phones breaks the connection.  In this case, there are two different roles being played between the two phones: the first phone requested a connection by dialing the number, and the other listened for the connection request.

Similarly, there are two sides to a network socket connection: the server (listening) socket and the client (dialing) socket. The server socket, once created, is bound to a specific port number and listens for incoming connections.  The client socket calls the IP address and port number of the server socket, and a connection is made.  The client and server may exchange information until the connection is closed.

Sockets can be created on many different devices using many different programming languages.  Most languages have APIs specifically built for socket communication, and PLC brands have methods of socket creation built into their programs. 

Server Socket Creation Example

Our p1ge0n software acts as a server socket, listening for messages from the PLCs or robots. Creation of a server socket generally involves the following steps:

  1. Create the socket instance. This includes designating:
    • IP Address type
    • Socket type
  2. Bind the socket to the IP address and Port number
  3. Listen for incoming connections from a Client Socket
  4. When called, accept the connection from the Client Socket
  5. Receive data form Client Socket
  6. Send/receive any replies to and from the Client Socket
  7. Close out/shut down the socket when it no longer needs to listen for connections

Here is an example python script running a Server Socket:

Python Socket Server Example

Client Socket creation example

Once a server socket is established, a client socket can be created on the device from which we are sending the message. This is much more temporary than a server socket, as it opens only when it needs to send information rather than remaining open to listen for incoming messages. Creating the Client Socket generally involves the following steps:

  1. Create the socket instance. This includes designating:
    • IP Address type
    • Socket type
  2. Connect to the server socket using the IP address and port number
  3. Send data to the server socket
  4. Receive reply from the server socket (in the case of p1ge0n, the message will be echoed back to the client socket)
  5. Close socket connection

Here is an example python script running a Client Socket:

Python Socket Client Example

The steps are similar on other programs and programming languages.  Some PLC software keeps this broken down into steps, and some have combined them so that the user only enters the IP address, port number, and data to send.  Please contact us for support in setting this up to work with your device. 

References

https://www.ibm.com/docs/en/zos/2.1.0?topic=services-what-is-socket

https://www.ibm.com/docs/en/i/7.1?topic=programming-how-sockets-work

https://upskilld.com/learn/network-sockets-and-ports/#:~:text=The%20difference%20between%20a%20socket,implementations%20that%20enable%20network%20connections