tcp ip sockets in c computer chat
play

TCP/IP Sockets in C Computer Chat How do we make computers talk? - PDF document

TCP/IP Sockets in C Computer Chat How do we make computers talk? Great and inexpensive book: How are they interconnected? Michael J. Donahoo Kenneth L. Calvert Internet Protocol (IP) Morgan Kaufmann Publisher $14.95 Paperback


  1. TCP/IP Sockets in C Computer Chat � How do we make computers talk? Great and inexpensive book: � How are they interconnected? Michael J. Donahoo Kenneth L. Calvert Internet Protocol (IP) Morgan Kaufmann Publisher $14.95 Paperback Internet Protocol (IP) IP Address � Datagram (packet) protocol � 32-bit identifier (IPv4, IPv6=128 bits) � Best-effort service � Dotted-quad: 192.118.56.25 � Loss � www.mkp.com -> 167.208.101.28 � Reordering � Identifies a host interface (not a host) � Duplication � Delay � Host-to-host delivery 192.18.22.13 209.134.16.123 Transport Protocols Ports Best-effort not sufficient! Identifying the ultimate destination � IP addresses identify hosts � Add services on top of IP � User Datagram Protocol (UDP) � Host has many applications � Data checksum � Ports (16-bit identifier) 1-65,535 � Best-effort Application WWW E-mail Telnet � Transmission Control Protocol (TCP) � Data checksum Port 80 25 23 � Reliable byte-stream delivery � Flow and congestion control 192.18.22.13 1

  2. Socket Sockets � Identified by protocol and local/remote How does one speak TCP/IP? address/port � Applications may refer to many sockets � Sockets accessed by many applications � Sockets provides interface to TCP/IP � Generic interface for many protocols TCP/IP Sockets Specifying Addresses struct sockaddr � mySock = socket(family, type, protocol); � { Generic unsigned short sa_family; /* Address family (e.g., AF_INET) */ � TCP/IP-specific sockets char sa_data[14]; /* Protocol-specific address information */ }; Protocol Family Type struct sockaddr_in � TCP SOCK_STREAM IPPROTO_TCP { PF_INET unsigned short sin_family;/* Internet protocol (AF_INET) */ UDP SOCK_DGRAM IPPROTO_UDP unsigned short sin_port; /* Port (16-bits) */ IP Specific struct in_addr sin_addr; /* Internet address (32-bits) */ char sin_zero[8]; /* Not used */ � Socket reference }; struct in_addr � File (socket) descriptor in UNIX { unsigned long s_addr; /* Internet address (32-bits) */ }; Clients and Servers TCP Client/Server Interaction � 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 Create a TCP socket 1. “Hi, Bob. I’m Jane” 1. Assign a port to socket Establish connection 2. 2. Set socket to listen Communicate 3. 3. “Nice to meet you, Jane.” Repeatedly: Close the connection 4. 4. Accept new connection a. Communicate � Server: Passively waits to respond b. Close the connection c. 2

  3. 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 Create a TCP socket 1. Create a TCP socket 1. 1. 1. Bind socket to a port Bind socket to a port Establish connection 2. Establish connection 2. 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 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 Create a TCP socket 1. Create a TCP socket 1. 1. 1. Bind socket to a port Bind socket to a port 2. Establish connection Establish connection 2. 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 Create a TCP socket 1. Create a TCP socket 1. 1. 1. Bind socket to a port Bind socket to a port Establish connection 2. Establish connection 2. 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. 3

  4. 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 Create a TCP socket 1. Create a TCP socket 1. 1. 1. Bind socket to a port Bind socket to a port Establish connection 2. Establish connection 2. 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 echoStringLen = strlen(echoString); /* Determine input length */ /* Send the string to the server */ if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) if (send(sock, echoString, echoStringLen, 0) != echoStringLen) DieWithError("accept() failed"); DieWithError("send() sent a different number of bytes than expected"); Server Server Client Client Create a TCP socket Create a TCP socket Create a TCP socket 1. Create a TCP socket 1. 1. 1. Bind socket to a port Bind socket to a port 2. Establish connection Establish connection 2. 2. 2. Set socket to listen Set socket to listen 3. Communicate 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 /* Receive message from client */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) close(sock); close(clntSocket) DieWithError("recv() failed"); Server Server Client Client Create a TCP socket Create a TCP socket Create a TCP socket 1. Create a TCP socket 1. 1. 1. Bind socket to a port Bind socket to a port Establish connection 2. Establish connection 2. 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

  5. TCP Tidbits Closing a Connection � Client knows server address and port � close() used to delimit communication � No correlation between send() and recv() � Analogous to EOF Client Server Client Server send(“Hello Bob”) send( string ) recv( buffer ) recv() -> “Hello ” while (not received entire string) while(client has not closed connection) recv() -> “Bob” recv( buffer ) send( buffer ) send(“Hi ”) send( buffer ) recv( buffer ) send(“Jane”) recv() -> “Hi Jane” close( socket ) close( client socket ) TCP/IP Byte Transport � TCP/IP protocols transports bytes Constructing Messages Application Application byte stream byte stream Here are some I’ll pass …beyond simple strings bytes. I don’t these to TCP/IP know what TCP/IP the app. It they mean. knows what to do. � Application protocol provides semantics Application Protocol Primitive Types � Encode information in bytes � String � Character encoding: ASCII, Unicode, UTF � Sender and receiver must agree on semantics � Delimit: length vs. termination character � Data encoding 0 77 0 111 0 109 0 10 � Primitive types: strings, integers, and etc. � Composed types: message with fields M o m \n 3 77 111 109 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend