1 / 18

Programming with UDP – II

Programming with UDP – II. Covered Subjects : Creating UDP sockets Client Server Sending data Receiving data Connected mode. Creating a UDP socket. i nt socket (int family , int type , int protocol ) i nt sock ; s ock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP );

phuc
Télécharger la présentation

Programming with UDP – II

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. Programming with UDP – II CoveredSubjects: Creating UDP sockets Client Server Sending data Receiving data Connectedmode

  2. Creating a UDP socket int socket(int family, int type, int protocol) int sock; sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock< 0){ printf("Unable to create the UDP socket\n"); return 0; } //end-if

  3. Binding to well known address This is typically done by server only. int mysock; structsockaddr_inmyaddr; mysock= socket(AF_INET,SOCK_DGRAM, IPPROTO_UDP); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(80); myaddr.sin_addr = htonl(INADDR_ANY); bind(mysock, &myaddr, sizeof(myaddr));

  4. Sending UDP Datagrams int sendto(int sockfd, char* buff, size_tnbytes, int flags, structsockaddr* to, int addrlen); • sockfdis a UDP socket • buffis the address of the data (nbyteslong) • tois the address of a sockaddr containing the destination address. • Return value is the number of bytes sent, or -1 on error.

  5. sendto() • You can send 0 bytes of data! • Somepossibleerrors : EBADF, ENOTSOCK: badsocketdescriptor EFAULT: badbufferaddress EMSGSIZE: messagetoolarge ENOBUFS: system buffers are full

  6. Moresendto() • The return value of sendto() indicateshow much data was accepted by theOS for sending as a datagram – nothow much data made it to thedestination. • There is no error condition thatindicates that the destination did not getthe data!

  7. Receiving UDP Datagrams int recvfrom(int sockfd, char* buf, size_tnbytes, int flags, structsockaddr* from, int* fromaddrlen); • sockfdis a UDP socket • buffis the address of a buffer (nbyteslong) • fromis the address of a sockaddr. • Return value is the number of bytes receivedand put into buff, or -1 on error.

  8. recvfrom() • If buffis not large enough, any extra data is lostforever! • You can receive 0 bytes of data! • The sockaddrat fromis filled in with theaddressof thesender. • You should set fromaddrlenbefore calling. • If fromand fromaddrlenare NULL we don’t findout who sent the data.

  9. Morerecvfrom() • Same errors as sendto, but also: EINTR: System call interruptedby signal. • Unless you do something special - recvfromdoesn’t return until thereis adatagramavailable!

  10. Typical UDP clientcode • Create UDP socket. • Create sockaddrwith address of server. • Call sendto(), sending request to theserver. • No call to bind() is necessary! • Possibly call recvfrom()(if we need areply).

  11. Typical UDP Server code • Create UDP socket and bind to wellknownaddress. • Call recvfrom() to get a request, notingthe address of the client. • Process request and send reply backwithsendto().

  12. UDP Echo Server !! NEED TO CHECK FOR ERRORS !! int mysock; structsockaddr_inmyaddr, cliAddr; charmsgbuf[MAXLEN]; int clilen; int msglen; mysock = socket(AF_INET,SOCK_DGRAM,IP_PROTO_UDP); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(S_PORT); myaddr.sin_addr = htonl(INADDR_ANY); bind(mysock, &myaddr, sizeof(myaddr)); while (1) { len = sizeof(cliaddr); msglen=recvfrom(mysock,msgbuf, MAXLEN,0,&cliAddr,&clilen); sendto(mysock,msgbuf,msglen,0,&cliAddr,clilen); }

  13. Debugging Debugging UDP can be difficult. • Write routines to print out sockaddrs. • Include code that can handleunexpectedsituations.

  14. Timeoutwhencallingrecvfrom() • It might be nice to have each call torecvfrom() return after a specifiedperiod of time even if there is noincomingdatagram. • We can do this by using SIGALRM andwrapping each call to recvfrom() with acalltoalarm() • Will be coveredlater.

  15. Connectedmode • A UDP socket can be used in a call toconnect(). • This simply tells the OS the address of thepeer. • No handshake is made to establish that thepeerexists. • No data of any kind is sent on the network asa result of calling connect() on a UDPsocket.

  16. Connected UDP • Once a UDP socket is connected: • can use sendto() with a null destinationaddress • can use write() and send() • can use read() and recv() • only datagrams from the peer will be returned. • Asynchronous errors will be returned to theprocess. • OS specific, somewon’t do this!

  17. AsynchronousErrors • What happens if a client sends data to aserver that is not running? • ICMP “port unreachable” error isgenerated by receiving host and sent tosending host. • The ICMP error may reach the sendinghost after sendto() has already returned! • The next call dealing with the socket couldreturn the error.

  18. Backto UDP connect() • connect() is typically used with UDPwhen communication is with a singlepeeronly. • Many UDP clients use connect(). • Someserversalso (TFTP). • It is possible to disconnect and connectthe same socket to a new peer.

More Related