CMPT 471 Networking II Network Programming © Janice Regan, 2006-2013.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Nonblocking I/O Blocking vs. non-blocking I/O
Socket Options. abstraction Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
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.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
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.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
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 Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Sockets IMGD Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Socket Options Jari Kellokoski. Introduction So far we have discovered some basic usage of socket With socket options we control sockets more suitable.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Computer Network Architecture and Programming
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Lecture 8 UDP Sockets & I/O Multiplexing
Socket option Getsockopt ou setsockopt fcntl ioctl.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Introduction to Raw Sockets 2 IP address Port address MAC address TCP/IP Stack 67 Bootp DHCP OSPF protocol frame type UDP Port # TCP Port.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
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.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
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.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
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.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
Socket options: a summary –Tcp options, ip options and general socket options can be examined and sometimes modified. getsockopt, setsockopt routines Some.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
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,
Today’s topic: UDP Reliable communication over UDP.
Review: –Concurrent server and multiplexed server How they work? Which one is better?
1 Socket Options getsockopt and setsockopt functions Check options and obtain default values Generic socket options IPv4 socket options IPv6 socket options.
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
I/O Multiplexing.
Socket Option.
Review: TCP Client-Server Interaction
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
UDP Sockets Programming
Chapter 07. Socket Options.
Lecture 11 Overview.
Advanced Network Programming spring 2007
Socket Programming.
Advanced Sockets Programming
Advanced I/O Functions
Socket Programming with UDP
Socket options: a summary
Advanced UNIX programming
Presentation transcript:

CMPT 471 Networking II Network Programming © Janice Regan, 2006-2013

Transfer of Data The following functions all allow the transfer of data through the socket. #include <sys/socket.h> int read( int socketfd, void *buffer, int maxbuflen) int write ( int socketfd, void *buffer, int maxbuflen) ssize_t readv( int filedes, const struct iovec *iov, int iovcnt) ssize_t writev( int filedes, const struct iovec *iov, int iovcnt) ssize_t recv( int socketfd, void *buf, size_t nbytes, int flags) ssize_t send( int socketfd, const void *buf, size_t nbytes, int flags) ssize_t recvmsg( int socketfd, struct msghdr *msg, int flags) ssize_t sendmsg( int socketfd, struct msghdr *msg, int flags) ssize_t recvfrom( int socketfd, void *buf, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen) ssize_t sendto( int socketfd, const void *buf, size_t nbytes, int flags, struct sockaddr *to, socklen_t *addrlen) © Janice Regan, 2006-2013

Standard Unix read/write int read( int socketfd, void *buffer, int buflen) int write ( int socketfd, void *buffer, int buflen) The buffer holds the data being read from/written to the socket The size of the buffer is buflen A single call to read or write transfers up to buflen bytes The actual number of bytes transferred is given by the return value. 0 means no data transferred. -1 indicates an error Can be used with file descriptors as well as socket descriptors © Janice Regan, 2006-2013

Gather read or Scatter write ssize_t readv( int filedes, const struct iovec *iov, int iovcnt) ssize_t writev( int filedes, const struct iovec *iov, int iovcnt) Gather data from several places and read or write data to multiple buffers through a socket Read or write a vector of buffers at the same time iov is a pointer to a vector of iovcnt structures of type struct iovec Each structure contains the starting address and size of the buffer to be written LINUX allows up to 1024 structures Can be used with filedes being a socket descriptor or another type of descriptor © Janice Regan, 2006-2013

The Format of an iovec © Janice Regan, 2006-2013

Recv and Send ssize_t recv( int socketfd, void *buf, size_t nbytes, int flags) ssize_t send( int socketfd, const void *buf, size_t nbytes, int flags) Similar to read and write The flags argument contains the logical or of a series of possible flags (0 if no flags used) The flags can specify if routing is enabled, if the data transfer is blocking or nonblocking, if data is to be sent/received out of band, to continue the transfer until the requested number of bytes are read © Janice Regan, 2006-2013

