220 likes | 326 Views
MP2: P2P File Server. Hoang Nguyen. 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
E N D
MP2: P2P File Server Hoang Nguyen
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
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
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
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
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 (connection-oriented/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
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)
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, connection-oriented, ordered • SOCK_DGRAM: datagram, unreliable, connection-less, not-ordered
How to create a server socket? • Pseudo-code & examplefill out address informationsock_fd = create/set socket optionbind to the portlisten for incoming connectionswhile (forever || not crash) client_fd = accept a client socket create a process/thread to communicate with the client… // ready to send/recv
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.
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
Sending/Receiving data • no_send = send(sock_fd, data, len, flag) • no_recv = recv(sock_fd, buf, max_len, flag) • Example
Data serialization • Converting data objects into bits • Three ways: • Use readable string description: e.g. “text ‘this is a text’, int 1, float 1.1234” • Sending raw data, passing it send() (dangerous, un-portable)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)
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)
An example implementation of control planefor our MP Dispatcher setup peer-to-peer connections Handling a client request
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 on-demand port for the client to send/recv data
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
Dispatcher setup connection cairo socket Bi-directional Socket on port 7000 Setup connection request cairo Bi-directional sanjose socket Setup connection request Socket on port 8000
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