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