CS计算机代考程序代写 Slide 1

Slide 1

1

CMPT 471
Networking II

Network Programming
© Janice Regan, 2006-2013

© Janice Regan, 2006-2013
2
Transfer of Data
The following functions all allow the transfer of data through the socket.
#include
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
3
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
4
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
5
The Format of an iovec

© Janice Regan, 2006-2013
6
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
7
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
8
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
9
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
10
Establish connection

socket( )
connect( )

accept( )
listen( )
bind( )
socket( )

TCP server
TCP client
Connection establishment
TCP 3-way handshake
Well known port
Blocks until connection
from client

© Janice Regan, 2006-2013
11
TCP Data Transfer

write( )

read( )
close( )
read( )
read( )
write( )
Process request

close( )
Data (request)
Data (reply)
End of file notification
TCP client
TCP server

© Janice Regan, 2006-2013
12
UDP Data Transfer

sendto( )

recvfrom( )
bind( )
socket( )

sendto( )
socket( )

Data (request)
Blocks until connection
from client
Process request
recvfrom( )
UDP client
UDP client

Data (reply)

close( )

© Janice Regan, 2006-2013
13
Socket Options
getsockopt can be used to request or the values of socket options
#include
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
14
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
15
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
16
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
17
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
int close( int socketfd)

© Janice Regan, 2006-2013
18
Default operation of close

data
write
close

FIN

ACK of data and FIN
Read queued data

FIN

ACK of FIN
Close returns

© Janice Regan, 2006-2013
19
operation of close l_linger > 0

data
write
close

FIN

ACK of data and FIN
Read queued data

FIN

ACK of FIN
Close returns

© Janice Regan, 2006-2013
20
operation of close l_linger >0

data
write
close

FIN

ACK of data and FIN
Read queued data

FIN

RST
Close returns -1 EWOULDBLOCK

© Janice Regan, 2006-2013
21
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
22
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
23
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
24
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
25
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
26
The select function
#include
#include
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
27
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
28
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
29
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
30
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
31
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

/docProps/thumbnail.jpeg