1 / 75

TDC561 Network Programming

TDC561 Network Programming. Week 1: Course Introduction; Socket Application Programming Interface; Examples: C/C++ UNIX/Linux APIs . Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu. TDC561 – Course Summary. Topics: TCP/IP protocol suite

masato
Download Presentation

TDC561 Network Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TDC561 Network Programming Week 1: Course Introduction; Socket Application Programming Interface; Examples: C/C++ UNIX/Linux APIs Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu

  2. TDC561 – Course Summary • Topics: • TCP/IP protocol suite • Socket programming; TCP and UDP sockets; Reliable UDP Communication; • Client-Server Models; • Socket options • Threads; concurrency; locking • Select calls • Non-Blocking I/O • IOCTL • ICMP • Broadcast/Multicast • Programming aspects related to network performance

  3. Objectives • Upon completion of this course, students will • have a good understanding of the TCP/UDP network programming interface • be able to develop client-server network applications on the Internet, based on UNIX/Linux • know how to apply methods for measuring and tuning the performance of network applications

  4. Prerequisites • TDC 463, CSC 309 • C/C++ will be used to illustrate the concepts presented in the lecture material and for the implementation of the programming assignments and the project. • Basic understanding of UNIX / Linux operating system and programming environment

  5. Douglas Comer, David Stevens, Internetworking with TCP/IP : Client-Server Programming, Volume III (BSD Unix and ANSI C), 2nd edition, 1996 (ISBN 0-13-260969-X) W. Richard Stevens, Network Programming : Networking API: Sockets and XTI, Volume 1, 2nd edition, 1997 (ISBN 0-13-490012-X) Texts

  6. L.Peterson, B. Davie. Computer Networks: A System Approach. Morgan Kaufman, 2000. (ISBN 1-55860-514-2) W. Stallings. Local and Metropolitan Area Networks. Prentice Hall,2000. (ISBN 0130129390) W. Richard Stevens, Network Programming : Inter-process Communication, Volume 2, 2nd edition, 1999 (ISBN 0-13-081081-9) Robertazzi, Thomas G., Computer Networks and Systems - Queuing Theory and Performance Evaluation, second edition, Springer Verlag, 1994 Other References

  7. Class Work and Grading

  8. Programming Environment • All students will get an account on hawk.depaul.edu server (running HP-UX). • Optional, you can use Linux system • Project submissions via COL ( dlweb.cti.depaul.edu ) • use Submit Assignments link • Grading will be done on HP-UX or Linux • An experimental report is required for each programming project • The the report should include the program design description and the test cases and test scenarios for the application.

  9. TDC561 – Network Programming Instructor: Camelia Zlatea, PhD Office Hours: T 4:45-5:45PM; 9:00-9:30PM Where: Rm. 429, ext. x26149 (during office hours) Email: czlatea@cs.depaul.edu Web: http://condor.depaul.edu/~czlatea/TDC561/(*) Note: • In addition, course materials will be available from dlweb.cti.depaul.edu

  10. Class Communication • COL (dlweb.cti.depaul.edu) will be used primarily for: • Links to course materials/assignments • Grades • Announcements • Email/Communication • Newsgroup • Asking questions • Send me an email at czlatea@cs.depaul.edu OR • Use dlweb.cti.depaul.edu and post questions to class forum • Anybody may reply (including your classmates) • Everyone benefits from common issues

  11. Socket APIs C/C++ UNIX/Linux Systems Calls (APIs)

  12. Douglas Comer, David Stevens, Internetworking with TCP/IP : Client-Server Programming, Volume III (BSD Unix and ANSI C), 2nd edition, 1996 (ISBN 0-13-260969-X) Chap. 3,4,5 W. Richard Stevens, Network Programming : Networking API: Sockets and XTI, Volume 1, 2nd edition, 1998 (ISBN 0-13-490012-X) Chap. 1,2,3,4 John Shapley Gray, Interprocess Communications in UNIX -- The Nooks and Crannies Prentice Hall PTR, NJ, 1998 Chap. 10 References

  13. Client Server Communication • The transport protocols TCP and UDP were designed to enable communication between network applications • Internet host can have several servers running. • usually has only one physical link to the “rest of the world” • When packets arrive how does the host identify which packets should go to which server? • Ports • ports are used as logical connections between network applications • 16 bit number (65536 possible ports) • demultiplexing key • identify the application/process to receive the packet • TCP connection • source IP address and source port number • destination IP address and destination port number • the combination IP Address : Port Number pair is called a Socket

  14. 65536 65536 65535 65535 65534 65534 65533 65533 82 82 81 81 80 80 79 79 78 78 3 3 2 2 1 1 Client Server Communication Port IP Network Host Network 122.34.45.67 Network Host 123.45.67.89 SOCKETS 122.34.45.67: 65534 123.45.67.89:80

  15. 65536 IP Host 65535 65534 65533 82 IP Host 81 80 79 78 3 2 IP Host 1 Client Server Communication Port HTTP Server with three active connections (sockets). IP Network Active Active Active Listening IP Host/ Server The HTTP server listens for future connections.

  16. Ports • Port - A 16-bit number that identifies the application process that receives an incoming message. • Port numbers divided into three categories • Well Known Ports 0-1023 • Registered Ports 1024-49151 by the IANA (Internet Assigned Numbers Authority), and represent second tier common ports (socks (1080), WINS (1512), kermit (1649), https (443)) • Dynamic/Private Ports 49152-65535 ephemeral ports, available for temporary client usage • Reserved ports or well-known ports (0 to 1023) • Standard ports for well-known applications. • See /etc/services file on any UNIX machine for listing of services on reserved ports. • 1 TCP Port Service Multiplexer • 20 File Transfer Protocol (FTP) Data • 21 FTP Control • 23 Telnet • 25 Simple Mail Transfer (SMT) • 43 Who Is • 69 Trivial File Transfer Protocol (TFTP) • 80 HTTP

  17. Associations • A socket address is the triplet: {protocol, local-IP, local-port} • example, {tcp, 130.245.1.44, 23} • An association is the 5-tuple that completely specifies the two end-points that comprise a connection: {protocol, local-IP, local-port, remote-IP, remote-port} • example: {tcp, 130.245.1.44, 23, 130.245.1.45, 1024}

  18. Socket Domain Families • There are several significant socket domain families: • Internet Domain Sockets (AF_INET) • implemented via IP addresses and port numbers • Unix Domain Sockets (AF_UNIX) • implemented via filenames (similar to IPC “named pipe”)

  19. Creating a Socket #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); • domain is one of the Protocol Families (AF_INET, AF_UNIX, etc.) • type defines the communication protocol semantics, usually defines either: • SOCK_STREAM: connection-oriented stream (TCP) • SOCK_DGRAM: connectionless, unreliable (UDP) • protocol specifies a particular protocol, just set this to 0 to accept the default

  20. The Socket Structure • INET Address struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ } • INET Socket Struct sockaddr_in { uint8_t sin_len; /* length of structure (16) */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* 16-bit TCP/UDP port number */ struct in_addr sin_addr; /* 32-bit IPv4 address */ char sin_zero[8]; /* unused */ }

  21. Setup for an Internet Domain Socket struct sockaddr_in { sa_family_t sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char pad[...]; }; • sin_family is set to Address Family AF_INET • sin_port is set to the port number you want to bind to • sin_addr is set to the IP address of the machine you are binding to (struct in_addr is a wrapper struct for an unsigned long) • ignore padding

  22. Stream Socket Transaction (TCP Connection) Server socket() Client bind() socket() listen() 3-way handshake connect() accept() write() data read() data write() read() EOF close() read() close()

  23. Connection-oriented socket connections • Client-Server view

  24. Server Side Socket Details SERVER int socket(int domain, int type, int protocol) Create socket sockfd = socket(AP_INET, SOCK_STREAM, 0); bind a port to the int bind(int sockfd, struct sockaddr *server_addr, socklen_t length) socket bind(sockfd, &server, sizeof(server)); listen for incoming int listen( int sockfd, int num_queued_requests) connections listen( sockfd, 5); accept an int accept(int sockfd, struct sockaddr *incoming_address, socklen_t length) incoming newfd = accept(sockfd, &client, sizeof(client)); /* BLOCKS */ connection read from the int read(int sockfd, void * buffer, size_t buffer_size) connection read(newfd, buffer, sizeof(buffer)); write to the int write(int sockfd, void * buffer, size_t buffer_size) connection write(newfd, buffer, sizeof(buffer));

  25. Client Side Socket Details CLIENT int socket(int domain, int type, int protocol) Create socket sockfd = socket(AF_INET, SOCK_STREAM, 0); int connect(int sockfd, struct sockaddr *server_address, socklen_t length) connect to Server socket connect(sockfd, &server, sizeof(server)); /* Blocking */ int write(int sockfd, void * buffer, size_t buffer_size) write to the connection write(sockfd, buffer, sizeof(buffer)); int read(int sockfd, void * buffer, size_t buffer_size) read from the connection read(sockfd, buffer, sizeof(buffer));

  26. Reading From and Writing To Stream Sockets • Sockets, are inter-process-communication mechanism, similar with files: • low level IO: • read() system call • write() system call • higher level IO: • int recv(int socket, char *buf, int len, int flags); • blocks on read • returns 0 when other connection has terminated • int send(int socket, char *buf, int len, int flags); • returns the number of bytes actually sent • where flags may be one of: • MSG_DONTROUTE (don’t route out of localnet) • MSG_OOB (out of band data (causes interruption)) • MSG_PEEK (examine, but don’t remove from stream)

  27. Closing a Socket Session • int close(int socket); • closes read/write IO, closes socket file descriptor • int shutdown( int socketfd, int mode); • where mode is: • 0: no more receives allowed “r” • 1: no more sends are allowed “w” • 2: disables both receives and sends (but doesn’t close the socket, use close() for that) “rw”

  28. Byte Ordering • Different computer architectures use different byte ordering to represent/store multi-byte values (such as 16-bit/32-bit integers) • 16 bit integer: Little-Endian (Intel) Big-Endian (RISC-Sparc) Low Byte High Byte Address A High Byte Low Byte Address A+1

  29. Byte Order and Networking • Suppose a Big Endian machine sends a 16 bit integer with the value 2: • A Little Endian machine will understand the number as 512: • How do two machines with different byte-orders communicate? • Using network byte-order • Network byte-order = big-endian order 0000000000000010 0000001000000000

  30. Network Byte Order • Conversion of application-level data is left up to the presentation layer. • Lower level layers communicate using a fixed byte order called network byte order for all control data. • TCP/IP mandates that big-endian byte ordering be used for transmitting protocol information • All values stored in a sockaddr_in must be in network byte order. • sin_port a TCP/IP port number. • sin_addr an IP address.

  31. Network Byte Order Functions • Several functions are provided to allow conversion between host and network byte ordering, • Conversion macros (<netinet/in.h>) • to translate 32-bit numbers (i.e. IP addresses): • unsigned long htonl(unsigned long hostlong); • unsigned long ntohl(unsigned long netlong); • to translate 16-bit numbers (i.e. Port numbers): • unsigned short htons(unsigned short hostshort); • unsigned short ntohs(unsigned short netshort);

  32. TCP Sockets Programming Summary • Creating a passive mode (server) socket. • Establishing an application-level connection. • send/receive data. • Terminating a connection.

  33. Creating a TCP socket int socket(int family,int type,int proto); int mysockfd; mysockfd = socket( AF_INET, SOCK_STREAM, 0); if (mysockfd<0) { /* ERROR */ }

  34. Binding to well known address int mysockfd; int err; struct sockaddr_in myaddr; mysockfd = socket(AF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( 80 ); myaddr.sin_addr = htonl( INADDR_ANY ); err= bind(mysockfd, (sockaddr *) &myaddr, sizeof(myaddr));

  35. Bind – What Port Number? • Clients typically don’t care what port they are assigned. • When you call bind you can tell it to assign you any available port: myaddr.port = htons(0);

  36. Bind - What IP address ? • How can you find out what your IP address is so you can tell bind() ? • There is no realistic way for you to know the right IP address to give bind() - what if the computer has multiple network interfaces? • Specify the IP address as: INADDR_ANY, this tells the OS to handle the IP address specification.

  37. Converting Between IP Address formats • From ASCII to numeric • “130.245.1.44”  32-bit network byte ordered value • inet_aton(…) with IPv4 • inet_pton(…) with IPv4 and IPv6 • From numeric to ASCII • 32-bit value  “130.245.1.44” • inet_ntoa(…) with IPv4 • inet_ntop(…) with IPv4 and IPv6 • Note – inet_addr(…) obsolete • cannot handle broadcast address “255.255.255.255” (0xFFFFFFFF)

  38. IPv4 Address Conversion int inet_aton( char *, struct in_addr *); Convert ASCII dotted-decimal IP address to network byte order 32 bit value. Returns 1 on success, 0 on failure. char *inet_ntoa(struct in_addr); Convert network byte ordered value to ASCII dotted-decimal (a string).

  39. Establishing a passive mode TCP socket • Passive mode: • Address already determined. • Tell the kernel to accept incoming connection requests directed at the socket address. • 3-way handshake • Tell the kernel to queue incoming connections for us.

  40. listen() int listen( int mysockfd, int backlog); mysockfd is the TCP socket (already bound to an address) backlog is the number of incoming connections the kernel should be able to keep track of (queue for us). listen() returns -1 on error (otherwise 0).

  41. Accepting an incoming connection • Once we call listen(), the O.S. will queue incoming connections • Handles the 3-way handshake • Queues up multiple connections. • When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.

  42. accept() int accept( int mysockfd, struct sockaddr* cliaddr, socklen_t *addrlen); mysockfd is the passive mode TCP socket. cliaddr is a pointer to allocated space. addrlen is a value-result argument • must be set to the size of cliaddr • on return, will be set to be the number of used bytes in cliaddr. • accept() return value • accept() returns a new socket descriptor (positive integer) or -1 on error. • After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls.

  43. Terminating a TCP connection • Either end of the connection can call the close() system call. • If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF.

  44. Client Code • TCP clients can call connect() which: • takes care of establishing an endpoint address for the client socket. • don’t need to call bind first, the O.S. will take care of assigning the local endpoint address (TCP port number, IP address). • Attempts to establish a connection to the specified server. • 3-way handshake

  45. connect() int connect( int sockfd, const struct sockaddr *server, socklen_t addrlen); sockfdis an already created TCP socket. servercontains the address of the server (IP Address and TCP port number) connect() returns 0 if OK, -1 on error

  46. Reading from a TCP socket int read( int fd, char *buf, int max); • By default read() will block until data is available. • reading from a TCP socket may return less than max bytes (whatever is available).

  47. Writing to a TCP socket int write( int fd, char *buf, int num); • write might not be able to write all num bytes (on a nonblocking socket). • Other functions (API) • readn(), writen() and readline() - see man pages definitions.

  48. Example [ from R. Stevens text] Client Server communication Server Client Network Machine B Machine A • Web browser and server • FTP client and server • Telnet client and server

  49. Example – Daytime Server/Client Application protocol (end-to-end logical connection) Daytime client Daytime server Socket API Socket API TCP protocol (end-to-end logical connection) TCP TCP IP protocol (physical connection ) IP IP MAC-level protocol (physical connection ) MAC driver MAC driver Actual data flow MAC = media access control Network

  50. Daytime client • Connects to a daytime server • Retrieves the current date and time % gettime 130.245.1.44 Thu Sept 05 15:50:00 2002 #include "unp.h" int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; if( argc != 2 )err_quit(“usage : gettime <IP address>”); /* Create a TCP socket */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); /* Specify server’s IP address and port */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server port */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]);

More Related