110 likes | 211 Views
Programming a UDP client. Find the IP address and protocol port number of the server with which communication is desired Allocate a socket Specify that the communication needs an arbitrary, unused protocol port on the local machine, and allow UDP to choose one.
E N D
Programming a UDP client • Find the IP address and protocol port number of the server with which communication is desired • Allocate a socket • Specify that the communication needs an arbitrary, unused protocol port on the local machine, and allow UDP to choose one. • Specify the server to which messages must be sent • Communicate with the server using the application-level protocol(this usually involves sending requests and awaiting replies) • Close the socket
continued • A connectionless client • The sending process creates a connected socket and uses it to send one or more requests iteratively • This algorithm ignores the issue of reliability
Connected and unconnected UDP sockets • Client application can use a UDP socket in one of two basic modes: connected and unconnected • UDP can be connected, making it convenient to interact with a specific server, • UDP can be unconnected, making it necessary for the application to specify the server’s address each time it sends a message
Using connect with UDP • Client can connect a socket of type SOCK_DGRAM • Connect call doesnot initiate any package exchange • Does not test validity of the remote endpoint address • It merely records the remote endpoint information in the socket data structure for later use • Even if the connect call succeeds, it doesn’t mean that the remote endpoint address is valid or that server is reachable
Communicating with a server using UDP • After a UDP client calls connect, it can use send to send a message or recv to receive a response. • Each time the client call send, UDP sends a single message to the server • Message contain all data to be sent • Similarly, each call to recv returns one complete message. • If the user’s buffer is not large enough, UDP discards bytes from the message that do not fit in the buffer
Closing a socket that uses UDP • A UDP client calls close to close a socket and release the resources associated with it. • Once a socket has been closed, the UDP software will reject further messages that arrive. • It does not inform the remote endpoint that the socket is closed. • Application must be designed so the remote side knows how long to retain a socket before closing
Iterative, connectionless server • Iterative server work best for services that have a low request processing time. Algorithm • Create a socket and bind to the well-known address for the service being offered • Repeatedly read next request from a client, formulate a response, and send a reply back to the client according to the application protocol
Forming a reply address • A connectionless server cannot use connect because doing so restricts the socket to communication with one specific remote host and port;the server cannot use socket again to receive datagrams from arbitrary clients. • Thus, a connectionless server uses an unconnected socket
sendto int sendto( int s, // socket descriptor const char *buff, // message int len, // length of message int flag, // 0 struct sockaddr *addrptr, //server add int addlen); // size of server add
recvfrom() int recvfrom( int s, // socket desc char *buff, // message int len, // length int flag, // 0 struct sockaddr *addrptr, //client int *addrlen); //size of client
recvfrom • int recvfrom( int s, const char *buff, int len, int flag, struct sockaddr *addrptr, int *len);