1 / 34

Network Programming

Network Programming. Tutorial #9 CPSC 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

donnel
Download Presentation

Network Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network Programming Tutorial #9 CPSC 261

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

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

  4. Socket calls in sequence (UDP) • Client side Socket Bind Sendto Recvfrom Close • Server side Socket Bind Recvfrom Sendto Close

  5. Socket calls in sequence (TCP) • Client side Socket Bind Connect Send Recv Close • Server side Socket Bind Listen Accept Recv Send Close

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

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

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

  9. socket addresses structsockaddr An address that can be in any address family structsockaddr_in An address in the INET (v4) address family Consists 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

  10. socket addresses – C evil You allocate variables of type structsockaddr But then treat them as if they are of type structsockaddr_in For example: structsockaddrs; structsockaddr_in *si = (structsockaddr_in *) &s; si->sin_family ... si->sin_addr ... si->sin_port ...

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

  12. bind() bind(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a structsockaddr addrlen: the length of the addr

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

  14. sendto() sendto(socket, buf, len, flags, destaddr, addrlen) socket: the socket file descriptor buf: pointer to data to send len: length of data to send flags: almost always 0 destaddr: a structsockaddr * indicating the destination socket addrlen: the length of destaddr

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

  16. recvfrom() recvfrom(socket, buf, len, flags, srcaddr, addrlen) socket: the socket file descriptor buf: pointer to recv buffer len: length of recv buffer flags: almost always 0 srcaddr: a structsockaddr * for receiving the source socket addr addrlen: a pointer to the length of srcaddr, changed by the call

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

  18. close() close(socket) socket: the socket file descriptor

  19. Look at a couple of examples • All are in the network directory of the lectures repository • receive.c • send.c • shared.c

  20. Socket calls in sequence (TCP) • Client side Socket Bind Connect Send Recv Close • Server side Socket Bind Listen Accept Recv Send Close

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

  22. listen() listen(socket, backlog) socket: a file descriptor returned from socket() backlog: the number of incoming connection requests to allow to queue

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

  24. connect() connect(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a structsockaddr addrlen: the length of the addr

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

  26. accept() accept(socket, addr, addrlen) socket: a file descriptor returned from socket() addr: a pointer to a structsockaddr addrlen: a pointer to the length of the addr, changed by the call

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

  28. send() send(socket, buf, len, flags) socket: the socket file descriptor buf: pointer to data to send len: length of data to send flags: almost always 0

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

  30. recv() recv(socket, buf, len, flags) socket: the socket file descriptor buf: pointer to recv buffer len: length of recv buffer flags: almost always 0

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

  32. shutdown() shutdown(socket, how) socket: a file descriptor returned from socket() how: SHUT_RD no more receives SHUT_WR no more sends SHUT_RDWR no more communication at all

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

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

More Related