1 / 21

More on Socket API

More on Socket API. How to Place Timeouts on Sockets (1). Using SIGALRM signal. Connection timeout 기간의 축소. Response timeout. lib/connect_timeo.c. advio/dgclitimeo3.c. alarm() 은 초 단위 setitimer() 는 micro sec 단위 설정 가능 ( 실제는 msec 단위로 동작 ). How to Place Timeouts on Sockets (2).

Télécharger la présentation

More on Socket API

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. More on Socket API

  2. How to Place Timeouts on Sockets (1) • Using SIGALRM signal Connection timeout 기간의 축소 Response timeout lib/connect_timeo.c advio/dgclitimeo3.c alarm()은 초 단위 setitimer()는 micro sec 단위 설정 가능(실제는 msec 단위로 동작)

  3. How to Place Timeouts on Sockets (2) • Using select with timeout advio/dgclitimeo1.c lib/readable_timeo.c

  4. How to Place Timeouts on Sockets (3) • Using SO_RCVTIMEO and SO_SNDTIMEO socket options • Caution: timeout applies to all I/O operations for the socket descriptor advio/dgclitimeo2.c

  5. More on Socket I/O Functions • recv and send (only for sockets) • Scatter read and gather write

  6. More Advanced Socket I/O Functions

  7. Ancillary data - cmsghdr Structure

  8. Socket I/O Summary

  9. Socket and Standard I/O • Buffering in Standard I/O library • fully buffered: all stream except for terminal devices • line buffered : terminal devices • unbuffered: stderr • Caution • Socket에 standard I/O functions(fgets, fputs)를 쓰면 fully buffered됨

  10. Socket Options

  11. getsockopt and setsockopt Functions

  12. Generic Socket Options(1) • SO_BROADCAST • Enable the ability of the process to send broadcast messages • Application must set this option before broadcasting • SO_DEBUG • Kernel keeps track of detailed information about all the packets sent or received by TCP for the socket • SO_DONTROUTE • Bypass the normal routing mechanism • The packet is directed to the appropriate local interface. If the local interface cannot be determined (i.e. destination is not on the same network), ENETUNREACH is returned. • SO_ERROR • Get the value of pending error • Socket에 error가 발생하면, so_error (pending error)에 먼저 setting하고 나서 errno에 setting함

  13. Generic Socket Options(2) • SO_KEEPALIVE • Enforce TCP to send a keepalive probe automatically to the peer if no data has been exchanged for 2 hours (to detect if the peer host crashes) • Peer’s response is either • ACK: everything is OK (application is not notified) • RST: the peer host has crashed and rebooted. Socket’s pending error = ECONNRESET and socket is closed • No response: send 8 additional probes, 75 sec apart (totally, 11mim and 15sec). Listen the peer’s response. • Still no response  ETIMEOUT and socket is closed • Receives ICMP error, EHOSTUNREACH or … • To shorten probe period • Use TCP_KEEPALIVE option (Not all implementation support this option). But, this will affect all sockets on the host. • Or, implement your own heartbeat mechanism.

  14. Ways to Detect Various TCP Condition

  15. Default Operation of close • Any data remaining in the socket send buffer is sent. Close returns immediately without receiving ACK. • Client cannot know whether the data has been delivered correctly to the server !!!

  16. Generic Socket Options(3) • SO_LINGER: linger when the socket is closed • Data type for option value • If l_onoff = 0, default close(). i.e. return immediately, but the remaining data is delivered to the peer • else • linger when the socket is closed • If l_linger==0, TCP aborts the connection. i.e. TCP discards any data still remaining in the socket send buffer and sends an RST to the peer. • else linger when the socket is closed. Any data still remaining in the socket send buffer is sent. But, if linger time expires, return EWOULDBLOCK and any remaining data is discarded.

  17. Using shutdown to know that peer has received data

  18. Generic Socket Options(4) • SO_OOBINLINE • OOB data will be placed in the normal input queue • SO_RCVBUF and SO_SNDBUF • Set or get socket send buffer size / socket receive buffer size • TCP: • advertise receive buffer(window) to the peer • sliding window mechanism(flow control) • buffer size  3 * MSS • UDP: typical send buffer size  9000, receive buffer size  40,000, • If data received > available receive buffer, datagram is discarded • Capacity of full-duplex pipe (bandwidth-delay product) • bandwidth * RTT (<= 64KB) • SO_RCVLOWAT, SO_SNDLOWAT: socket low water mark • data in socket receive buffer >= SO_RCVLOWAT  readable • available space in socket send buffer >= SO_SNDLOWAT  writable • SO_RCVLOWAT = 1, SO_SNDLOWAT = 2048 by default • SO_RCVTIMEO, SO_SNDTIMEO: socket time out for receiving and sending message

  19. Generic Socket Options(6) • SO_REUSEADDR: reuse the established port • Allows a listening server to start and bind its well-known port even if previously established connections exist that use this port • Allows multiple instances of the same server to be started on the same port as long as each instance binds a different local IP address • hosting multiple HTTP servers using IP alias technique • Allows a single process to bind the same port to multiple socket as long as each bind specifies a different local IP address • Allows completely duplicate bindings when same IP address and port are already bound to another socket. • Used with multicasting to allow the same application to be run multiple times on the same host when the SO_REUSEPORT option is not supported

  20. TCP Socket Options • TCP_KEEPALIVE: specifies the idle time in seconds for the connection before TCP starts sending keepalive probes • 7200 sec (2 hours), default • Effective only when SO_KEEPALIVE socket option is enabled • TCP_MAXSEG: fetch or set MSS for TCP connection • TCP_NODELAY • e.g) rlogin, telnet • Nagle algorithm / delayed algorithm disabled • TCP’s Nagle algorithm • No small packet( < MSS ) will not be sent until the existing data is ACKed • TCP’s delayed ACK algorithm • piggyback: TCP sends an ACK after some small amount of time( 50 ~ 200msec)

  21. Nagle Algorithm Enabled No small packet( < MSS ) will not be sent until the existing data is ACKed Nagle Algorithm Disabled Nagle Algorithm Enabled/Disabled

More Related