1 / 11

TCP

TCP. section 6.5 (Read this section!) 27 Feb 2007. TCP. Transmission Control Protocol (RFC 793/and many more) Reliable, connection-based protocol byte stream max packet size: 64 KB (1460 bytes is often the practical limit). Tanenbaum, Figure 6-28, page 534. Socket Libraries (Unix/Linux).

carla-sloan
Télécharger la présentation

TCP

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. TCP section 6.5 (Read this section!) 27 Feb 2007

  2. TCP • Transmission Control Protocol (RFC 793/and many more) • Reliable, connection-based protocol • byte stream • max packet size: 64 KB (1460 bytes is often the practical limit) Tanenbaum, Figure 6-28, page 534

  3. Socket Libraries (Unix/Linux) This is all the same, just like UDP! #include <sys/types.h> // data types #include <sys/socket.h> // socket interface #include <netinet/in.h> // Internet interface • The socket is the common Unix interface to the network • a socket represents an end point for network communication • Berkeley Software Distribution socket API • 4.2 BSD Unix • most OSes now provide a BSD socket interface for networking • Microsoft Windows almost provides it • defacto standard • a socket is represented by an int

  4. API Usage: Call Sequences • TCP over IP (connection-based): socket() bind() listen() accept() recv()/send() close() server socket() connect() send()/recv() close() client

  5. Socket library functions • TCP over IP • domain (protocol family): PF_INET • type: SOCK_STREAM • protocol: IPPROTO_TCP • see /etc/protocols for a list • address family: AF_INET int socket(int domain, int type, int protocol) int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen) • actually use struct sockaddr_in for IP networking

  6. Socket library functions int listen(int sockfd, int backlog) • backlog defines the length of the queue of pending connections • If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) • addr is the address of the incoming connection • really use a struct sockaddr_in* ssize_t send(int sockfd, const void *buf, size_t len, int flags); • flags sets some options ssize_t recv(int sockfd, void *buf, size_t len, int flags); • flags sets some options: MSG_WAITALL, etc.

  7. Socket library functions int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); • again, use struct sockaddr_in for IP connections • this struct is filled with address information specifying the destination of the data int close(int sockfd) • just like closing a file int setsockopt(int sockfd, int level, int ptname, const void *optval,socklen_t optlen) - #include <netinet/tcp.h> • manipulate the options associated with a socket • level: SOL_TCP for options to TCP • man socket for full list of options • TCP_NODELAY: send data immediately

  8. Code: Client! • page 490 has a TCP example • it uses read/write rather than recv/send. why? struct sockaddr_in sAddr; int addrLen = sizeof(struct sockaddr_in); int sock; int connectStatus; char buf[1024]; int bufLen = 1024; sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); memset(&sAddr, 0, addrLen); sAddr.sin_family = AF_INET; sAddr.sin_port = htons(PORT); sAddr.sin_addr.s_addr = inet_addr(“64.59.233.238”); connectStatus = connect(sock, &sAddr,addrLen); if( send(sock, (void)*buf, bufLen, 0) == -1) { } if( recv(sock, (void)*buf, bufLen, MSG_WAITALL) == -1) { } close(sock);

  9. Code: Server! struct sockaddr_in sAddr; int addrLen = sizeof(struct sockaddr_in); int listenSock, dataSock, status, one = 1; char buf[1024]; int bufLen = 1024; listenSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); status = setsockopt(listenSocket, SOL_TCP, TCP_NODELAY, &one, sizeof(int)); memset(&sAddr, 0, addrLen); sAddr.sin_family = AF_INET; sAddr.sin_port = htons(PORT); sAddr.sin_addr.s_addr = NULL; status = bind(listenSock, &sAddr, addrLen); status = listen(listenSocket, 10); dataSock = accept(listenSocket, &sAddr, &addrLen); status = recv(dataSock, (void)*buf, bufLen, MSG_WAITALL); status = send(dataSock, (void)*buf, bufLen, 0); close(dataSock); close(listenSock);

  10. Your Project • A stream of MathPackets • calculation stream • Server needs to be multithreaded • one thread per connection • Server stores intermediate value for each stream • perform calculations as the packets arrive • A client can set a named value • 4 character name (exactly) • integer • accessible by all clients of the server

  11. Suggested Road Map • Convert Project #1 to TCP • Add the calculation stream • Add named values • Add threads to the server • Plan ahead! • think about threads as you work with the server • build a nice data structure in step three • use mutexes efficiently in step 4 • commit to Subversion often! • I will be checking through your repository • name your project PUNetID_cs36007_PA3 • Start Early!

More Related