A client socket

Download Report

Transcript A client socket

MP2: P2P File Server
Hoang Nguyen
1
Outline
• Basic network concepts revisited
– Client/Server, Peer-to-peer
– IP address, TCP/UDP
• Basic network programming revisited
– Server socket, Client Socket
– Send/Receive data
– Data Serialization: converting objects to bits
• Network Design: Control/Data plane
– MP explanation
– Implementation suggestions/hints
2
Basic network concepts revisited
3
Basic network concepts (1)
• Client/server
– Servers = machines providing network services
– Clients = machines connecting to servers and
use network services
– Example: FTP server/FTP client
4
Basic network concepts (2)
• A server provides a network service through a
“port”
– Has to “bind” to a particular port
• Clients requests for a specific service at a server
via a pre-defined port
• E.g.: Port 21: FTP control, Port 20: FTP data
• Port: (0..65535)
– 0..1023: well-known ports, requiring admin right to
bind
– 1024..49151: registered ports (for propriety apps)
– The rest: dynamic/private ports
5
Basic network concepts (3)
• What is a Protocol?
– A convention standard that controls or enables the
connection, communication and data transfer between
two parties.
• Example:
– “Protocol” to apply to UIUC
– “Admission protocol”
• In our MP, we will have to define our own
“protocols” and use some existing network
protocols
6
Basic network concepts (3)
• IP address: a tuple of four 8-bit integers
– E.g.: 128.174.252.84
• Host name: name representative of IP
– E.g.: www.cs.uiuc.edu
• TCP: Transmission Control Protocol
– Reliable, ordered, heavy-weight, streaming (connectionoriented/stateful)
– Suitable for any applications requiring reliability such as FTP
• UDP: User Datagram Protocol
– Unreliable, not-ordered, light-weight, datagrams
(connectionless/stateless)
– Suitable for any protocols that can tolerate reasonable losses such
as video/audio/real-time streaming
7
Basic network programming
revisited
8
Basic network programming
revisited
• Highly recommended tutorial
– Beej's Guide to Network Programming
Using Internet Sockets
• http://beej.us/guide/bgnet/
• Make sure you go through until
Section 7 (esp. Section 6 and Section
7.4)
9
Network Socket
• A socket is a one end of a two-way
communications link between two programs
running on the network
– In UNIX, socket is just a file descriptor on which send()
and recv() can be used.
• As mentioned above, a socket can be
– SOCK_STREAM: streaming, reliable, connectionoriented, ordered
– SOCK_DGRAM: datagram, unreliable, connectionless, not-ordered
10
How to create a server socket?
• Pseudo-code & example
fill out address information
sock_fd = create/set socket option
bind to the port
listen for incoming connections
while (forever || not crash)
client_fd = accept a client socket
create a process/thread to
communicate with the client
… // ready to send/recv
11
Comments
• sock_fd: Server socket keeps listening for
incoming connections
– If the accept() function is not invoked, no client
connection can be accepted
– If the number of pending connections exceeds the
backlog, any incoming requests are refused.
• client_fd: socket to communicate with the client
– Think of it like a file descriptor.
12
How to create a client socket?
• Pseudo-code & example
fill out address information (hostname,
port)
sock_fd = connect to the server
… // ready
to send/recv
13
Sending/Receiving data
• no_send = send(sock_fd, data, len,
flag)
• no_recv = recv(sock_fd, buf, max_len,
flag)
• Example
14
Data serialization
•
•
Converting data objects into bits
Three ways:
1) Use readable string description: e.g. “text ‘this is a
text’, int 1, float 1.1234”
2) Sending raw data, passing it send() (dangerous, unportable)
double d = 3490.15926535;
send(s, &d, sizeof d, 0);
3) Encode the data into a portable binary form. The
receiver encodes it. (see pack(), unpack() in the
tutorial)
15
Control/Data Plane
• Control plane: a set of
protocols/software to
bootstrap/setup and control
the communication
• Data plane: a set of
protocols/software to
transmit data
• E.g.: FTP has two ports for
control channel (21) and
data channel (20)
16
In our MP…
Data Plane Functions
Stream (Play, FF,
Rewind) A/V Files
Setup UDP
Connection between
Client and a Specific
Peer-Server
UDP/IP Protocol
Stack
Insert (Upload) A/V
Files
Control Plane Functions
Insert/Delete/Search Stream Request
Control Protocols and Services
Setup TCP
Connection between
Client and a Specific
Peer-Server
Dispatcher setup Peer-Server
addition/Connection setup between
Dispatcher and Peer-Servers for
Control Purposes
TCP/IP Protocol
Stack
TCP/IP Protocol
Stack
17
An example implementation of
control plane
for our MP
1) Dispatcher setup peer-to-peer
connections
2) Handling a client request
18
Entities (1)
• A server has
– a server socket listening on port 7000 for
dispatcher setup connection request
– a client socket to communicate with dispatcher
obtained once accepting dispatcher setup
connection request
– an on-demand server socket listening on a ondemand port for the client to send/recv data
19
Entities (2)
• A dispatcher has
– several client sockets for connections to each
server
– a server socket listening on port 8000 for
clients to send/recv control messages
• A client has
– A client socket to send/recv control messages
to the dispatcher
– A client socket to send/recv data to the server
20
Dispatcher setup connection
cairo
socket
Bi-directional
Socket on port 7000
Setup connection request
cairo
Bi-directional
socket
sanjose
Setup connection request
Socket on port 8000
21
Handling a client request
cairo
3. response
socket
Socket on port 8000
2. request
1. request
Bi-directional
4. response
cairo
Bi-directional
sanjose
socket
22