Socket Programming
This week we were introduced to something near and dear to me: socket programming! I am no stranger to it. In fact, it was one of the very first things I ever learned about programming (while modifying mIRC connection scripts for the MSN Chat Network). What I did not know or understand until this week, however, was the difference between UDP and TCP sockets. Either the User Datagram Protocol (UDP) or the Transmission Control Protocol (TCP) may be used to send data across the internet from one host to another. For general information about them, see my blog post last week
So what is a socket? A socket is, in reality, just part of the transport layer's API. Sockets are what applications use to send and receive data over the internet. All of the major languages provide a high-level abstraction (socket library) for accessing the transport layer's services. There is little programmatic difference between using TCP or UDP sockets. The primary difference is that TCP requires a connection to be established using a three-way-handshake procedure before sending data. First, the client (sending application) sends a packet (SYN) to the server (receiving application). The server then responds to the client with a packet (SYN/ACK). Finally, the client acknowledges the response with another packet (ACK). Note that applications often choose to send their first data packet with their ACK packet. For example, web browsers place an HTTP request in theirs.
The handshake procedure involves a little extra work in our program. Unlike UDP's interface, which simply requires us to bind the server application to a certain port number* before receiving data, TCP's interface requires the server application to listen to the port (after binding to it) for any incoming connections, and then explicitly accept the connection. On the client side, a TCP application must first connect to the server before sending data, whereas UDP applications attach the server's IP address and port to each sent data packet. Note that the operating system automatically assigns an available port to our client application so it can receive data from the server.
* Most internet users know that their computer/device has an IP address that others can potentially connect to. What they may not know about is something called a port. There are many port numbers, ranging from 0 to 65353, and they all belong to your IP address! Common applications are tied to certain ports. For example, e-mail applications use port 25, web browsers use port 80, and chat rooms use port 6667. Ports make it easier for the operating system to direct traffic to the correct application. If you can think of your IP address like a seaport, you probably would not want all arriving ships to unload their cargo at the same dock.
No comments:
Post a Comment