Recvfrom and Sendto ssize_t recvfrom( int socketfd, void *buf, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen) ssize_t sendto( int socketfd, const void *buf, size_t nbytes, int flags, struct sockaddr *to, socklen_t *addrlen) Used with unconnected sockets (for example UDP) First 4 arguments are the same as recv and send The from argument is a sockaddr structure containing the communcation endpoint from which the data is being sent The to argument is a sockaddr structure containing the communication endpoint to which the data should be sent © Janice Regan, 2006-2013

Transfer of Data ssize_t recvmsg( int socketfd, struct msghdr *msg, int flags) ssize_t sendmsg( int socketfd, struct msghdr *msg, int flags) Most general form of data transfer, can replace any of the other functions Sends/receives the message rather than just the data © Janice Regan, 2006-2013

message structure format struct msghdr { void *msg_name; socklen_t msg_namelen; struct iovec *msg_iov; int msg_iovlen; void *msg_accrights; socklen_t msg_accrightslen; } © Janice Regan, 2006-2013

Connection establishment Establish connection TCP client TCP server socket( ) bind( ) Well known port listen( ) accept( ) socket( ) Blocks until connection from client Connection establishment TCP 3-way handshake connect( ) © Janice Regan, 2006-2013

End of file notification TCP Data Transfer TCP client TCP server write( ) Data (request) read( ) Process request Data (reply) write( ) read( ) close( ) End of file notification read( ) close( ) © Janice Regan, 2006-2013

UDP Data Transfer socket( ) bind( ) socket( ) recvfrom( ) sendto( ) UDP client socket( ) UDP client socket( ) bind( ) recvfrom( ) sendto( ) Data (request) Blocks until connection from client Process request sendto( ) Data (reply) recvfrom( ) © Janice Regan, 2006-2013 close( )

Socket Options getsockopt can be used to request or the values of socket options #include <sys/socket.h> int getsockopt( int sockfd, int level, int optname void *optval, soclen_t *optlen); int setsockopt( int sockfd, int level, int optname const void *optval, soclen_t *optlen); Returns 0 on success 1 on failure When a server uses accept the server obtains the information on the clients communication endpoint using getpeername © Janice Regan, 2006-2013

Socket Options Socket options can apply on different levels Executed in the general socket code (SOL_SOCKET) Executed in the protocol specific code (IPPROTO_IP, IPPROTO_IPV6, IPPROTO_TCP…) The option name specifies the particular option being looked at or set. There are two basic types of options Binary options: set on or off Options that set or return values of various types (through the void pointer) Actual options are discussed in the man pages for the functions © Janice Regan, 2006-2013

Examples Socket Options Change the default behavior of the close function Set buffer size (size of sliding window) Allow broadcast of packets Route/don’t route outgoing packets Turn on/off keepalive messages (2 hours) Set maximum TCP segment size Reuse addresses or ports © Janice Regan, 2006-2013

Examples Socket Options Change default operation of the close function (SO_LINGER option) Default is 0: normal 3-way handshake, close returns immediately (l_onoff 0) Value >0: (l_onoff >0 l_linger>0) waits for l_linger seconds before closing or closes as soon as all outstanding data have been sent and acknowledged. In case of time out normal close is aborted. l_onoff>0 l_linger=0 abort connection when closed, discard unsent data, send a RST (no time wait state) Used to catch problems with server or client crash before completing the handshake causing lost data © Janice Regan, 2006-2013

Closing a connection The close functions default results are mark the socket with socket descriptor sockfd as closed For an TCP connection, start the three way handshake to terminate the connection by sending a FIN Prevents further use of the socket by the application Allows any previously queued data to be sent before closing the connection #include <sys/socket.h> int close( int socketfd) © Janice Regan, 2006-2013

Default operation of close write data close FIN Close returns ACK of data and FIN Read queued data FIN ACK of FIN © Janice Regan, 2006-2013

