tcp client server example
play

TCP Client/Server Example CSCE 515: Computer Network Programming - PDF document

TCP Client/Server Example CSCE 515: Computer Network Programming fgets writen read stdin TCP TCP ------ Select stdout client server fputs readline writen Wenyuan Xu Department of Computer Science and Engineering University of


  1. TCP Client/Server Example CSCE 515: Computer Network Programming fgets writen read stdin TCP TCP ------ Select stdout client server fputs readline writen Wenyuan Xu Department of Computer Science and Engineering University of South Carolina 2007 CSCE515 – Computer Network Programming Echo client Echo client (cont.) int sockfd; str_cli(FILE *fp, int sockfd) { struct sockaddr_in server; char sendline[MAXLINE], recvline[MAXLINE]; socket = Socket(AF_INET, SCOK_STREAM, 0); while (Fgets(sendline, MACLINE,fp) != NULL) { server.sin_family = AF_INET; server.sin_port = htons(SERV_PORT); Writen(sockfd, sendline, strlen(sendline)); Inet_pton(AF_INET, argv[1], &server.sin_addr); if (Readline(sockfd, recvline, MAXLINE)) == 0 Connect(sockfd,(sockaddr *)&server,sizeof(servaddr)); err_quit(“str_cli: server terminated prematurely”); str_cli(stdin, sockfd); Fputs(recvline, stdout); } exit(0); } 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming TCP Termination Problem Server Client � Server sends FIN � Client TCP responds with ACK “I have no more FIN 1 FIN data for you” � After that: SN= X FIN_WAIT_1 SN= X � Server: FIN_WAIT2 “ OK, I understand you � Client: CLOSE_WAIT ACK= X +1 2 are done sending .” ACK= X +1 CLOSE_WAIT FIN_WAIT_2 � The client process is blocked in fgets when FIN ... arrives on the socket “OK – one 3 more line”. Data � The client is working with two descriptor, while it should Data not block on one of them: “I’ve quit � Socket already!” 4 RST RST � User input “oops, something wrong” 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming

  2. I/O Multiplexing Example - generic TCP client � We often need to be able to monitor � Input from standard input should be sent multiple descriptors: to a TCP socket. � a generic TCP client (like telnet) � Input from a TCP socket should be sent to � A server that handles both TCP and UDP standard output. � Client that can make multiple concurrent requests (browser?). � How do we know when to check for input from each source? 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming Generic TCP Client Options � Use nonblocking I/O. � use fcntl() to set O_NONBLOCK � Use alarm and signal handler to interrupt TCP SOCKET STDIN slow system calls. � Use multiple processes/threads. � Use functions that support checking of STDOUT multiple input sources at the same time. 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming while (! done) { Non blocking I/O if ( (n=read(STDIN_FILENO,…)<0)) � use fcntl() to set O_NONBLOCK : if (errno != EWOULDBLOCK) /* ERROR */ int flags; else write(tcpsock,…) flags = fcntl(sock,F_GETFL,0); fcntl(sock,F_SETFL,flags | O_NONBLOCK); if ( (n=read(tcpsock,…)<0)) if (errno != EWOULDBLOCK) � Now calls to read() (and other system calls) /* ERROR */ will return an error and set errno to else write(STDOUT_FILENO,…) EWOULDBLOCK . } 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming

  3. The problem with nonblocking I/O Using alarms � Using blocking I/O allows the Operating System to put your process to sleep when signal(SIGALRM, sig_alrm); nothing is happening (no input). Once alarm(MAX_TIME); read(STDIN_FILENO,…); input arrives, the OS will wake up your ... process and read() (or whatever) will A function you write return. signal(SIGALRM, sig_alrm); � With nonblocking I/O, the process will alarm(MAX_TIME); chew up all available processor time!!! read(tcpsock,…); ... 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming Select() Alarming Problem � The select() system call allows us to use blocking I/O on a set of descriptors (file, What will happen to the response time ? socket, …). � For example, we can ask select to notify What is the ‘right’ value for MAX_TIME? us when data is available for reading on either STDIN or a TCP socket. 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming select() Select() int select( int maxfd, � Return when fd_set *readset, � Any of the descriptors in the set {1,4,5} are fd_set *writeset, ready for reading fd_set *excepset, � Any of the descriptors in the set {2,7} are const struct timeval *timeout); ready for writing maxfd : highest number assigned to a descriptor. � Any of the descriptors in the set {1,4} have an exception condition pending readset : set of descriptors we want to read from. � Specify what descriptors we are interested writeset : set of descriptors we want to write to. in and how long to wait excepset : set of descriptors to watch for exceptions. timeout : maximum time select should wait 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming

  4. fd_set struct timeval struct timeval { � Implementation is not important long tv_sec; /* seconds */ � Operations you can use with an fd_set : long tv_usec; /* microseconds */ } void FD_ZERO( fd_set *fdset); void FD_SET( int fd, fd_set *fdset); struct timeval max = {1,0}; void FD_CLR( int fd, fd_set *fdset); struct timeval forever = NULL; int FD_ISSET( int fd, fd_set *fdset); struct timeval polling = {0,0} 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming Using select() Errors -- errno � Create fd_set � EBADF � An invalid file descriptor was given in one of the sets. � Clear the whole thing with FD_ZERO � EINTR � Add each descriptor you want to watch � A non blocked signal was caught. using FD_SET . � EINVAL � Call select � n is negative or the value contained within timeout is invalid. � when select returns, use FD_ISSET � ENOMEM to see if I/O is possible on each � select was unable to allocate memory for internal descriptor. tables. 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming shutdown() shutdown()vs close() int shutdown( int sockfd , int howto ); � Reference counter: � close() decrements the descriptor’s reference count and close the socket only if the count sockfd is the TCP socket reaches 0. howto: � shutdown() initiate TCP connection termination sequence regardless of the reference count. � SHUT_RD: close the read half of the connection � SHUT_WR: close the write half of the connection � SHUT_RDWR: close both the read and write half of the � Directions: connection. � close() terminate both directions of data transfer, reading and writing. shutdown() returns -1 on error (otherwise 0). � shutdown() can close one-half of the TCP connection, either reading or writing. 2007 CSCE515 – Computer Network Programming 2007 CSCE515 – Computer Network Programming

  5. Assignment & Next time � Reading: � UNP 5.12, 6.3-6.9** � Next Lecture: � thread 2007 CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming

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