network programming tutorial #9 cpsc 261. a socket is one end of a virtual communication channel...

34
Network Programming Tutorial #9 CPSC 261

Upload: terence-spencer

Post on 26-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Network Programming

Tutorial #9CPSC 261

A socket is one end of a virtual communication channel

• Provides network connectivity to any other socket anywhere else in the world

• Using either the TCP or UDP transport protocol

Network API

• Sockets are the key data structure• Collection of system calls that

create/destroy/manage sockets– Create/destroy: socket, close– Connect: bind, connect, listen, accept– Manage: shutdown, setsockopt– Send/Receive: send, recv, write, read, sendto,

recvfrom

Socket calls in sequence (UDP)

• Client sideSocketBind

SendtoRecvfromClose

• Server sideSocketBind

RecvfromSendtoClose

Socket calls in sequence (TCP)

• Client sideSocketBind

ConnectSendRecvClose

• Server sideSocketBindListenAcceptRecvSendClose

Socket programming is ... baroque*

• The socket calls were standardized a long time ago– There were lots of different network standards– Each with their own addressing information– The socket calls need to be general enough to

handle them all (and any other ones that might come along)

– Easy things are much harder than they should need to be

*Extravagant, complex, or bizarre

socket()

• Create a communication endpoint• Returns a file descriptor (an integer) which

must be used in every call referring to this communication endpoint

• Returns -1 on error– errno is set to indicate why

socket()

socket(domain, type, protocol)domain: AF_UNIX, AF_INET, AF_INET6

type: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RDM

protocol: 0 means the only available protocol

socket addresses

struct sockaddr An address that can be in any address family

struct sockaddr_inAn address in the INET (v4) address familyConsists of a 32 bit IP address (s_addr) and a 16

bit port (s_port)Both in network byte order

All unused bits in an address must be 0

socket addresses – C evilYou allocate variables of typestruct sockaddr

But then treat them as if they are of typestruct sockaddr_in

For example:struct sockaddr s;struct sockaddr_in *si = (struct sockaddr_in *) &s;

si->sin_family ...si->sin_addr ...si->sin_port ...

bind()

• Establish a local address for a socket• The local address is the address of this

endpoint, so will be the current machine’s IP address and a port

• Returns 0 on success• Returns -1 on error– errno is set to indicate why

bind()

bind(socket, addr, addrlen)socket: a file descriptor returned from socket()

addr: a pointer to a struct sockaddr

addrlen: the length of the addr

sendto()

• Sends data through an unconnected socket• The destination address is specified each time• Returns the number of bytes sent on success• Returns -1 on error– errno is set to indicate why

sendto()

sendto(socket, buf, len, flags, destaddr, addrlen)

socket: the socket file descriptorbuf: pointer to data to sendlen: length of data to sendflags: almost always 0destaddr: a struct sockaddr * indicating the destination socket

addrlen: the length of destaddr

recvfrom()

• Receives data through an unconnected socket• The source address is specified each time• Returns the number of bytes received on

success• Returns -1 on error– errno is set to indicate why

recvfrom()

recvfrom(socket, buf, len, flags, srcaddr, addrlen)

socket: the socket file descriptorbuf: pointer to recv bufferlen: length of recv bufferflags: almost always 0srcaddr: a struct sockaddr * for receiving the source socket addr

addrlen: a pointer to the length of srcaddr, changed by the call

close()

• closes a socket• makes it unusable in the future for sending or

receiving• Returns 0 on success• Returns -1 on error– errno is set to indicate why

close()

close(socket)socket: the socket file descriptor

Look at a couple of examples

• All are in the network directory of the lectures repository– receive.c– send.c– shared.c

Socket calls in sequence (TCP)

• Client sideSocketBind

ConnectSendRecvClose

• Server sideSocketBindListenAcceptRecvSendClose

listen()

• Indicates that the socket is to be used for accepting incoming connections

• Done on the “server” side only• Returns 0 on success• Returns -1 on error– errno is set to indicate why

listen()

listen(socket, backlog)socket: a file descriptor returned from socket()

backlog: the number of incoming connection requests to allow to queue

connect()

• Connects a socket to a destination socket whose address is given as an argument

• Done on the “client” side only• Returns 0 on success• Returns -1 on error– errno is set to indicate why

connect()

connect(socket, addr, addrlen)socket: a file descriptor returned from socket()

addr: a pointer to a struct sockaddr

addrlen: the length of the addr

accept()

• Accepts an incoming connection– waits for an incoming connection request

• Done on the “server” side only• Returns a new socket file descriptor on

success• Returns -1 on error– errno is set to indicate why

accept()

accept(socket, addr, addrlen)socket: a file descriptor returned from socket()

addr: a pointer to a struct sockaddr

addrlen: a pointer to the length of the addr, changed by the call

send()

• Sends data through a connected socket• No destination address is specified • Returns the number of bytes sent on success• Returns -1 on error– errno is set to indicate why

send()

send(socket, buf, len, flags)socket: the socket file descriptor

buf: pointer to data to sendlen: length of data to sendflags: almost always 0

recv()

• Receives data through a connected socket• No source address is indicated• Returns the number of bytes received on

success– 0 usually means “end-of-file”, meaning there will

be no more data to receive, ever again• Returns -1 on error– errno is set to indicate why

recv()

recv(socket, buf, len, flags)socket: the socket file descriptor

buf: pointer to recv bufferlen: length of recv bufferflags: almost always 0

shutdown()

• Indicates that an application is done with either sending or receiving data on a socket

• When a connected stream socket is shutdown for sends, then the other side receives “end-of-file”

• Returns 0 on success• Returns -1 on error– errno is set to indicate why

shutdown()

shutdown(socket, how)socket: a file descriptor returned from socket()

how: SHUT_RD no more receivesSHUT_WR no more sendsSHUT_RDWR no more communication at all

Web resources

• There are lots of network programming guides on the net

• The “standard” reference is Beej’s guide• It has become way too long over the years• The 1999 version is pretty good– On the web pointed to by the lab page

Bits of advice

• Pay very close attention to the arguments to every network call– Especially addresses

• Check the returned value from every network call– If something goes wrong, you need to know about

it early– And bail out, or start over and try again