Socket Programming

Socket Programming

PCBWay

A typical network application consists of a pair of programs – a client program and a server program – residing in two different end systems. When these two programs are executed, a client process and a server process are created, and these processes communicate with each other by reading from, and writing to, sockets. When creating a network application, the developer’s main task is therefore to write the code for both the client and server programs.

DRex Electronics

There are two types of network applications. One type is an implementation whose operation is specified in a protocol standard, such as an RFC or some other standards document; such an application is sometimes referred to as “open”, since the rules specifying its operation are known to all. For such an implementation, the client and server programs must confirm to the rules dictated by the RFC. For example, the client program could be an implementation of the client side of the FTP protocol; similarly , the server program could be an implementation of the FTP server protocol. If one developer writes code for the client program and another developer writes code for the server program  ,and both developers carefully follow the rules of the RFC, then the two programs will be able to interoperate. Indeed, many of today’s network applications involve communication between client and server programs that have been created by independent developers – for example, a Firefox browser communicating with an Apache Web server, or a BitTorrent client communicating with BitTorrent Tracker.

The other type of network application is of proprietary network application. In this case the client and server programs employ an application-layer protocol that has not been openly published in an RFC or elsewhere. A single developer (or development team) creates both the client and server programs, and the developer has complete control over what goes in the code. But because the code does not implement an open protocol, other independent developers will not be able to develop code that interoperates with the application.

In this section, we’ll examine the key issues in developing a client-server application, and we’ll “get out hands dirty” by looking at code that implements a very simple client-server application. During the development phase, one of the first decisions the developer must make is whether the application is to run over TCP or over UDP. TCP is connection oriented and provides a reliable byte-stream channel through which data flows between two end systems. UDP is  connectionless and sends independent packets of data from one end system to the other, without any guarantees about delivery.

When a client or sever program implements a protocol defined by an RFC, it should use the well-known port number associated with the protocol; conversely, when developing a proprietary application, the developer must be careful to avoid using such well-known port numbers.

We introduce UDP and TCP socket programming by way of a simple UDP application and a simple TCP application. We present the simple UDP and TCP applications in Python. We could have written the code in Java, C or C++, but we chose Python mostly because Python clearly exposes the key socket concepts. With Python there are fewer lines of code, and each line can be explained to the novice programmer without difficulty. But there’s no need to be frightened if you are not familiar with Python. You should be able to easily follow the code if you have experience programming in Java, C or C++.