• Sockets • Sockets in C • Sockets in Delphi

advertisement
Overview
• Sockets
• Sockets in C
• Sockets in Delphi
Computer Net Lab/Praktikum Datenverarbeitung 2
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:
– Files
– Shared memory
– Pipes
– Sockets
Computer Net Lab/Praktikum Datenverarbeitung 2
2
Socket Properties
• A Socket is an end point of a communication connection
between two objects (processes).
– Sockets belong to an application.
– Sockets are bind to a port number.
– Sockets can be bind to all or to definite IP-Addresses of a
computer.
– Sockets are available as TCP, UDP and as RAW Variant.
Computer Net Lab/Praktikum Datenverarbeitung 2
3
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 und windows were available Microsoft
set up a standard: „windows Sockets“= „WinSocks“
Computer Net Lab/Praktikum Datenverarbeitung 2
4
Sockets and Protocol hierarchy
Computer Net Lab/Praktikum Datenverarbeitung 2
5
Client-Server Communication
Client: User of a service
Server: Offers a service
Computer Net Lab/Praktikum Datenverarbeitung 2
6
Socket 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.
Computer Net Lab/Praktikum Datenverarbeitung 2
7
Socket 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.
Computer Net Lab/Praktikum Datenverarbeitung 2
8
Sockets 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.
Computer Net Lab/Praktikum Datenverarbeitung 2
9
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
Computer Net Lab/Praktikum Datenverarbeitung 2
10
Functions for C-Programming
• Server und Client
– Socket(...)
– Send(...)
– Recv(...)
– Close(...)
• Only server
– Bind (...)
– Listen (...)
– Accept (...)
• Clientseitig
– Connect (...)
Computer Net Lab/Praktikum Datenverarbeitung 2
11
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
Computer Net Lab/Praktikum Datenverarbeitung 2
12
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.
Computer Net Lab/Praktikum Datenverarbeitung 2
13
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.
Computer Net Lab/Praktikum Datenverarbeitung 2
14
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
Computer Net Lab/Praktikum Datenverarbeitung 2
15
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
Computer Net Lab/Praktikum Datenverarbeitung 2
16
Functionen Send, Recv
• send(int socket, data, int length,unsigned int flags)
recv(int socket,data,int length,unsigned int flags)
• By use of these function we can send and receive data. The
function “recv” blocks the process until the data are received.
The result of these function 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.
Computer Net Lab/Praktikum Datenverarbeitung 2
17
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“.
Computer Net Lab/Praktikum Datenverarbeitung 2
18
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);
bytes = send(sock, buffer, strlen(buffer), 0);
bytes = recv(s, buffer, sizeof(buffer) - 1, 0);
close(s);
return 0;
}
Computer Net Lab/Praktikum Datenverarbeitung 2
19
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);
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);
}
}
Computer Net Lab/Praktikum Datenverarbeitung 2
20
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“.
Computer Net Lab/Praktikum Datenverarbeitung 2
21
Sockets 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.
Computer Net Lab/Praktikum Datenverarbeitung 2
22
Download