Network Programming with Internet Sockets
Internet Sockets Introduction
"Internet sockets", while generally unseen by internet users, are the backbone of nearly all web applications. The effective use of network sockets allows server applications to communicate with client applications, such as a web browser accessing a web page.
Every internet socket can be defined by a few characteristics, a local socket address and a protocol. One of the most common ports used is TCP port 80, through which HyperText Transfer Protocol (HTTP) is sent, allowing web browsing.
Seven-Layer OSI model
(Layers of the Networking Onion)
Internet sockets can be used and functionally understood without grasping all the concepts of computer networking. However, a firm grasp of the seven-layer OSI model can provide a little context for making the understanding of sockets a little bit easier.
Layer 1 - The Physical Layer - where the bit stream actually travels as electricity, light or radio through the hardware of a network.
Layer 2 - Data Link - encoded data packets are sent here, with the Media Access Control (MAC) and Logical Link Control (LLC) layer determining identidy and synchronization respectively.
Layer 3 - The Network Layer - this is where routing and switching takes place, setting up "virtual circuits" and pushing data from node to node, handling addressing and packet flow. This is where your IP address exists.
Layer 4- Transport Layer - this is a transparent flow of data between any two hosts, which handles errors, data recovery and manages information exchange. This is where TCP and UDP exist. Web sockets and HTTP all fit into here!
Layer 5 - Session Layer - this pertains to a single connection between applications, coordinating exchanges and managing process flow. For example, a web server or SQL server might be at work here.
Layer 6 - Presentation Layer - this is how the data is formatted and represented. So here you would find encryption & file types.
Layer 7 - Application Layer - This is where the controls exist for the user to interact with all the layers beneath. A web browser or FTP client would be found at layer 7.
The network socket originally emerged as a part of a "socket API," based on the Berkely Sockets project in the early 1980's. This was implemented originally in C as part of the networking interface for the Unix operating system.
By 1989, Berkely Sockets Distribution (BSD) became open source and influenced the Microsoft project Winsock. The BSD API went on to influence other socket APIs and helped to shape the commercial web as we know it today.
Alternative Methods for Sending Data
There is more than one way to send data from an internet application. Internet sockets are just one way, here's a few others which operate at various levels of the network system.
Each of these has it's own advantages and disadvantages, and is worth researching if you are learning about internet sockets:
Socket Streams - an internet socket connection implemented on TCP for reliable data flow.
Datagrams - a self-contained message sent over UDP which does not have "guaranteed" delivery to destination.
RESTful Calls - REST means Representational State Transfer, and is effectively an extension of HTTP for encapsulated communication.
SOAP - similar to rest, it is a messaging service for passing HTTP and markup files.
Each option has a different way to implement it. REST is fairly standard for web forms, while socket streaming is preferred for real time communications or any streaming content.
Places to get started
Working with web sockets generally requires a basic knowledge of programming before getting started. The language that you use though doesn't really matter, as every major language will have some implementation of a socket API for this type of communication.
Perhaps the best thing to do first would be to get familiar with the core concepts.
TCP/IP Sockets in C: Practical Guide for Programmers - Baylor University Presentation.
Sometimes a few useful links just isn't good enough. To get really good at using web sockets, and controlling them at a deep level for steering networks, it might be more practical to take a course.
Libraries by Programming Language
If you have a preferred language already, the best place to start with web sockets might be to use the libraries and resources available within the domain where you are already comfortable.
Here's a separation of resources and examples by programming language.
Web Sockets with Java
Web Sockets in C/C++
Web Sockets in C# & .NET
Web Sockets in Python
Web Sockets in Ruby
Q & A
Q. What is the difference between a web port and an internet socket?
A. This can be pretty technical answer, but on the surface - a web socket is a TCP connection endpoint while a port is "virtualization identifier" and is not considered part of the network unless there is an IP address attached to make it into a web port.
In "Plain English," a "port" is a numbered address found on a particular device, while a "socket" is tied to the applications in communication. Without an application there is no "socket."
Q. When should I use RESTful calls versus Web Sockets?
A. Usually, RESTful calls are more useful for smaller pieces of data, say like a web form that gets triggered by a click event. Web Sockets require setting up a "handshake" to allow streaming data through a connection.
This is generally best for streaming of large files such as online video or for real-time communication.
Q. All of these concepts sound surreal and difficult to me, should I start somewhere else?
A. That's entirely up to you! You should probably be familiar with at least one programming language before trying to use web sockets, but you can always find a good instructor or project where you get to learn both the language and sockets as you go.
Just be patient and stay optimistic, web sockets can be tricky but are very useful!