Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Similar documents
Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Network programming(ii) Lenuta Alboaie

UNIT IV- SOCKETS Part A

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Elementary TCP Sockets

Introduction! Overview! Signal-driven I/O for Sockets! Two different UDP servers!

Multicast on a LAN 3/4. Multicast on a LAN 2/4. Multicast on a WAN. Multicast on a LAN 4/4

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

CSE 124 Discussion Section Sockets Programming 10/10/17

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

EEC-484/584 Computer Networks

STUDY OF SOCKET PROGRAMMING

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

Network Programming. Multicast on a LAN 1/4. Sending & Receiving Multicast Messages. Multicasting II. Dr. Thaier Hayajneh

Group-A Assignment No. 6

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Socket Programming for TCP and UDP

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF MCA QUESTION BANK UNIT 1

Chapter 8: I/O functions & socket options

Overview. Daemon processes and advanced I/O. Source: Chapters 13&14 of Stevens book

Any of the descriptors in the set {1, 4} have an exception condition pending

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

UDP CONNECT TO A SERVER

UNIT IV - SOCKET OPTIONS, ELEMENTARY UDP SOCKETS

Lecture 10 Overview!

The User Datagram Protocol

Oral. Total. Dated Sign (2) (5) (3) (2)

IPv4 and ipv6 INTEROPERABILITY

Redesde Computadores(RCOMP)

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

Ports under 1024 are often considered special, and usually require special OS privileges to use.

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

VALLIAMMAI ENGINEERING COLLEGE. SRM Nagar, Kattankulathur QUESTION BANK

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Socket Programming(2/2)

MARIA COLLEGE OF ENGINEERING AND TECHNOLOGY, ATTOOR DEPARTMENT OF INFORMATION TECHNOLOGY NETWORK PROGRAMMING MANAGEMENT 2 MARKS QUESTIONS & ANSWERS

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

TCP: Three-way handshake

Contents. Part 1. Introduction and TCP/IP 1. Foreword Preface. xix. I ntroduction 31

Chapter 6. The Transport Layer. Transport Layer 3-1

CSE 333 Section 8 - Client-Side Networking

User Datagram Protocol

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Socket programming in C

CS 3516: Computer Networks

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Linux Kernel Application Interface

Standards / Extensions C or C++ Dependencies POSIX.1 XPG4 XPG4.2 Single UNIX Specification, Version 3

Network Socket Programming - 3 BUPT/QMUL

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Network Communication

Network programming(i) Lenuta Alboaie

Network Socket Programming - 3 BUPT/QMUL

UNIX Sockets. COS 461 Precept 1

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Tutorial on Socket Programming

Lenuta Alboaie Computer Networks

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

File I/0. Advanced Programming in the UNIX Environment

Εργαστήριο 9 I/O Multiplexing

Sockets 15H2. Inshik Song

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

A. Basic Function Calls for Network Communications

(Refer Slide Time: 1:09)

Unix Network Programming

Introduction for SPI mapping Service Discovery Interoperability Testing. 20, Sep PWG Fumio Nagasaka

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS4700/CS5700 Fundamentals of Computer Networking

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

NETWORK PROGRAMMING. Ipv4 and Ipv6 interoperability

Networked Applications: Sockets. End System: Computer on the Net

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Introduction to Client-Server Model

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

Computer Networks Prof. Ashok K. Agrawala

A Client-Server Exchange

CS 351 Week 15. Course Review

What s an API? Do we need standardization?

SOCKETS: INTRODUCTION

Vision Components. The Smart Camera People. VCRT 5.0 TCP/ IP Functions. TCP/ IP Operation System Functions

Introduction to Socket Programming

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Introduction to Network Programming in C/C++

SOCKET. Valerio Di Valerio

Transcription:

Lecture 5 Overview! Last Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book! This Lecture! Socket options! Source: Chapter 7 of Stevens book! Elementary UDP sockets! Source: Chapter 8 of Stevens book! Next Lecture! Name and address conversions! Source: Chapter 11 of Stevens book! TELE402 2012 Lecture 5 Socket Options 1

Socket Options! There are many socket options for programmers to set for fine control of the underlying system and protocols! Generic socket options! IPv4 socket option! TCP socket options! IPv6 socket options (to be discussed later)! ICMPv6 socket options (to be discussed later)! Options can be manipulated with the following functions! getsockopt and setsockopt! fcntl (recommended by Posix)! ioctl! TELE402 2012 Lecture 5 Socket Options 2

getsockopt and setsockopt (1)! These two functions apply only to sockets! 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); TELE402 2012 Lecture 5 Socket Options 3

