tcp ip sockets in c practical guide for computer chat
play

TCP/IP Sockets in C: Practical Guide for Computer Chat Programmers - PDF document

TCP/IP Sockets in C: Practical Guide for Computer Chat Programmers ! How do we make computers talk? http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/ ! How are they interconnected? Michael J. Donahoo Kenneth L. Calvert Internet Protocol


  1. TCP/IP Sockets in C: Practical Guide for Computer Chat Programmers ! How do we make computers talk? http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/ ! How are they interconnected? Michael J. Donahoo Kenneth L. Calvert Internet Protocol (IP) Morgan Kaufmann Publisher $14.95 Paperback A protocol – an agreement on how to communicate – e.g., how is the information structured (length of message, address location – port) http://cobweb.cs.uga.edu/~maria/classes/1730-Fall-2014/HW6.htm TCP/IP protocol suit. (preview) Internet Protocol (IP) IP Address ! Datagram (packet) protocol ! 32-bit identifier (IPv4, IPv6=128 bits) ! Best-effort service ! Dotted-quad: 192.118.56.25 (readable) ! Loss ! www.mkp.com -> 167.208.101.28 ! Reordering ! Identifies a host interface (not a computer. ! Duplication could have multiple interfaces on a ! Delay computer) ! Host-to-host delivery (we are not at ‘application’ level yet – more later) Like a Street Address 192.18.22.13 209.134.16.123 Transport Protocols Best-effort is not sufficient! ! Organize Protocols in Layers ! Add services on top of IP (higher level – abstractions) Application protocol Application Application ! User Datagram Protocol (UDP) TCP TCP protocol TCP ! Data checksum IP IP IP IP IP protocol IP protocol IP protocol ! Best-effort ! Transmission Control Protocol (TCP) Data Data Data Data Data Network Data Data Data Link Link Link Link Access Link Link Link Link ! Data checksum Host Router Router Host ! Reliable byte-stream delivery ! Flow and congestion control

  2. http://www.defaultports.com/ Internet Assigned Numbers Authority Well-known 1-1,023 Internet Phone Book Ports Registered 1,024-49,151 Dynamic 49,152-65,535 Identifying the ultimate destination ! Domain Name Service (DNS) ! Data base maps domain names to internet ! IP addresses identify hosts addresses ! Host has many applications ! Ports (16-bit identifier) 1-65,535 (about 2000 are reserved). Application WWW E-mail Telnet Port 80 25 23 Echo 7 Like a Room Number 192.18.22.13 Socket Sockets How does one speak TCP/IP? ! Identified by ! protocol and ! local/remote address/port ! (both address and a port) ! Sockets provides an interface to TCP/IP ! Applications may refer to ! Generic interface for many protocols many sockets ! Sockets accessed by many applications Like a File Descriptor for a file Data Structure to Specifying TCP/IP Sockets: Addresses Creates end point (and flavor) int mySock = socket( family, type, protocol ); ! Generic struct sockaddr ! TCP/IP-specific sockets { unsigned short sa_family; /* Address family (e.g., AF_INET) */ char sa_data[14]; /* BLOB */ /* Protocol-specific address information */ Protocol Family Type }; TCP SOCK_STREAM IPPROTO_TCP struct sockaddr_in PF_INET { UDP SOCK_DGRAM IPPROTO_UDP IP Specific unsigned short sin_family; /* Internet protocol (AF_INET) */ ! Socket reference unsigned short sin_port; /* Port (16-bits) */ struct in_addr sin_addr; /* Internet address (32-bits) */ ! File (socket) descriptor in UNIX char sin_zero[8]; /* Not used */ }; struct in_addr { Type : Semantics of transmission: e.g., is it reliable, best-effort, unsigned long s_addr; /* Internet address (32-bits) */ boundaries (packets, streams) };

  3. Historically the intent was that a single protocol family Family Blob ( 14 bytes) might support multiple address families 2 bytes 2 bytes 4 bytes 8 bytes Note: 16 bits 32 bits Family Port Internet Address Unused ! In Theory: Protocol family to socket() 16 byte/128 bit data structure (PF_INET) for internet family are struct sockaddr { different from the addressing scheme unsigned short sa_family; /* Address family (e.g., AF_INET) * how to interpret the rest */ char sa_data[14]; /* Protocol-specific address information */ (AF_INET) – here it is 1-1 but does not }; struct sockaddr_in /* TCP/IP structure form */ need to be. { unsigned short sin_family; /* Internet protocol (AF_INET) */ ! In Practice: AF_XXXX and PF_XXXX unsigned short sin_port; /* Port (16-bits) */ struct in_addr sin_addr; /* Internet address (32-bits) */ constants are interchangeable. char sin_zero[8]; /* Not used */ }; ! Values are the same AF_XXXX = PF_XXXX struct in_addr { unsigned long s_addr; /* Internet address (32-bits) */ }; Clients and Servers TCP Client/Server Interaction ! Server: Waits until needed ! Client: Initiates the connection Server starts by getting ready to receive client connections… Server: Jane Client: Bob “Hi. I’m Bob.” Server Client Create a TCP socket 1. Create a TCP socket “Hi, Bob. I’m Jane” 1. Assign a port to socket 2. Establish connection 2. Set socket to listen Communicate 3. 3. “Nice to meet you, Jane.” Repeatedly: Close the connection 4. 4. Accept new connection a. Two separate programs – works both on the same Communicate b. machine or on remote (e.g., nike, ajax) Close the connection c. TCP Client/Server Interaction TCP Client/Server Interaction echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);/* Any incoming interface */ /* Create socket for incoming connections */ echoServAddr.sin_port = htons(echoServPort); /* Local port */ if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); if( bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr) ) < 0) DieWithError("bind() failed"); Server Server Client Client Create a TCP socket Create a TCP socket 1. 1. Create a TCP socket Create a TCP socket 1. 1. Bind socket to a port Bind socket to a port 2. 2. Establish connection Establish connection 2. 2. Set socket to listen Set socket to listen Communicate 3. Communicate 3. 3. 3. Repeatedly: Repeatedly: Close the connection 4. Close the connection 4. 4. 4. Accept new connection Accept new connection a. a. Communicate Communicate b. b. Close the connection Close the connection c. c.

  4. TCP Client/Server Interaction TCP Client/Server Interaction for (;;) /* Run forever */ /* Mark the socket so it will listen for incoming connections */ { if (listen(servSock, MAXPENDING) < 0) clntLen = sizeof(echoClntAddr); DieWithError("listen() failed"); if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) DieWithError("accept() failed"); Server Server Client Client Create a TCP socket Create a TCP socket 1. 1. Create a TCP socket Create a TCP socket 1. 1. Bind socket to a port Bind socket to a port 2. 2. Establish connection Establish connection 2. 2. Set socket to listen Set socket to listen Communicate 3. Communicate 3. 3. 3. Repeatedly: Repeatedly: Close the connection 4. Close the connection 4. 4. 4. Accept new connection Accept new connection a. a. Communicate Communicate b. b. Close the connection Close the connection c. c. TCP Client/Server Interaction TCP Client/Server Interaction Server is now blocked waiting for connection from a client Later, a client decides to talk to the server… Server Server Client Client Create a TCP socket Create a TCP socket 1. 1. Create a TCP socket Create a TCP socket 1. 1. Bind socket to a port Bind socket to a port 2. 2. Establish connection Establish connection 2. 2. Set socket to listen Set socket to listen Communicate 3. Communicate 3. 3. 3. Repeatedly: Repeatedly: Close the connection 4. Close the connection 4. 4. 4. Accept new connection Accept new connection a. a. Communicate Communicate b. b. Close the connection Close the connection c. c. TCP Client/Server Interaction TCP Client/Server Interaction echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ /* Create a reliable, stream socket using TCP */ echoServAddr.sin_port = htons(echoServPort); /* Server port */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed"); Server Server Client Client Create a TCP socket Create a TCP socket 1. 1. Create a TCP socket Create a TCP socket 1. 1. Bind socket to a port Bind socket to a port 2. 2. Establish connection Establish connection 2. 2. Set socket to listen Set socket to listen Communicate 3. Communicate 3. 3. 3. Repeatedly: Repeatedly: Close the connection 4. Close the connection 4. 4. 4. Accept new connection Accept new connection a. a. Communicate Communicate b. b. Close the connection Close the connection c. c.

Recommend


More recommend