Computer Network Architecture and Programming

Slides:



Advertisements
Similar presentations
Multicasting© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Advertisements

Socket Options. abstraction Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option.
Programming with TCP – I
Out-of-Band Data© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
I/O Models Satish Krishnan. I/O Models Blocking I/O Non-blocking I/O I/O Multiplexing Signal driven I/O Asynchronous I/O.
Transport Layer – TCP (Part1) Dr. Sanjay P. Ahuja, Ph.D. Fidelity National Financial Distinguished Professor of CIS School of Computing, UNF.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Multicasting - Part II© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
TCP© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets IMGD Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Socket Options Jari Kellokoski. Introduction So far we have discovered some basic usage of socket With socket options we control sockets more suitable.
1 I/O Multiplexing Computer Network Programming. 2 Input from multiple sources Process keyboard sockets file A process may have multiple sources of input.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Advanced UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
UDP© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Socket option Getsockopt ou setsockopt fcntl ioctl.
Broadcast and Multicast. Unicast Host 2Host 1 Broadcast Packet received by every host on network (including the sender!)
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
Socket Options –Four functions define the API for ‘get’ and ‘setting’ socket options. We will cover those aspects which address generic, IPv4, TCP UDP.
6.1. Transport Control Protocol (TCP) It is the most widely used transport protocol in the world. Provides reliable end to end connection between two hosts.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
TCP1 Transmission Control Protocol (TCP). TCP2 Outline Transmission Control Protocol.
Chapter 12 Transmission Control Protocol (TCP)
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Chapter 2 Applications and Layered Architectures Sockets.
7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞.
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
More on Socket API. More on Socket I/O Functions  Scatter read and gather write.
Netprog: Advanced Sockets Programming1 Advanced Sockets Programming Ref: Chapter 7,11,21,22.
Advanced Sockets API-II Vinayak Jagtap
I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs.
1 Socket Options Ref: Chapter 7. 2 Socket Options Various attributes that are used to determine the behavior of sockets.Various attributes that are used.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Socket options: a summary –Tcp options, ip options and general socket options can be examined and sometimes modified. getsockopt, setsockopt routines Some.
I/O Multiplexing. What is I/O multiplexing? When an application needs to handle multiple I/O descriptors at the same time –E.g. file and socket descriptors,
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
1 Socket Options getsockopt and setsockopt functions Check options and obtain default values Generic socket options IPv4 socket options IPv6 socket options.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
I/O Multiplexing Chap 6. I/O Models  Blocking I/O Model  Nonblocking I/O Model  I/O Multiplexing Model  Signal Driven I/O Model  Asynchronous I/O.
TCP/IP Illustrated, Volume 1: The Protocols Chapter 6. ICMP: Internet Control Message Protocol ( 월 ) 김 철 환
Netprog: Client/Server Issues1 Issues in Client/Server Programming Refs: Chapter 27.
TCP/IP1 Address Resolution Protocol Internet uses IP address to recognize a computer. But IP address needs to be translated to physical address (NIC).
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
11 CS716 Advanced Computer Networks By Dr. Amir Qayyum.
Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun.
IPv4 socket option Level => IPPROTO_IP
I/O Multiplexing.
Socket Option.
Operating Systems Sockets ENCE 360.
Network Programming CSC- 341
Network Programming CSC- 341
Chapter 07. Socket Options.
Advanced Network Programming spring 2007
Advanced Sockets Programming
Socket options: a summary
Advanced UNIX programming
Presentation transcript:

Computer Network Architecture and Programming CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Socket Options Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

© Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 Outline Socket Options (Chapter 7) Introduction Checking for Options and default values Some Generic Socket Options TCP Socket Options Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Getting and Setting Options 1/2 Various attributes that are used to determine the behavior of sockets #include <sys/socket.h> int getsockopt (int sockfd, int level, int optname, void * optval, socklen_t *optlen); int setsockopt (int sockfd, int level, int optname, const void * optval, socklen_t optlen); Both return 0 if OK, -1 on error sockfd: an open socket descriptor level: code in the system that interprets the option (general socket code, or protocol-specific code) (SOL_SOCKET, IPPROTO_IP, IPPROTO_IPv6, IPPROTO_TCP are examples) optname: see page 193-figure 7.1, and page 194-figure 7.2 Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Getting and Setting Options 2/2 Some socket options examples (see table on page 193 and 194) Socket Level SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE, SO_BROADCAST, SO_REUSEADDR, SO_RESUEPORT IP Level IP_TTL, IPMULTICAST_IF, IPMUTLICAST_TTL, IP_MULTICAST_LOOP, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP TCP Level TCP_KEEPALIVE, TCP_MAXSEG, TCP_NODELAY Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Checking for socket Options Not all implementations support all socket options Source code in sockopt/checkopts.c Declares 4 different functions to handle the value for a given socket option SO_REUSEPORT can be undefined Have to surround with #ifdef SO_USELOOPBACK can be undefined Need to change the source code for our lab machines Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

© Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 Socket States The following socket options are inherited by a connected socket from the listening socket SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_RCVLOWAT, SO_SNDBUF, SO_SNDLOWAT, TCP_MAXSEG, and TCP_NODELAY To ensure one of the previous option is set for a connected socket, when 3WHS completes Set the option for the listening socket Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 1/13 SO_BROADCAST Enable or disable the ability of the process to send broadcast messages (only datagram socket : Ethernet, Token ring..) SO_DEBUG Kernel keep track of detailed information about all packets sent or received by TCP (only supported by TCP) SO_ERROR When error occurs on a socket, the protocol module in a BSD, kernel sets a variable named so_error for that socket (pending error) Process can obtain the value of so_error by fetching the SO_ERROR socket option Socket option can be fetched but not set Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 2/13 SO_KEEPALIVE When set for a TCP socket, and no data has been exchanged in either direction for two hours TCP automatically sends a keep-alive probe to the peer Peer must respond Peer responds with expected ACK  OK Peer responds with an RST  peer host has crashed and rebooted. Socket pending error is set to ECONNRESET and socket closed No repsonse from peer BSD TCPs send 8 additional probes, 75 seconds apart Give up if no response within 11 minutes and 15 seconds after first probe Socket pending error set to ETIMEDOUT (or set to ICMP error) See Figure 7.6 Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 3/13 SO_LINGER specify how the close function operates for a connection-oriented protocol (default: close returns immediately) struct linger{ int l_onoff; /* 0 = off, nonzero = on */ int l_linger; /*linger time : seconds*/ }; l_onoff = 0 : turn off , l_linger is ignored l_onoff = nonzero and l_linger is 0:TCP aborts the connection, discard any remaining data in send buffer. l_onoff = nonzero and l_linger is nonzero process waits until remaining data sent and ACKed, or until linger time expired If socket has been set non-blocking, it will not wait for the close to complete, even if linger time is nonzero Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 4/13 SO_LINGER client server write Close close returns immediately Data queued by TCP Application reads queued data and FIN close data FIN ACK of (data and FIN) ACK of data and FIN Default operation of close:it returns immediately Server host can crash before application reads data Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 5/13 SO_LINGER client server write Close Data queued by TCP Application reads queued data and FIN close data FIN ACK of (data and FIN) ACK of data and FIN close returns Close with SO_LINGER socket option set and l_linger a positive value Server host can crash before application reads data Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 6/13 SO_LINGER (making sure receiver reads the data) client server write Shutdown read block Data queued by TCP Application reads queued data and FIN close data FIN ACK of data and FIN read returns 0 Using shutdown (with 2nd argument SHUT_WR) to know that peer has received our data Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 7/13 SO_LINGER (making sure receiver reads the data  Application-level ACK) Please see Figure 7.12 for a summary of shutdown and SO_LINGER scenarios Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 8/13 SO_RCVBUF and SO_SNDBUF Change the default send-buffer, receive-buffer sizes Default TCP send and receive buffer size Older BSD implementations  4,096 bytes Newer  8,192-61,440 bytes Default UDP buffer size send 9,000bytes, receive 40,000 bytes SO_RCVBUF option must be set before connection is established (calling connect for a client) TCP socket buffer size should be at least 4 times the MSS Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 9/13 SO_RCVLOWAT and SO_SNDLOWAT Every socket has a receive low-water mark and send low-water mark (used by select function) Receive low-water mark Amount of data that must be in the socket receive buffer for select to return “readable” Default receive low-water mark : 1 for TCP and UDP Send low-water mark Amount of available space that must exist in the socket send buffer for select to return “writable” Default send low-water mark : 2048 for TCP UDP send buffer never changes (UDP does not keep a copy of datagram sent by application  see Figure 2.16 in section 2.11) Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 10/13 SO_RCVTIMEO and SO_SNDTIMEO Allows us to place a timeout on socket receives and sends. By default disabled Argument is a pointer to a timeval structure (same as select) Later, disable a timeout by setting its value to 0 (seconds and microseconds) See Figure 14.5 (source code is in advio/dgclitimeo2.c) Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 11/13 SO_RCVTIMEO and SO_SNDTIMEO struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; Setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); n = recvfrom (sockfd, recvline, MAXLINE, 0, NULL, NULL); if (n < 0) { if (errno == EWOULDBLOCK) { fprintf (stderr, "socket timeout\n"); continue; } else err_sys ("recvfrom error"); } Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 12/13 SO_REUSEADDR and SO_REUSEPORT Allow a listening server to start and bind its well known port even if previously established connections exist that use this port as their local port Possible scenario Listening server started connection accepted a child process is spawned listening server terminates (child is still there) listening server is restarted Call to bind will fail because listening server is trying to bind a port that is part of an existing connection Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

Some Generic Socket Options 13/13 SO_REUSEADDR and SO_REUSEPORT Allow multiple instance of the same server to be started on the same port, as long as each instance binds a different local IP address Common for a site hosting multiple HTTP servers while using IP alias technique TCP does not allow completely duplicate bindings across multiple servers (same IP address and port) What about TCP clients? (see exercise 7.4) Allow a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address Allow completely duplicate bindings : multicasting 4.4 BSD introduced SO_REUSEPORT socket option Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

© Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 TCP Socket Options 1/2 SO_MAXSEG Set or get the MSS for a TCP connection Often is the MSS announced by the other end with its SYN MSS can change during the lifetime of the connection if TCP supports path MTU discovery Setting the socket option is not available on all systems 4.4BSD limits the application to decreasing the value Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006

© Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 TCP Socket Options 2/2 SO_NODELAY If set, disables TCP’s Nagle Algorithm (by default enabled) Nagle algorithm aims to reduce the number of small packets on a WAN If a given connection has outstanding data, then no small packets will be sent on the connection (small means smaller than the MSS) Common generators of small packets are Rlogin and Telnet clients (normally send each keystroke as a separate packet) Might be OK on a LAN, but problematic on a WAN because of RTT Socket Options © Dr. Ayman Abdel-Hamid, CS4254 Spring 2006