! getsockopt and setsockopt (2)! Both return: 0 if OK, -1 on error! sockfd must refer to an open socket descriptor! level specifies the code in the system to interpret the option (the general code or protocol-specific code). See Figure 7.1 & 7.2! optname is an integer representing the specific option. Use the macro definitions in Figure 7.1 & 7.2! optval is a pointer to a variable storing the option value. (new value for setsockopt; current value for getsockopt)! Optlen is a result-value parameter referring to the size of optval! TELE402 2012 Lecture 5 Socket Options 4

Option Values! There are two types of option values! Binary options that enable or disable a certain feature (flags with in Figure 7.1 & 7.2)! 0 for disable! nonzero for enable.! Options that fetch and return specific values that we can either set or examine (values). The actual values are passed between the kernel and the user spaces.! Data types for values! Most option values are integer! Some are structures, such as timeval, linger, and character array! Refer to Datatype column in Figure 7.1 & 7.2 for details! TELE402 2012 Lecture 5 Socket Options 5

Socket States! Some socket options have timing considerations about when to set or fetch the option due to the state of the socket! The following options are inherited by a connected TCP socket from the listening socket! SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_SNDBUF, SO_RCVLOWAT, SO_SNDLOWAT, TCP_MAXSEG, AND TCP_NODELAY! For TCP, the connected socket is not returned to a server by accept until the three-way handshake is completed by the TCP layer.! To ensure that one of the above options is set for the connected socket when the three-way handshake completes, we must set that option for the listening socket.! TELE402 2012 Lecture 5 Socket Options 6

Generic Socket Options (1)! SO_BROADCAST! Enable or disable the ability of the socket to send broadcast messages! SO_DEBUG! Supported only by TCP. When enabled, the kernel keeps track of detailed info about all the packets sent or received by the socket! SO_DONTROUTE! Bypass the normal routing mechanism of the underlying protocol! TELE402 2012 Lecture 5 Socket Options 7

Generic Socket Options (2)! SO_ERROR! Get pending error and clear! so_error is set to a Exxxx value! Called a pending error! Two ways for process to be immediately notified! If blocked in a call to select, select returns! If using signal-driven I/O, SIGIO signal is generated! Process can obtain so_error by fetching SO_ERROR option.! If so_error is nonzero! If read is called and no data to return, -1 is returned and errno=so_error! If read is called and data is queued, data is returned instead of the error condition! If write is called, -1 is returned and errno=so_error! TELE402 2012 Lecture 5 Socket Options 8

Generic Socket Options (3)! SO_KEEPALIVE! If there is no data exchanged in either direction for 2 hours, a probe is sent to the peer if this option is set. One of three scenarios exists:! Peer responds with an ACK! Peer responds with a RST! No response! TELE402 2012 Lecture 5 Socket Options 9

Ways to detect TCP conditions! TELE402 2012 Lecture 5 Socket Options 10

Linger (1)! SO_LINGER! Specifies how close operates for a connection-oriented protocol! The following structure is used:! struct linger { int l_onoff; int l_linger; } // l_onoff - 0=off; nonzero=on // l_linger specifies seconds Three scenarios:! If l_onoff is 0, close returns immediately. If there is any data still remaining in the socket send buffer, the system will try to deliver the data to the peer. The value of l_linger is ignored.! TELE402 2012 Lecture 5 Socket Options 11

Linger (2)! If l_onoff is nonzero and linger is 0, TCP aborts the connection when close is called. TCP discards data in the send buffer and sends RST to the peer.! If l_onoff is nonzero and linger is nonzero, the kernel will linger when close is called.! If there is any data in the send buffer, the process is put to sleep until either:! the data is sent and acknowledged! Or! the linger time expires (for a nonblocking socket the process will not wait for close to complete)! When using this feature, the return value of close must be checked. If the linger time expires before the remaining data is send and acknowledged, close returns EWOULDBLOCK and any remaining data in the buffer is ignored.! TELE402 2012 Lecture 5 Socket Options 12

close scenarios (1)! TELE402 2012 Lecture 5 Socket Options 13

close scenarios (2)! TELE402 2012 Lecture 5 Socket Options 14

close scenarios (3)! Application ACK to confirm the receipt of data! TELE402 2012 Lecture 5 Socket Options 15

close and shutdown! TELE402 2012 Lecture 5 Socket Options 16

Generic Socket Options (4)! SO_OOBINLINE! Out-of-band data can be placed in the normal input queue! SO_RCVBUF and SO_SNDBUF! Get/set the send buffer size and receive buffer size! These sizes are related to capacity of the connection! SO_RCVLOWAT and SO_SNDLOWAT! Decide the conditions for readable and writable! TELE402 2012 Lecture 5 Socket Options 17

