Top Banner
Network Programming Tutorial #9 CPSC 261
34

Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Dec 26, 2015

Download

Documents

Terence Spencer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Network Programming

Tutorial #9CPSC 261

Page 2: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 3: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 4: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Socket calls in sequence (UDP)

• Client sideSocketBind

SendtoRecvfromClose

• Server sideSocketBind

RecvfromSendtoClose

Page 5: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Socket calls in sequence (TCP)

• Client sideSocketBind

ConnectSendRecvClose

• Server sideSocketBindListenAcceptRecvSendClose

Page 6: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 7: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 8: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 9: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 10: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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 ...

Page 11: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 12: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 13: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 14: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 15: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 16: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 17: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 18: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

close()

close(socket)socket: the socket file descriptor

Page 19: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Look at a couple of examples

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

Page 20: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

Socket calls in sequence (TCP)

• Client sideSocketBind

ConnectSendRecvClose

• Server sideSocketBindListenAcceptRecvSendClose

Page 21: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 22: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

listen()

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

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

Page 23: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 24: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 25: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 26: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 27: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 28: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 29: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 30: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

recv()

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

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

Page 31: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 32: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 33: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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

Page 34: Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.

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