Computer Network Lab 2016 Fachgebiet Technische Informatik, Joachim Zumbrägel Overview Sockets Sockets in C Sockets in Delphi 1
Inter process communication There are two possibilities when two processes want to exchange data. Either they communicate directly to each other (via a network) or they use a common storage system. Data exchange can be done by: File(s) Shared memory Pipes Sockets Socket properties A Socket is an end point of a communication connection between two objects (processes). Sockets belong to an application Sockets are bound to a port number Sockets can be bound to all or to definite IPaddresses of a computer Sockets are available as TCP, UDP and as RAW variant 2
Socket (History) Developed for communication of local processes. Implemented 1982 in BSD Unix 4.1c In BSD Version 4.3 extension of this interface in order to allow communication over TCP/IP First time only available under UNIX-systems. Later some different Socket-libraries under windows were available. Microsoft set up a standard: windows Sockets = WinSocks Protocol hierarchy 3
Client-Server Communication Client: User of a service Server: Offers a service Client-Server communication Client 1 Port x Port y Server Client N Client-Server communication over several listen ports 4
Socket and UDP UDP: Service is not reliable, no guarantee that transmitted data will reach the receiver No connection between client and server. No handshaking Sender specifies receivers IP-address and port number. The receiver must extract IP-address and port number out of the senders datagram in order to be able to send data back to the sender Socket and TCP TCP: reliable transfer of data from one point to the other. Handshake method. Distinguishes between server and client process Server creates listen-socket which is listening on a well defined port Client creates client-socket with destination IP-address and port of the server If the client connects the server, the server creates an additional socket which is used for the further communication between client and server 5
Socket in Raw mode Neither TCP nor UDP Allows direct access to the network because it is possible to feed directly packages into the network Protocol can be selected (i.e. ICMP) or can be completely new implemented. Even the IP- Header must be developed for the latter case A very good knowledge about network protocols is required Communication (TCP) Set up Socket end points Set up Socket end points Bind socket to IP-Address and port number Listen for incoming connections Open connection Accept connection Data transfer Data transfer Close connection Close connection 6
Communication (TCP) Server socket Client socket bind listen accept Client socket connect asock1 asock2 connect fork fork send recv recv send process process process process recv send send recv Functions for C-Programming Server und Client Socket(...) Send(...) Recv(...) Close(...) Only server Bind (...) Listen (...) Accept (...) Only Client Connect (...) 7
Function socket descriptor = socket(protofamily, type, protocol) The procedure socket creates a new socket and returns an integer value as descriptor (Identifier and reference for the Socket-Object). As arguments (parameter) the protocol family, the communication type and the protocol are required. Arguments: protofamily defines Protocol family (TCP/IP, Unix,...) type: communication type (connection oriented, without connection or raw) protocol: only used in raw mode Function bind (Server) bind(socket, localaddr, addrlen) The procedure bind connects a new created socket to a network address and a port. Arguments: Socket is the descriptor, which is returned by the function socket. localaddr defines the local address of the socket (IP-Address and port) The integer value addrlen contains the length of the used data structure 8
Function listen (Server) listen (int socket, int queuesize) This function is used in order to show that the server is ready for accepting incoming requests of clients. If the server is working on an other request, the incoming requests are placed in a waiting queue. Arguments: Socket contains the descriptor, which is returned by the function Socket queuesize is the maximum of requests which are accepted by the waiting queue. Further requests are rejected if waiting queue is full. Function accept (Server) newsock = accept(int socket, struct clientadress, int cadresslen) accept p creates a new socket data structure and return a new descriptor (newsock). By use of this newsock the further communication with the client is made. Remark: The new socket does not use a new port. The incoming data are assigned to the right socket by use of the client address data (IP-Address, port). Arguments: Socket contains the descriptor returned by the function socket. clientadress is a data structure where the data (IP-Address, port number) of the requesting client are stored. cadresslen is the length of the clientadress-structure 9
Function connect (Server) connect(int socket, struct serveradress, int sadresslen) Allows the client to start a connection request to the server. The function returns when the server has successfully finished his accept procedure. Arguments: socket contains the descriptor serveradress is data structure which specifies the IP-address and the port number of the desired server. If the connection request is successful the servers data are filled in this data structure. A new port is bind to the socket (like bind at the server). sadresslen the length of the serveradress-structure Function send, recv send(int socket, data, int length,unsigned int flags) recv(int socket,data,int length,unsigned int flags) By use of these functions we can send and receive data. The function recv blocks the process until the data are received. The result of these functions is in both cases the number of the transferred bytes. Arguments: socket is the descriptor of the socket. data is a pointer to the data structure where the sent/received data are stored. Length is the number of bytes. flags contain information about special function for sending and receiving. 10
Function close close(int socket) The function close closes a socket which was before created by the function socket. Therefore this socket is not longer used by the system. If TCP is used, the queued data are transmitted before the socket is closed. If only the data transmission should be finished, the function shutdown can be used instead of the function close. Client Example in C int main(int argc, char *argv[]) { int s; struct sockaddr_in srv; char buffer[1024]; if (argc!= 3) { fprintf(stderr, "usage: %s host port\n", argv[0]); return 1; } s = socket(af_inet, SOCK_STREAM, 0); srv.sin_addr.s_addr = inet_addr(argv[1]); srv.sin_port = htons( (unsigned short int) atol(argv[2])); srv.sin_family = AF_INET; connect(s, (struct sockaddr*) &srv, sizeof(srv)) gets(buffer); send(sock, buffer, strlen(buffer), 0); recv(s, buffer, sizeof(buffer) - 1, 0); close(s); } return 0; 11
Server-example in C int main(int argc, char *argv[]) { int s, c, cli_size, bytes; struct sockaddr_in srv, cli; char buffer[buffer_size], name[buffer_size]; s = socket(af_inet, SOCK_STREAM, 0); srv.sin_addr.s_addr = INADDR_ANY; srv.sin_port = htons( (unsigned short int) atol(argv[1])); srv.sin_family = AF_INET; bind(s, (struct sockaddr*) &srv, sizeof(srv)); listen(s, 3); for(;;) { cli_size = sizeof(cli); c = accept(s, (struct sockaddr*) &cli, &cli_size); bytes = recv(c, name, sizeof(name) - 1, 0); name[bytes] = '\0'; printf("reveived string :%s\n",name); sprintf(buffer, "Hello %s, nice to meet you!\r\n", name); printf("send: %s",buffer); send(c, buffer, strlen(buffer), 0); close(c); } } Delphi Powerful graphical development environment. Easy to create simple application under windows. Components (i.e. text field) are placed onto a form by use of drag and drop. Support data bases, Web-Application, OLE, etc. Pascal like syntax. Supports object oriented programming. Components are easy to manipulate during development time and during runtime too. Event oriented handling of components i.e. Click on a button. 12
Sockets in Delphi Sockets are components Sockets are easy to configure. Open Function connects Server und Client. Intuitive and easy handling of events (i.e. OnConnect) reduces programming effort significantly. Sockets in Delphi Form Internet register card ClientSocket Component ServerSocket Component 13