Generic Socket Options (5)! SO_RCVTIMEO and SO_SNDTIMEO! Place a timeout on socket receives and sends! They affect read and write function families! SO_TYPE! Returns the socket type such as SOCK_STREAM and SOCK_DGRAM! SO_USELOOPBACK! Applies only to routing sockets! TELE402 2012 Lecture 5 Socket Options 18

Generic Socket Options (6)! SO_REUSEADDR! Allows 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! 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.! Allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address! Allows completely duplicate bindings: a bind of an IP address and port, when that same IP address and port are already bound to another socket (normally for support of multicasting).! TELE402 2012 Lecture 5 Socket Options 19

IPv4 Socket Options (1)! IP_HDRINCL! For raw IP socket to build user specified header! The header is completely built, except the checksum, ID field, and the source IP address if the address is set INADDR_ANY! IP_OPTIONS! Allow us to set IP options in the IP header! IP_RECVDSTADDR! Causes the destination IP address of a received UDP datagram to be returned as ancillary data by recvmsg! TELE402 2012 Lecture 5 Socket Options 20

IPv4 Socket Options (2)! IP_RECVIF! Causes the index of the interface on which a UDP datagram is received to be returned as ancillary data by recvmsg! IP_TOS! Get/set the type-of-service field in the IP header! IP_TTL! Get/set the default TTL! TELE402 2012 Lecture 5 Socket Options 21

TCP Socket Options! TCP_MAXSEG! Get/set the maximum segment size for a TCP connection! TCP_NODELAY! Disable the delay algorithm (Nagle algorithm)! TELE402 2012 Lecture 5 Socket Options 22

Nagle Algorithm! TELE402 2012 Lecture 5 Socket Options 23

fcntl function (1)! fcntl stands for file control, and this function performs various descriptors control operations! int fcntl(int fd, int cmd, )! Returns: depends on cmd if OK, -1 on error! TELE402 2012 Lecture 5 Socket Options 24

fcntl function (2)! TELE402 2012 Lecture 5 Socket Options 25

fcntl function (3)! fcntl provides the following features related to network programming! Nonblocking I/O (be aware of error-handling in the following code)! flags=fcntl(fd, F_GETFL, 0); flags = O_NONBLOCK; fcntl(fd, F_SETFL, flags);! Signal driven I/O! flags=fcntl(fd, F_GETFL, 0); flags = O_ASYNC; fcntl(fd, F_SETFL, flags);! Set socket owner to receive SIGIO signals! fcntl(fd, F_SETOWN, getpid()); TELE402 2012 Lecture 5 Socket Options 26

UDP client/server! TELE402 2012 Lecture 5 Socket Options 27

recvfrom and sendto (1)! ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen); ssize_t sendto(int sockfd, const void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen); Both return: number of bytes read or written if OK, -1 on error! TELE402 2012 Lecture 5 Socket Options 28

recvfrom and sendto (2)! sockfd, buff, and nbytes are identical to read/write! flags is normally set 0, but can be set for advanced functions! The final two arguments to recvfrom are similar to the final two arguments to accept. They can be NULL.! The final two arguments to sendto are similar to the final two arguments to connect! Send 0 bytes is ok; likewise receive 0 bytes is ok! TELE402 2012 Lecture 5 Socket Options 29

Simple UDP C/S! Refer to udpcliserv/udpserv01.c, lib/dg_echo.c, udpcliserv/ udpcli01.c, and lib/dg_cli.c for details! Sockets are created with type SOCK_DGRAM! If a datagram is lost, the client will wait forever! Timeout is needed, but not enough (duplicate problem)! TELE402 2012 Lecture 5 Socket Options 30

Comparison with TCP C/S! TELE402 2012 Lecture 5 Socket Options 31

Verifying Received Response! Any process that knows the client s ephemeral port could send datagrams to the client.! Change call to recvfrom to return the IP address and port of the replies.! Check they are from the intended server.! TELE402 2012 Lecture 5 Socket Options 32

Connected UDP sockets (1)! Distinguish between! An unconnected UPD socket, the default when we create a UDP socket! A connected UPD socket, the result of calling connect on a UPD socket.! Three things change with connected UDP sockets! We can no longer specify the destination IP address and port for an output operation.! No point to use recvfrom, since the datagrams received come from the same source as the socket is connected to! Asynchronous errors are returned! TELE402 2012 Lecture 5 Socket Options 33

Connected UDP Sockets (2)! TELE402 2012 Lecture 5 Socket Options 34

Connected UDP Sockets (3)! TELE402 2012 Lecture 5 Socket Options 35

Connected UDP Sockets (4)! A UDP client or server can call connect only if that process uses the UDP socket to communicate with exactly one peer! TELE402 2012 Lecture 5 Socket Options 36