Saturday, August 9, 2014

Network Sockets

It seems as though network sockets has generated considerable confusion in the computer world. I feel that that confusion largely exists because the need to directly utilize sockets has pretty much faded. Until recently, I've only heard sockets being used as a networking buzzword and whether or not people actually knew what they were wasn't any of my business. With the arrival of HTML5's WebSockets, however, much more consideration is being given to the word. In this article, I want to first distinguish "sockets" from "WebSockets". Then I will do a little socket programming.

HTML5's WebSockets came about as a result of the interactive web. Several years back the internet was all about serving up static web pages usually filled with information about some topic. Many websites served up a bunch of heavily linked pages often stored in a directory structure that separated them into some understandable hierarchy as determined by the authors. This resulted in us end users seeing rather long paths in the URL sometimes with a lot of slashes. Soon enough those static pages learned how to be a little more interactive with the help of AJAX. Websites turned into web applications. Every status update, blog post, comment, etc. you created or saw was a request sent to the server followed by a reaction on the web page easily carried out by JavaScript.

However, it was the case that much of the "real-time" data we wanted to pass around the interwebs was rather small: a comment like "lol", a few pieces of weather data, etc.  The meaningful data on a request had a sizable amount text packaged with it in the form of headers that were sometimes much larger than the data itself.  For sending small real-time data back and forth, carrying tons of header info is unnecessarily cumbersome. Additionally, using this method, data that needs to arrive sequentially such as a vehicle's GPS coordinates isn't guaranteed to arrive in the right order. A much more sensible solution would take advantage of the TCP from the suite of protocols known as IP. TCP is ordered and error checked. WebSockets is the protocol and API that resulted from this realization.

Finally arriving at the subject of this article, a socket is simply an endpoint in network communication. The Linux man pages describes the system implementation of sockets in C. The socket method signature is simply:

int socket(int domain, int type, int protocol);

The domain specifies what protocol family is used in communication. In type, we specify whether we want to use SOCK_STREAM (tcp), SOCK_DGRAM (udp), or SOCK_RAW (raw), among others.

Because a socket is an endpoint for network communication, it can be essentially defined very simply as an address and a port. Their usefulness beyond this depends on how you want to send the packets of information. The typical choice is TCP because the packets are numbered and can be reassembled at the recipient's endpoint. For most use cases, people want "real-time" data to arrive in the same order it was sent.

For more information on Linux socket implementation you can visit the man page at http://man7.org/linux/man-pages/man2/socket.2.html

I want to quickly move on to demonstrating socket programming. The following very simple use case for socket programming in PHP should get users started with PHP's scoket functions.

A port scanner application in PHP




The above image is a quick program I wrote in PHP that accomplishes port scanning using PHP socket functions.

First, we create a socket object by specifying that we want to use IPv4 (AF_INET), TCP (SOCK_STREAM), and IPPROTO_IP which is integer value 0. We allows the user to specify the $hostname by the system's command line arguments. It should come in as $argv[1] because $argv[0] is the filename of the script itself. We use PHP's gethostbyname() function to grab the IP address of our host. Then we iterate through all of the potential ports (I know, some of them are not used with TCP) and check whether or not we can create a connection within the specified timeout limit (0.2 seconds). If there are any successful connections as we are printing ports we are currently scanning we put a star next to them. Finally, we print out a list of all the ports we could successfully connect to along with the size of the array.

A similar program can be written in Python (the reader is encouraged to try writing this one). Python includes a socket module that pretty closely mimics the C implementation found in Unix. It likewise creates socket objects based on domain, type, and protocol. It has the same functions that you may have discovered in the Linux man pages such as bind(), accept(), connect(), listen() and close().

Because this article is now ending I want to finish with some points that will make future socket programming for the reader much more enjoyable:

  • Sockets are endpoints in network communication
  • TCP is the protocol that keeps your data ordered for the recipient
  • Sockets depend on underlying system calls written in C as well as protocols which govern the way data is sent so that interconnected devices have a way of understanding them
  • Sockets are where we listen for and respond to information sent across the web
  • The TCP type socket usually uses a stream to receive and write out data much like a filestream
Look forward to an an article about how to set up a PHP socket server as I will consider writing that in the future. For now, I felt it necessary to give a brief introduction to network sockets and set them apart from the newly popular buzzword "WebSockets"

16 comments:

  1. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  2. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  3. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  4. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  5. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  6. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  7. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  8. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  9. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  10. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  11. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  12. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  13. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  14. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  15. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  16. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete