Vous êtes sur la page 1sur 2

Chapter 11 Network Programming

by Inoussa Ouedraogo TCP/IP is a network protocol that allows two computer programs (whether running on the same computer or on different computers) to communicate with each other. Usually the protocol is used in a client-server communication scheme. This allows a server process to listen and respond to clients requests, the client processes usually being located on different computers. The listings in this chapter are too long to be printed. They will be included in the downloadable code that belongs to this book. The code will be provided by our website at

11.1 TCP/IP programming

This chapter presents a simple guide to TCP client and server programming. The code has been simplified to highlight just the essential points. So no full error checking is done as it needs to be in actual production-quality professional software. The Synapse sockets library will be used as the base TCP library. This library can be downloaded for free from this address:


The complete source code of the chapter is provided in the tcp sub-folder of the folder of the disk accompanying this book. In a TCP/IP network, every computer is uniquely identified by its IP address. A server process running on a computer listens for clients' requests on a specified port. A TCP port is identified by a positive integer. Only one server process can listen on a given port at a time. A client process is then able to open a connection to a server process given the server computer's IP address and the port number the server process is listening on. In this chapter we will build a simple server process that listens on port 1234 in order to provide simple integer calculation functionality (Add, Subtract, Multiply and Divide) to its clients. So a client request will have to specify both what the desired numerical operation is, and provide appropriate numerical arguments to be operated on. We start by defining a data structure to represent this client request:



11.1 TCP/IP programming

<Code ID = 1> TIntType = ShortInt; TChar = ansichar; TCalcRequest Operation: ArgumentA: ArgumentB: end; </Code> = packed record TChar; TIntType; TIntType;

and another data structure to represent the server's response:

<Code ID = 2> TCalcResponse = packed record Operation: TChar; ArgumentA: TIntType; ArgumentB: TIntType; ResultArg: TIntType; end; </Code>

The response data structure includes the query's items and the ResultArg that holds the result of the computation. Interaction between the server and the client occurs as follows: 1. 2. 3. 4. 5. The server listens for client connections A client connects to the server The client sends a buffer stream representing its request TCalcRequest to the server The server reads the buffer stream into a request variable TCalcRequest The server performs the computation and stores the request's data and the result of the computation to the result variable TCalcResponse 6. The server returns the result variable to the client 7. The client reads and displays the result 8. The client disconnects from the server The sequence of actions from (3) to (7) can be repeated as needed by the client. The required data structures are defined in the file service_intf.pas.