Download presentation
Presentation is loading. Please wait.
1
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007
2
CS 360 – Spring 2007 Pacific University 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
CS 360 – Spring 2007 Pacific University Socket Libraries (Unix/Linux) This is all the same, just like UDP! #include // data types #include // socket interface #include // 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
CS 360 – Spring 2007 Pacific University API Usage: Call Sequences TCP over IP (connection-based): socket() bind() listen() accept() recv()/send() close() server socket() connect() send()/recv() close() client
5
CS 360 – Spring 2007 Pacific University 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
CS 360 – Spring 2007 Pacific University 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
CS 360 – Spring 2007 Pacific University 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 –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
CS 360 – Spring 2007 Pacific University 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
CS 360 – Spring 2007 Pacific University 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
CS 360 – Spring 2007 Pacific University 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
CS 360 – Spring 2007 Pacific University Suggested Road Map 1)Convert Project #1 to TCP 2)Add the calculation stream 3)Add named values 4)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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.