SSC - Communication and Networking SSC - Communication and Networking Java Socket Programming (II) Shan He School for Computational Science University of Birmingham Module 06-19321: SSC SSC - Communication and Networking Outline Outline of Topics UDP Socket Java Programming Multicast in Java SSC - Communication and Networking UDP Socket Java Programming User Datagram Protocol (UDP) I Sends independent packets of data (called datagrams) between computers without guarantees about arrival and sequencing. I Transaction-oriented: suitable for simple query-response protocols, I Examples: clock server, Domain Name System, ... I Not connection-oriented (point-to-point) like TCP: suitable for very large numbers of clients I Examples: streaming media applications, e.g., IPTV I Faster: no acknowledge of receiving packets, no flow control and very simple error control (only check errors but no correction). I Examples: Voice over IP, online games, ... SSC - Communication and Networking UDP Socket Java Programming Datagrams I Datagrams: an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed. I UDP header consists of 4 fields, each of which is 16 bits Table : UDB datagram Bit 0-15 16-35 0 Source port Destination port 32 Length Checksum ... Data SSC - Communication and Networking UDP Socket Java Programming Datagrams: checksum I Used for error-checking of the UDP header and data I Also include IP address to prevent misrouting I Basic idea: the complement of a 16-bit sum calculated over an IP ”pseudo-header” and the actual UDP data. I A pseudo-header: the IP header and the UDP header without checksum field I At the receiver end: all the 16-bit words of the headers plus data area are added together =⇒ Sum I Sum + Checksum = 11111111 11111111 SSC - Communication and Networking UDP Socket Java Programming Datagrams: checksum calculation Source IP address Destination IP address Protocol UDP total length Source port number Destination port number Data source: San Diego University SSC - Communication and Networking UDP Socket Java Programming UDP datagram communication in Java I Two packages in java.net package: I DatagramPacket : contains several constructors for creating datagram packet object I Example: DatagramPacket(byte[] buf, int length, InetAddress address, int port); I DatagramSocket : provides various methods for transmitting or receiving datagrams over the network I Example: void send(DatagramPacket p) or void receive(DatagramPacket p) SSC - Communication and Networking UDP Socket Java Programming Example 1: UDP word counting server I We will create a simple UDP server waits for clients requests and then accepts the message (datagram) and send back the number of words in the message. I Since DatagramPacket only accept byte array as its argument, we need to convert String to byte[] SSC - Communication and Networking Multicast in Java Multicast I Imaging you need to send data to a group of 1,000 clients I What’s wrong with TCP? I Connection-based: you need 1,000 connections which consume a lot of processing power on sender! I Flow control: The arrival time is not the same for every clients I Multicast is a special feature of UDP protocol that enable programmer to send data to a group of receivers on a specific multicast IP address and port. I One-to-many and many-to-many real-time communication over internet I Send data only once to any number of any receivers I The data is also called multicast packets SSC - Communication and Networking Multicast in Java Multicast: Java example I A multicast group is specified by a class-D IP address and by a standard UDP port number. I Class-D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. I Receivers must join multicast group to receive data Multicast IP address Sender Joint to receive multicast message Receivers SSC - Communication and Networking Multicast in Java Multicast: Java example I MulticastSocket class: sending and receiving multicast packets I MulticastSocket class is inherited from the DatagramSocket class, with additional capabilities for joining ”groups” of other multicast hosts on the internet. I For Android App development, you can also use MulticastSocket class I More information can be found at Oracle’s webpage SSC - Communication and Networking Multicast in Java Multicast: Java example Steps of using MulticastSocket to receive data I Step 1: create a MulticastSocket object with the desired port I Step 2: join a group using the joinGroup(InetAddress groupAddr) method I Step 3: leave the group using leaveGroup(InetAddress addr) method. SSC - Communication and Networking Multicast in Java Multicast: Java example Steps of using MulticastSocket to send data I Step 1: create a MulticastSocket I Step 2: create a DatagramPacket object for data I Step 3: set Time-To-Live (TTL) using setTimeToLive method I Step 4: send data using send() method I Step 5: close the MulticastSocket using close() method Note: Time-To-Live (TTL): a value between zero and 255. Every time a router forwards the packet, it decrements the TTL field in the packet header. The packet will dropped if the value reaches zero. Used to avoid packages being looped forever due to routing errors.