Download presentation
Presentation is loading. Please wait.
Published byGabriella Watson Modified over 8 years ago
1
@Yuan Xue (yuan.xue@vanderbilt.edu) CS 283Computer Networks Spring 2013 Instructor: Yuan Xue
2
@Yuan Xue (yuan.xue@vanderbilt.edu) Socket Programming (II)
3
@Yuan Xue (yuan.xue@vanderbilt.edu) TCP-based Socket Overview Check out the code TCPClient_sproc.c and TCPServer_sproc.c
4
@Yuan Xue (yuan.xue@vanderbilt.edu) Connection setup: listen & accept Called by passive participant int status = listen(sock, queuelen); status: 0 if listening, -1 if error sock: integer, socket descriptor queuelen: integer, # of active participants that can “wait” for a connection listen is non-blocking: returns immediately int s = accept(sock, &name, &namelen); s: integer, the new socket (used for data-transfer) sock: integer, the orig. socket (being listened on) name: struct sockaddr, address of the active participant namelen: sizeof(name): value/result parameter must be set appropriately before call adjusted by OS upon return accept is blocking: waits for connection before returning
5
@Yuan Xue (yuan.xue@vanderbilt.edu) Connection setup: connect Called by active participant int status = connect(sock, &name, namelen); status: 0 if successful connect, -1 otherwise sock: integer, socket to be used in connection name: struct sockaddr: address of passive participant namelen: integer, sizeof(name) connect is blocking
6
@Yuan Xue (yuan.xue@vanderbilt.edu) Sending / Receiving Data With a connection (SOCK_STREAM): int count = send(sock, &buf, len, flags); count: # bytes transmitted (-1 if error) buf: char[], buffer to be transmitted len: integer, length of buffer (in bytes) to transmit flags: integer, special options, usually just 0 int count = recv(sock, &buf, len, flags); count: # bytes received (-1 if error) buf: void[], stores received bytes len: # bytes received flags: integer, special options, usually just 0 Calls are blocking [returns only after data is sent (to socket buf) / received] Sometimes recv() may return 0 (details in the code)
7
@Yuan Xue (yuan.xue@vanderbilt.edu) close When finished using a socket, the socket should be closed: status = close(s); status: 0 if successful, -1 if error s: the file descriptor (socket being closed) Closing a socket closes a connection (for SOCK_STREAM) frees up the port used by the socket
8
@Yuan Xue (yuan.xue@vanderbilt.edu) How TCP socket works TCP socket identified by 4-tuple: source IP address source port number dest IP address dest port number recv host uses all four values to direct segment to appropriate socket
9
@Yuan Xue (yuan.xue@vanderbilt.edu) How TCP socket works Client IP:B P1 client IP: A P1P2 childfd P4 server IP: C SP: 9157 DP: 80 SP: 9157 DP: 80 childfd P5 childfd P6 P3 D-IP:C S-IP: A D-IP:C S-IP: B SP: 5775 DP: 80 D-IP:C S-IP: B parentfd P Port: 80 IP: C
10
@Yuan Xue (yuan.xue@vanderbilt.edu) Dealing with blocking calls Many of the functions we saw block until a certain event accept: until a connection comes in connect: until the connection is established recv, recvfrom: until a packet (of data) is received send, sendto: until data is pushed into socket’s buffer Q: why not until received? For simple programs, blocking is convenient What about more complex programs? multiple connections simultaneous sends and receives simultaneously doing non-networking processing
11
@Yuan Xue (yuan.xue@vanderbilt.edu) Dealing w/ blocking (cont’d) Options: create multi-process or multi-threaded code Use setsockopt function call May not be supported in all systems. turn off the blocking feature (e.g., using the fcntl file-descriptor control function) use the select function call.
12
@Yuan Xue (yuan.xue@vanderbilt.edu) Multi-Process Programming in Unix fork() is used to create processes. no arguments returns a process ID. After a new child process is created, both processes will execute the next instruction following the fork() system call. To distinguish the parent from the child, test the returned value of fork(): a negative value the creation of a child process was unsuccessful. zero -> the newly created child process. a positive value the process ID of the child process, to the parent. T getpid() can retrieve the ID assigned to this process. Check out code in TCP/MultipleProcesses
13
@Yuan Xue (yuan.xue@vanderbilt.edu) Multi-Process Programming in Unix Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces. As file descriptor in Unix is local to each process, socket fd can be used/recycled in the parent process
14
@Yuan Xue (yuan.xue@vanderbilt.edu) Multi-Thread Programming in Unix pthread_create (thread,attr,start_routine,arg) creates a new thread and makes it executable. thread: An identifier for the new thread returned by the subroutine. attr: An attribute object that may be used to set thread attributes. You can specify a thread attributes object, or NULL for the default values. start_routine: the C routine that the thread will execute once it is created. arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed. Check out code in TCP/MultipleThread
15
@Yuan Xue (yuan.xue@vanderbilt.edu) select function call select() function can identify sockets that are “ready for use”: calls involving that socket will return immediately. int status = select(nfds, &readfds, &writefds, &exceptfds, &timeout); status: # of ready objects, -1 if error nfds: 1 + largest file descriptor to check readfds: list of descriptors to check if read-ready writefds: list of descriptors to check if write-ready exceptfds: list of descriptors to check if an exception is registered timeout: time after which select returns, even if nothing ready – can be 0 or (point timeout parameter to NULL for )
16
@Yuan Xue (yuan.xue@vanderbilt.edu) To be used with select: Recall select uses a structure, struct fd_set it is just a bit-vector if bit i is set in [readfds, writefds, exceptfds], select will check if file descriptor (i.e. socket) i is ready for [reading, writing, exception] Before calling select: FD_ZERO(&fdvar): clears the structure FD_SET(i, &fdvar): to check file desc. i After calling select: int FD_ISSET(i, &fdvar): boolean returns TRUE iff i is “ready”
17
@Yuan Xue (yuan.xue@vanderbilt.edu) Recvfrom_wtimeout (usage of select) int recvfrom_wtimeout() { fd_set read_fds; int iof = -1; FD_ZERO(&read_fds); //clears out the fd_set readset fds FD_SET(sockfd, &read_fds); //put sockfd to fd_set fds, int result = select(sockfd+1, &read_fds, NULL, NULL, timeoutp); if (result < 0) error("select"); if (result ==0) return 0; //timeout is triggered if (!FD_ISSET(sockfd, &read_fds)) //check sockfd is ready for receiving error("either select or FD_ISSET"); if ((iof = fcntl(sockfd, F_GETFL, 0)) == -1) // Set non-blocking mode error("fcntl F_GETFL"); //F_GETFL: Get the file access mode and the file status flags if (fcntl(sockfd, F_SETFL, iof | O_NONBLOCK) == -1) error("fcntl F_SETFL"); //F_SETFL: Set the file status flags; adding O_NONBLOCK int numbytes = recv(sockfd, buffer, len, flags); // receive in non-blocking mode if (fcntl(sockfd, F_SETFL, iof) == -1) error("fcntl F_SETFL to original"); //recover return numbytes; }
18
@Yuan Xue (yuan.xue@vanderbilt.edu) A few words about Lab 2 Transfer a big file Determine the end of the file (when transfer is completed?) Data Transfer in UDP Pace out the sendto() using sleep() or usleep() – recommended Receiver determines the end of the file transfer?
19
@Yuan Xue (yuan.xue@vanderbilt.edu) Final Notes Make sure to include the header files that define functions being used Check the following website for additional information and more sample code http://www.beej.us/guide/bgnet/output/html/multipa ge/index.html http://www.beej.us/guide/bgnet/output/html/multipa ge/index.html http://www.cs.utah.edu/~swalton/listings/sockets/pro grams/ http://www.cs.utah.edu/~swalton/listings/sockets/pro grams/ http://www.cs.uic.edu/~troy/spring05/cs450/sockets /socket.html http://www.cs.uic.edu/~troy/spring05/cs450/sockets /socket.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.