Unformatted text preview:

Page 1Yu-Chi LaiLecture 3Network ProgrammingCS 640: Computer Networking• Client-server model• Sockets interface• Socket primitives• Example code for echoclient and echoserver• Debugging With GDB• Programming Assignment 1 (MNS)TopicsClient/server model• Client asks (request) – server provides (response)• Typically: single server - multiple clients • The server does not need to know anythingabout the client– even that it exists• The client should always know somethingabout the server– at least where it is locatedClientprocessServerprocess1. Client sends request2. Server handlesrequest3. Server sends response4. Client handlesresponseResourceNote: clients and servers are processes running on hosts (can be the same or different hosts).Page 2Internet Connections (TCP/IP)Connection socket pair(128.2.194.242:3479, 208.216.181.15:80)Server(port 80)ClientClient socket address128.2.194.242:3479Server socket address208.216.181.15:80Client host address128.2.194.242Server host address208.216.181.15• Address the machine on the network– By IP address • Address the process– By the “port”-number• The pair of IP-address + port – makes up a “socket-address”Note: 3479 is anephemeral port allocatedby the kernel Note: 80 is a well-known portassociated with Web serversClients• Examples of client programs– Web browsers, ftp, telnet, ssh• How does a client find the server?– The IP address in the server socket address identifies the host– The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service.– Examples of well known ports• Port 7: Echo server• Port 23: Telnet server• Port 25: Mail server• Port 80: Web serverUsing Ports to Identify ServicesWeb server(port 80)Client hostServer host 128.2.194.242Echo server(port 7)Service request for128.2.194.242:80(i.e., the Web server)Web server(port 80)Echo server(port 7)Service request for128.2.194.242:7(i.e., the echo server)KernelKernelClientClientPage 3Servers• Servers are long-running processes (daemons).– Created at boot-time (typically) by the init process (process 1)– Run continuously until the machine is turned off.• Each server waits for requests to arrive on a well-known port associated with a particular service.– Port 7: echo server– Port 23: telnet server– Port 25: mail server– Port 80: HTTP server• Other applications should choose between 1024 and 65535See /etc/services for a comprehensive list of the services available on a Linux machine.The interface that the OS provides to its networking subsystemapplication layertransport layer (TCP/UDP)network layer (IP)link layer (e.g. ethernet)physical layerapplication layertransport layer (TCP/UDP)network layer (IP)link layer (e.g. ethernet)physical layerOS networkstackSockets as means for inter-process communication (IPC)Client ProcessServer ProcessSocketOS networkstackSocketInternetInternetInternetSockets• What is a socket?– To the kernel, a socket is an endpoint of communication.– To an application, a socket is a file descriptor that lets the application read/write from/to the network.• Remember: All Unix I/O devices, including networks, are modeled as files.• Clients and servers communicate with each by reading from and writing to socket descriptors.• The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.Page 4Socket Programming Cliches• Network Byte Ordering– Network is big-endian, host may be big- or little-endian– Functions work on 16-bit (short) and 32-bit (long) values – htons() / htonl() : convert host byte order to network byte order– ntohs() / ntohl(): convert network byte order to host byte order– Use these to convert network addresses, ports, …• Structure Casts– You will see a lot of ‘structure casts’struct sockaddr_in serveraddr; /* fill in serveraddr with an address */…/* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr,sizeof(serveraddr)); …Socket primitives• SOCKET: int socket(int domain, int type, int protocol);–domain := AF_INET (IPv4 protocol) –type:= (SOCK_DGRAM or SOCK_STREAM )–protocol := 0 (IPPROTO_UDP orIPPROTO_TCP)–returned: socket descriptor (sockfd), -1 is an error• BIND: int bind(int sockfd, struct sockaddr *my_addr, int addrlen);–sockfd- socket descriptor (returned from socket())–my_addr: socket address, struct sockaddr_in is used–addrlen:= sizeof(struct sockaddr)struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ }; • LISTEN: int listen(int sockfd, int backlog); –backlog: how many connections we want to queue• ACCEPT: int accept(int sockfd, void *addr, int *addrlen);–addr: here the socket-address of the caller will be written–returned:a new socket descriptor (for the temporal socket)• CONNECT: int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); //used by TCP client– parameters are same as for bind()• SEND: int send(int sockfd, const void *msg, int len, int flags);–msg: message you want to send–len:length of the message–flags := 0–returned:the number of bytes actually sent• RECEIVE: int recv(int sockfd, void *buf, int len, unsigned int flags);–buf:buffer to receive the message–len:length of the buffer (“don’t give me more!”)–flags := 0–returned:the number of bytes receivedPage 5• SEND (DGRAM-style): int sendto(int sockfd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen);–msg: message you want to send–len:length of the message–flags := 0–to:socket address of the remote process–tolen: = sizeof(struct sockaddr)–returned:the number of bytes actually sent• RECEIVE (DGRAM-style): int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen);–buf:buffer to receive the message–len:length of the buffer (“don’t give me more!”)–from: socket address of the process that sent the data–fromlen:= sizeof(struct sockaddr)–flags := 0–returned:the number of bytes received• CLOSE: close (socketfd);Client+server:


View Full Document

UW-Madison CS 640 - Network Programming

Documents in this Course
Security

Security

21 pages

Mobile IP

Mobile IP

16 pages

Lecture 7

Lecture 7

36 pages

Multicast

Multicast

38 pages

Load more
Download Network Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Network Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Network Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?