Network Applications: UDP Socket Programming 1/26/20122 Outline Ø Recap ❒ Network application programming3 Recap: DNS ❒ A mapping function from (domain name, service) to value ❒ Domain names divided into zones, where ❍ each zone is a sub-tree of the global tree and can have its own (multiple) authoritative name servers ❍ an authoritative name server of a zone may delegate a subset (i.e. a sub-tree) of its zone to another name server ❒ DNS allows recursive/iterated queries ❒ Local name servers cache query results4 Email Architecture + DNS mail server user agent user agent user agent mail server user agent user agent mail server user agent SMTP SMTP SMTP POP3 or IMAP SMTP DNS5 Recap: Socket buffers, states buffers, states6 Recap: DatagramSocket(Java) ❒ DatagramSocket() constructs a datagram socket and binds it to any available port on the local host ❒ DatagramSocket(int!lport) constructs a datagram socket and binds it to the specified port on the local host machine.! ❒ DatagramSocket(int!lport, InetAddress!laddr) creates a datagram socket and binds to the specified local port and laddress. ❒ DatagramSocket(SocketAddress!bindaddr) creates a datagram socket and binds to the specified local socket address. ❒ DatagramPacket(byte[]!buf, int!length) constructs a DatagramPacket for receiving packets of length length. ❒ DatagramPacket(byte[]!buf, int!length, InetAddress!address, int!port) constructs a datagram packet for sending packets of length length to the specified port number on the specified host. ❒ receive(DatagramPacket!p) receives a datagram packet from this socket.! ❒ send(DatagramPacket!p) sends a datagram packet from this socket. ❒ close() closes this datagram socket.Connectionless UDP: Big Picture (Java version) close clientSocket Server (running on servhost) read reply from clientSocket create socket, clientSocket = DatagramSocket() Client Create datagram using (servhost, x) as (dest addr. port), send request using clientSocket create socket, port=x, for incoming request: serverSocket = DatagramSocket( x ) read request from serverSocket write reply to serverSocket generate reply, create datagram using client host address, port number ❒ Create socket with a specific port number: DatagramSocket sSock = new DatagramSocket(9876); ❒ If no port number is specified, the OS will pick one8 Example: UDPServer.java ❒ A simple UDP server which changes any received sentence to upper case.9 Java Server (UDP): Create Socket import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); Create datagram socket bind at port 9876 Check socket state: %netstat –p udp –n10 System State after the Call server UDP socket space address: {*:9876} snd/recv buf: 128.36.232.5 128.36.230.2 address: {128.36.232.5:53} snd/recv buf: “*” indicates that the socket binds to all IP addresses of the machine: % ifconfig -a local address why shown as “*”? local port11 Binding to a Single IP server UDP socket space Public address: 128.36.59.2 Local address: 127.0.0.1 address: {128.36.232.5:53} snd/recv buf: InetAddress sIP1 = InetAddress.getByName(“localhost”); DatagramSocket ssock1 = new DatagramSocket(9876, sIP1); InetAddress sIP2 = InetAddress.getByName(“128.36.59.2”); DatagramSocket ssock2 = new DatagramSocket(9876,sIP2); address: {127.0.0.1:9876} snd/recv buf: address: {128.36.59.2:9876} snd/recv buf:12 UDP Demultiplexing server UDP socket space Public address: 128.36.59.2 Local address: 127.0.0.1 address: {128.36.232.5:53} snd/recv buf: UDP demutiplexing is based on matching (dst address, dst port) address: {127.0.0.1:9876} snd/recv buf: address: {128.36.59.205:9876} snd/recv buf: P1 client IP: A SP: x DP: 9876 S-IP: A D-IP: 127.0.0.1 P2 client IP: B SP: y DP: 9876 S-IP: B D-IP: 128.36.59.2Per Socket State ❒ Each socket has a set of states that can be configured: ❍ receive buffer size ❍ send buffer size ❍ Timeout See DatagramSocket API to display socket state. 1314 Java Server (UDP): Receiving import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create space for received datagram Receive datagram15 Java Server (UDP): Processing import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { … // process data String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength()); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); getData() returns a pointer to an underlying buffer array; for efficiency, don’t assume receive() will reset the rest of the array getLength() returns how much data is valid.16 UDP Demultiplexing ❒ Dest. receives UDP/IP datagrams ❍ each IP datagram has source IP address, destination IP address ❍ each transport-layer segment has source, destination port number ❒ Java DatagramPacket: ❍ getAddress()/getPort() returns the address/port17 Java server (UDP): Reply InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } } Get IP addr port #, of sender Write out datagram to socket End of while loop, loop back and wait for another datagram Create datagram to send to client18 Example: UDPClient.java ❒ A simple UDP client which reads input from keyboard, sends the input to server, and reads the reply back from the server.19 Example: Java client (UDP) import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new
View Full Document