180 likes | 270 Views
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 );
E N D
Programming with UDP – II CoveredSubjects: Creating UDP sockets Client Server Sending data Receiving data Connectedmode
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
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));
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.
sendto() • You can send 0 bytes of data! • Somepossibleerrors : EBADF, ENOTSOCK: badsocketdescriptor EFAULT: badbufferaddress EMSGSIZE: messagetoolarge ENOBUFS: system buffers are full
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!
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.
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.
Morerecvfrom() • Same errors as sendto, but also: EINTR: System call interruptedby signal. • Unless you do something special - recvfromdoesn’t return until thereis adatagramavailable!
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).
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().
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); }
Debugging Debugging UDP can be difficult. • Write routines to print out sockaddrs. • Include code that can handleunexpectedsituations.
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.
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.
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!
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.
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.