operation of close l_linger > 0 write data close FIN ACK of data and FIN Read queued data Close returns FIN ACK of FIN © Janice Regan, 2006-2013

operation of close l_linger >0 write data close FIN Close returns -1 EWOULDBLOCK ACK of data and FIN Read queued data FIN RST © Janice Regan, 2006-2013

Socket Options: Buffer size Set buffer size (size of sliding window SO_SNDBUF, SO_RCVBUF, set size of buffer used to hold data ready to be sent or after being received. Data that cannot be held in RCVBUF are discarded. Default sizes are implementation dependent Should be at least 4X MSS Used in negotiation of connection © Janice Regan, 2006-2013

Examples Socket Options Turn on/off keepalive messages (2 hours) SO_KEEPALIVE, when set a keepalive message will be sent across the connect after 2 hours without data crossing the connection. If an ACK is received connection continues, If a RST is received server has been rebooted connection is closed with ECONNRESET If no response probe is repeated every 75 seconds (8 trys) (for UNIX) © Janice Regan, 2006-2013

Socket Options: Broadcast, Route Allow broadcast of packets SO_BROADCAST if set enables application to send broadcast messages Specify packets are to bypass normal routing mechanisms SO_DONTROUTE is set, prevents normal routing of outgoing packets from the socket © Janice Regan, 2006-2013

Socket Options: KeepAlive Turn on/off keepalive messages (2 hours) SO_KEEPALIVE, when set a keepalive message will be sent across the connect after 2 hours without data crossing the connection. If an ACK is received connection continues, If a RST is received server has been rebooted connection is closed with ECONNRESET If no response probe is repeated every 75 seconds (8 trys) (for UNIX) © Janice Regan, 2006-2013

Socket Options: MSS for TCP Your application can specify the maximum segment size (largest amount of data that can be placed into a TCP segment) Different OS’s will have different defaults. You may have to reset this option to assure that your client and server will transfer up to a TCP segment of up to 1500 bytes. Use the TCP_MAXSEG option © Janice Regan, 2006-2013

The select function #include <sys/select.h> #include <sys/time.h> int select( int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout) Ask process to wait for any one of a set of events and wake up only if one or more of these events occur or after a specified time has elapsed Events include read or write to a socket or file descriptor, occurrence of an exception condition, specified time interval has expired. © Janice Regan, 2006-2013

Arguments of the select function timeout Time given in seconds and microseconds struct timeval { long tv_sec; long tv_usec;} If timeout is a null pointer wait forever before return If timeout structure contains 0 do not wait at all before return (polling) If timeout structure contains a time, wait that amount of time before returning Wait is interrupted by any event being watched for © Janice Regan, 2006-2013

Arguments of the select function readset, writeset, exceptset Each is an array of default length 32. Each member of the array corresponds to a file or socket descriptor between 0 and 31. Each member can be set to indicate that the particular descriptor the member represents should be watched FD_Set(5, &rset); indicates the member corresponding to descriptor 5 should be watched (is it ready to read data) © Janice Regan, 2006-2013

Arguments of the select function readset, writeset, exceptset Each is an array of default length 32. Array can be initialized to zero FD_ZERO(&rset); Can check if a member is ready FD_ISSET(5, &rset); If member is not ready bit is cleared, otherwise the waiting data is processed Any of the three sets can be a null pointer if they are not used © Janice Regan, 2006-2013

Arguments of the select function maxfdp1 This variable is used to indicate how many descriptors to check (for efficiency) Should be set the value of the largest file or socket descriptor +1 © Janice Regan, 2006-2013

When is a descriptor ready Receive and send buffers have a low watermark (default for TCP UDP is 1). When the number of bits of data waiting in the receive buffer is >= the low watermark the socket is ready to read. When the number of bits of data waiting in the send buffer is >= low watermark the socket is ready to write. (Option is available to set low watermark SO_RCVLOWAT, SO_SNDLOWAT) Ready for writing if the write half of the connection is closed, ready for reading if the read half is closed A socket error is pending © Janice Regan, 2006-2013