Network Programming
description
Transcript of Network Programming
![Page 1: Network Programming](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/1.jpg)
Network Programming
Tutorial #9CPSC 261
![Page 2: Network Programming](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/4.jpg)
Socket calls in sequence (UDP)
• Client sideSocketBind
SendtoRecvfromClose
• Server sideSocketBind
RecvfromSendtoClose
![Page 5: Network Programming](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/5.jpg)
Socket calls in sequence (TCP)
• Client sideSocketBind
ConnectSendRecvClose
• Server sideSocketBindListenAcceptRecvSendClose
![Page 6: Network Programming](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/18.jpg)
close()
close(socket)socket: the socket file descriptor
![Page 19: Network Programming](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/20.jpg)
Socket calls in sequence (TCP)
• Client sideSocketBind
ConnectSendRecvClose
• Server sideSocketBindListenAcceptRecvSendClose
![Page 21: Network Programming](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062410/56816384550346895dd46753/html5/thumbnails/34.jpg)
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