Computer Networks/DV2 Lab Room: BB 219 Additional Information: http://ti.uni-due.de/ti/en/education/teaching/ss13/netlab Equipment for each group: - 1 Server computer (OS: Windows Server 2008 Standard) - 1 Client computer (OS: Windows XP Professional) - 1 Computer as Router / Gateway (OS: Linux) - 1 Switch - Network cables 1. Practical Training: Network planning and installation of a file server 2. Practical Training: Web server installation and dynamic Web pages 3. Practical Training: Installation and configuration of a Firewall 4. Practical Training: Installation of a VPN for the connection of two networks 5. Practical Training: Programming; Client/Server connection over Sockets 6. Practical Training: Network Monitoring Name: Matriculation No.: Supervisor Signature: Contact: Joachim Zumbrägel BB 320 Tel: 0203/379-3978 E-Mail: joachim.zumbraegel@uni-due.de 1. Introduction Communication over sockets is a standard technique usually used for platform independent data exchange between applications. In the context of this practical training we will develop a bi-directional communication between two software applications. We will base our communication on the client-server principle and program the sockets needed for it with the help of the programming language Delphi. 2. Client-Server Model The client-server model is one of the ideas around which network computing revolves. It describes the relationship between two computer programs, service providers (i.e. servers) and service requesters, called clients. Usually clients and servers operate on different hardware. Servers most often feature high-performance central processors, more memory, and larger disk drives than clients. A server stores resources such as files, databases, Web sites, and shares them to clients on the network. Clients are typically computers with network software applications installed, which request and receive information over the network. Due to the growing global communication industry, however, mobile devices are also frequently used as clients in the global mobile network. A client does not share any of its resources, but requests a server's content or service function. 1 2
In some cases a given device can function both as a client and a server for the same application. Furthermore, a device that is a server for one application can simultaneously act as a client for a different application, when requesting services from other servers. Figure 2.2 shows two different possibilities for port communication between a server and several client applications. Either each client communicates over a different port with the server or a single port is made available for all clients. Client 1 Port 1 Client X Port Z Request Port N Server providing resources and/or services Response Client N Server Client Y Fig. 2.2: Client-Server communication over several and over one dedicated ports Server Client Devices Fig. 2.1: Client-Server Communication Figure 2.1 illustrates the basic idea of the client-server communication model. The server takes care of services and resources being available to the requesting clients. The clients request and make use of the provided data by the server. The number of clients to the server is unknown, however it could be limited in order to prevent storage capacity deficiency or reduce the processing load, therefore increasing the server s performance. Usually several clients' applications try to access the same service on a single server. Let us take the application of a Web server as an example. It is often the case where several browser clients try and access the same Web page on a Web server at the same time (quasi simultaneously). Communication over several ports is impractical and uncommon. Rather, it is customary for standardized network services such as FTP, Mail and HTTP to be assigned a fixed port. IANA (Internet Assigned Number Authority) declared that ports from 0 to 1023 are reserved for such services, while dynamic and/or private ports are in the range of 49152 to 65535. Reserved ports (0-1023) are often targets of hacker attacks, because the services they provide are running under special rights (i.e. Super-User rights under LINUX). 3. Internet Sockets Fundamentals An internet socket is the endpoint of a bidirectional communication flow across a TCP/IP computer network. It is the interface to the network s transport layer (layer 4 in the ISO/OSI model). 3 4
When a client connects to a server, a new internet socket is created on each end. Each socket is mapped by the OS to a communicating application process. Both sockets would deliver incoming data packets to the appropriate application process, based on a combination of local and remote IP addresses and port numbers. An Internet socket is characterized by a unique combination of: Protocol (used to establish the communication) Local socket address (Local IP address and port number) Remote socket address (Remote IP address and port number) A socket address is the combination of an IP address and a port into a single identity. When several clients connect to a server concurrently, the server creates one socket for each client, and these sockets share the same local socket address (the server's socket address). However, each of these sockets is considered different by the server's OS, since the remote socket address defined by each client is different. The client s operating system manages the source port and makes sure the built socket address is unique. Therefore, neither the user nor the programmer has to take care about the socket address when a client connects to a server, because unique dedicated sockets are created for each connection. Within the operating system and the application that created a socket, the socket is referred to by a unique integer number called socket number. Communicating local and remote sockets are called socket pairs. Each socket pair is describes by 5 elements (on both local and remote sides), which make it unique. These five parameters are: Protocol (used in the communication) Source IP address Local Socket Address Source port Destination IP Address Remote Socket Address Destination port The following parameters (3 for source and 3 for destination) define one endpoint of the socket pair connection: [Protocol; Local Address; Local port] together with [Protocol; Remote Address; Remote port]. When each application process defines its own endpoint, a connection is established by use of the socket functions. With a connected socket structure, data exchange is feasible. Activating Port 1. Listening on port Creating new connection 2. Assignment of a new Socket 3. Communication over Socket # 1 Communication over Socket # 2 Communication over Socket # N Fig 3.1: Server communication over single port Figure 3.1 illustrates the server communication with clients over a single dedicated port. First the server activates a port (step 1). Then if the client wants to exchange data with the server, a new socket is created and assigned to that connection (step 2). Another connection could be 5 6
subsequently established on that port. Communication is now accomplished through the assigned socket (step 3). There are two different types of sockets. The choice of the socket type automatically specifies the kind of data exchange: Stream sockets are connection-oriented and reliable. Connection-oriented implies that a fixed connection is established, between the two application processes involved (similar to a dedicated line), over which data can flow in both directions. The created connection remains until one of the application processes ends it. Data is transported over the connection in the form of a continuous byte stream. Reliable, because the arrival of data sent over a stream socket to a certain destination is guaranteed. Moreover, the data should arrive in the same order it was sent. Stream sockets are often called TCP sockets. Datagram Sockets are connectionless and not reliable. Connectionless, because no fixed connection between the two application processes involved is established. Data is sent in the form of packets. Since no connection to the destination socket exists, its address must be explicitly indicated. Not reliable suggests that only sending the packet is guaranteed, but not that it will actually reach its destination. Datagram sockets are often called UDP sockets. 4. Socket programming under Windows Any operating system nowadays, provides protocols and the operating software required for them to realize data transfer over different networks. Therefore, the software developer does not have to create the commanding software for the internet protocols, when he wants to write an application transmitting data over the Internet. Software development is not limited to a certain platform or operating system. The source code of an application could be compiled under different operating systems. This applies to Internet applications, as well. For example, the company Netscape developed an Internet browser that works for both the Unix/Linux operating systems, Mac OS and the Microsoft Windows OS. Access to the commanding software for the internet protocols for UNIX is made possible through the BSD Socket API (Berkeley Software Distribution Socket Application Programming Interface). Applications do not access the actual protocol software rather the transmitted data is handed over to the socket, which forms an interface to the Internet. This uniform programming interface gives the software developer the protocol functions independent of the implemented protocol stack. To access an internet application the socket uniform interface comes into play instead of assigned protocol stacks like TCP/IP or SPX/IPX. When Microsoft equipped its operating system Windows with Internet access functions, the BSD Socket API was included, so that Internet applications could be easily imported to the Windows platform. This API was extended to comprise Windows specific functions and is called WinSock API, or simply WinSock. To this date there are two WinSock specifications: Version 1.x, a 16-Bit implementation for Windows 3.11, Windows 95,Windows NT 3.51,Windows NT 4. Version 2.x, a 32-Bit implementation for Windows NT/2000/XP and as an update for Windows 95/98 Both versions are placed against each other in Figure 4.1. It shows the Windows Open Service Architecture (WOSA) and clarifies the interfaces of the WinSock architecture. The WinSock 2 API is an interface for upper applications. It provides application developers with a uniform specification, therefore allowing programmers to develop applications without specific knowledge of the underlying network protocols. The interface to the protocol software is called WinSock 2 SPI (Service Provider Interface). The file "ws2_32.dll" offers the application interface under the Windows OS. Below the protocol software, the hardware driver API is located. It allows access to the hardware driver and the network. 7 8
Since the Windows APIs are written in the "C" programming language, the Visual Component LIBRARY (VCL) will be used for socket development under Delphi. The VCL encapsulates the WinSock functions. Because of that socket encapsulation, programming socket applications with Delphi is much simpler than with classical "C" programming. WinSock 1.1 API Protocol Stack API WinSock 1.1 Application Anwendung winsock.dll (16 bit) wsock32.dll (32 bit) WinSock 2 Application Anwendung ws2_32.dll (32 bit) WinSock 2 API WinSock 2 SPI 5. Socket programming with Delphi Delphi is a powerful visual programming environment. It allows development of fastidious applications for the MS Windows operating systems. Its syntax is similar to the programming language Pascal. Since most of you have little or no experience with the Delphi programming language, we will give you a small introduction before the actual tasks of this practical training. As previously mentioned Delphi encapsulates the WinSock functions. It has predefined components (TClientSocket, TServerSocket), which contain the functionality of a client socket and a server socket. The way to handle such components is not complex. Delphi is based on the principle of a working surface called form, on which different type of graphic and non-graphic components can be placed. The components are selected from register cards and placed via left mouse-click on the form. Protocol Protokollsoftware e.g. z.b. TCP/IP Hardware Driver API Hardware Driver, Packet Driver Hardware Interface Network (Hardware) Interface Network Netzwerk Fig. 4.1: WinSock Architecture Form Internet register card ClientSocket Component Fig. 5.1: Delphi interface ServerSocket Component This procedure applies to all components. Moreover, each of them has a variety of characteristics, which can be manipulated during the development time. Another aspect of these components is the events connected to them. 9 10
For example, a switching surface possesses the event OnClick. That is, if a button is clicked at run time, the procedure assigned to the OnClick event of this switching surface is called in. Figure 5.2 shows the form of our example code, which is used to explain the general working process of the client-server communication. Apart from the components - Client socket and Server socket, the form contains a Verbinden and Listen buttons. Server Open a socket (socket) Name the socket (bind) Client Open a socket (socket) Listen for incoming client connections (listen) Connect to sever (connect) Fig. 5.2: Form of our example code Individual steps are described through the source code. In order to make the methods, procedures and characteristics of the socket communication available, we have to select the Server socket and Client socket components from the register card Internet and place them on the form. Figure 5.3 describes the sequence of the client-server socket communication. First a receiving port must be open on the server side. Opening the server socket with Delphi is accomplished as follows: At first we create a button on the form and name it Listen. By clicking on that button the procedure ListenClick, which assigns and activates a port for the server socket component, is called in. Because each Server socket component can administrate only one port, a separate component must be created on the form for each port. Accept client connections (accept) Send / Receive Data (send / receive) Close connection (close) Send / Receive Data (send / receive) Close connection (close) Fig. 5.3: Sequence of client-server socket communication 11 12
procedure TForm1.ListenClick(Sender: TObject); ServerSocket.Port := 1111; ServerSocket.Open; Afterwards a connection between client and server can be established. For the client application we place a button on the form named Verbinden, which calls in the procedure VerbindenClick if the button is pressed. In this procedure the client socket can be configured with required data. procedure TForm1.VerbindenClick(Sender: TObject); ClientSocket1.Address := 127.0.0.1 ; ClientSocket1.Port := 1111; ClientSocket1.Open; After the creation of a new socket on the server side (in response to a Client s connection request), the Client socket confirms the connection to the new server socket; for this purpose the event OnConnect and therefore the function ClientSocket1Connect is called in on the client socket. By use of this function we can send data to the server (here we send the test message "Hallo"), Procedure TForm1.ClientSocket1Connect(Sender: ClientSocket1.Socket.SendText( Hallo ); Since the socket is used as a parameter for the connection handed over, the following procedure has the same effect: procedure TForm1.ClientSocket1Connect(Sender: Socket.SendText( Hallo ); (1) (3) (2) (3) This way of writing code has the advantage that the event procedures can be used by several components, since the instructions do not depend on the component s name. More explanation will be given during the practical training. If the data sent by the client socket reaches the server socket, the event "OnClientRead" and therefore the function "ServerSocket1ClientRead" of the server socket component occurs. procedure TForm1.ServerSocket1ClientRead(Sender: var EmpfangenerText : string; EmpfangenerText := ServerSocket1.Socket. Connections[0].ReceiveText; The variable EmpfangerText contains the text received by the server socket and could be used for further processing. Even here the function ServerSocket1ClientRead receives as parameter the socket, which triggers the event. For that reason the following function has the same effect: procedure TForm1.ServerSocket1ClientRead(Sender: var EmpfangenerText : string; EmpfangenerText := Socket.ReceiveText; This way of writing code has the advantage that the server component is not limited to a connection and can process several clients. If the client socket writes onto the socket connection, the event OnWrite occurs. If all data is transferred to the server, the socket can be closed. procedure TForm1.ClientSocket1Write(Sender: ClientSocket1.Close; (5) (4) (4) 13 14
The instructions in the procedure can be formulated as follows: procedure TForm1.ClientSocket1Write(Sender: Socket.Close; (5) 6. Exercises Before we concern ourselves with the actual tasks, we will first look at two sample applications created with Delphi. Proceed as follows: Start Delphi on the server and the client (Start Programs Borland Delphi 7 Delphi 7) After Delphi loads navigate to File Open On the server navigate to and open the project located in: D:\netlab\P5\Server\server.dpr On the client navigate to and open the project located in: D:\netlab\P5\Client\client.dpr Press the F9 key. The following windows should appear on the client and server machines respectively. 1. Try to establish a connection between the two sample applications. Consider the order, in which the applications should be run and also mind the attributes you should input. Remark: Before we carry on with the actual tasks, the Delphi programming environment and the sample programs will be described, which should help you in the solutions of the upcoming tasks. 2. Extend the client application in such a way, that it sends automatically the string "Client Request" immediately after the connection to the server is established. 3. Extend the server application in such a way, that it sends automatically the string Server answer back to the client application when it receives the string "Client Request" from it. 4. Further extend the server application in such a way, that it accepts and keeps connections ONLY from your client computer. All other clients should receive the string "Service not available" as answer upon connection try-out and should not be able to communicate further with the server application. 15 16
Notes: Literature: W.Richard Stevens: Unix Network Programming V (Volume 1), Prentice-Hall, 1998 http://www.zotteljedi.de/socket-tipps.html shows how to program sockets in C. 17