Overview
description
Transcript of Overview
![Page 1: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/1.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 1
OverviewOverview
• Sockets
• Sockets in C
• Sockets in Delphi
![Page 2: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/2.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 2
Inter process communicationInter 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:– Files– Shared memory– Pipes– Sockets
![Page 3: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/3.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 3
Socket PropertiesSocket 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 IP-Addresses of a
computer.– Sockets are available as TCP, UDP and as RAW Variant.
![Page 4: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/4.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 4
Socket (History)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“
![Page 5: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/5.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 5
Sockets and Protocol hierarchySockets and Protocol hierarchy
![Page 6: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/6.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 6
Client-Server CommunicationClient-Server Communication
Client: User of a service
Server: Offers a service
![Page 7: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/7.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 7
Socket Programming with UDPSocket Programming with 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.
![Page 8: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/8.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 8
Socket Programming with TCPSocket Programming with TCP
• TCP: reliable transfer of data from one point to the other. Handshake method.
• Distinguish 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.
![Page 9: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/9.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 9
Sockets in RAW ModeSockets 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.
• Under Unix only the super user can use RAW-Sockets.• A very good knowledge about network protocols is required.
![Page 10: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/10.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 10
Communication (TCP)Communication (TCP)
Set up Socket end points
Set up Socket end points
Accept connection
Data transfer
Close connection Close connection
Data transfer
Open connection
Bind socket to IP-Address and port number
Listen for incoming connections
![Page 11: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/11.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 11
Communication (TCP)Communication (TCP)
socket
connect
send
recv
socket
bind
listen
accept
fork fork
socket
connect
send
recv
recv recv
send send
asock1 asock2
process process
Client Client
Server
process process
![Page 12: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/12.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 12
Functions for C-ProgrammingFunctions for C-Programming
• Server und Client– Socket(...)– Send(...)– Recv(...)– Close(...)
• Only server– Bind (...)– Listen (...)– Accept (...)
• Only Client– Connect (...)
• Client
![Page 13: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/13.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 13
Function Socket 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
![Page 14: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/14.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 14
Function Bind (Server)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.
![Page 15: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/15.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 15
Function Listen (Server)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 the waiting queue is full.
![Page 16: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/16.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 16
Function Accept (server) Function Accept (server)
• newsock = accept(int socket, struct clientadress, int cadresslen)• Reads the first connection request out of the queue. The process is blocked if
no request exist. The function “accept” 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
![Page 17: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/17.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 17
Function Connect (Client)Function Connect (Client)
• 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
![Page 18: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/18.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 18
Functionen Send, RecvFunctionen 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.
![Page 19: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/19.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 19
Function closeFunction 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“.
![Page 20: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/20.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 20
Client-example in CClient-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);• bytes = send(sock, buffer, strlen(buffer), 0);• bytes = recv(s, buffer, sizeof(buffer) - 1, 0); • close(s);•• return 0;• }
![Page 21: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/21.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 21
Server-example in CServer-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);• 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);• }• }
![Page 22: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/22.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 22
DelphiDelphi
• 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“.
![Page 23: Overview](https://reader035.fdocuments.in/reader035/viewer/2022062410/5681587e550346895dc5e151/html5/thumbnails/23.jpg)
Computer Net Lab/Praktikum Datenverarbeitung 2 23
Sockets in DelphiSockets in Delphi
• Sockets under Delphi are encapsulated (winsock).• Sockets are easy to configure.• Open Function connects Server und Client.• Intuitive and easy handling of events (i.e. OnConnect) reduces
programming effort significantly.