Outline Outline of Topics UDP Socket Java Programming Multicast in Java Real-time protocol (RTP) XMPP and Jingle protocols Java I/O and New IO (NIO)
UDP Socket Java Programming 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 Not connection-based (point-to-point) like TCP: suitable for very large numbers of clients Lack of retransmission delays
UDP Socket Java Programming 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
UDP Socket Java Programming 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 one s complement 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
UDP Socket Java Programming Datagrams: checksum calculation Source IP address UDP total length Protocol Destination IP address Source port number Data Destination port number
UDP Socket Java Programming 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)
UDP Socket Java Programming 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.
Multicast in Java Multicast Imaging you need to send message 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 message 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
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 packet Multicast IP address Sender Joint to receive multicast message Receivers
Multicast in Java Multicast: Java example MulticastSocket class: sending and receiving IP multicast packets A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining groups of other multicast hosts on the internet. For Android App development, you can also use MulticastSocket class
Multicast in Java Multicast: Java example Steps of using MulticastSocket to receive messages Step 1: creating a MulticastSocket with the desired port Step 2: joinng a group using the joingroup(inetaddress groupaddr) Step 3: leaving the group using leavegroup(inetaddress addr) method. method
Multicast in Java Multicast: Java example Steps of using MulticastSocket to send messages Step 1: creating a MulticastSocket Step 2: create a DatagramPacket for a message Step 3: set Time-To-Live (TTL) using Step 4: send data Step 4: close the MulticastSocket settimetolive 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.
Real-time protocol (RTP) Real-time protocol (RTP): What is it? Defined by RFC-1889, RTP, a Transport Protocol for Realtime Applications A standardized packet format for delivering audio and video over IP networks Why: to cope with misordered or lost packets How: ignoring the loss or re-sending the missing data, and processing out-of-order packages Implementations are built on the UDP Allows data transfer to multiple destinations through multicast
Real-time protocol (RTP) Applications of RTP IP Phone Audio conference Video conference IP television / video on demand
Real-time protocol (RTP) Java Media Framework (JMF) A Java library that enables audio, video and other time-based media to be added to Java applications and applets. Used for capturing, playing, streaming, and transcoding multiple media formats Provides support for RTP to enable the transmission and reception of real-time media streams across the network. Cannot be used for Android and lack of updates and maintenance (last update in 2008!) A few alternatives exist, the most promising one is Freedom for Media in Java (FMJ)
Real-time protocol (RTP) Further reading Since JMF and RTP are beyond this module, you can learn from: IBM JMF Tutorial JMF tutorial from Univ. of Colorado Some code examples from Old Dominion University Documents for developing RTP APPs in Android
XMPP and Jingle protocols XMPP: what is it? XMPP: Extensible Messaging and Presence Protocol A open standard communications protocol for message-oriented middleware based on XML Was a open-source project called Jabber Formalize by Internet Engineering Task Force (IETF) as a open standard, no royalties are required Cisco acquired Jabber (Cisco Jabber) in 2008 for Enterprise Instant Messaging Can be used for instant messaging, VoIP, video, file transfer, gaming Used by Google Talk, Facebook s chat, Skype and Microsoft Messenger service. Click Full list or here
XMPP and Jingle protocols Jingle: what is it? An extension to XMPP which adds peer-to-peer (P2P) session control (signaling) for multimedia interactions, e.g., Voice over IP (VoIP) or video conferencing communications. Designed by Google and the XMPP Standards Foundation Real-time Transport Protocol (RTP) as the media transport Google App Engine provides support for Jingle or XMPP You can use the libjingle to develop Google Talk applications Google replaced GTalk with Handouts, as well as their Jingle protocol You can also use this Java library: Smack For Android, you can use asmack Other libraries
XMPP and Jingle protocols Samck Java example: chat with Google Talk We can use Smack to send/receive chat messages to/from Google Talk. You can try Facebook Chat Not in the campus because of the firewall A few tutorials/examples to get you started: Smack official document. A facebook chat client using Smack You can do a lot of cool things: voice chat, video chat, file transfer, games, etc.
Java I/O and New IO (NIO) Java I/O: what is it? I/O: input/output, refers to the interface between a computer/the world, or program/the rest of the computer. Usually built into the operating system. Uses stream metaphor: packaged and transmitted data as one byte at a time, through an object called a Stream. Reminder: Socket is a kind of I/O. Advantages: easy to process the streamed data through I/O, e.g., to create filters. Disadvantages: slow
Java I/O and New IO (NIO) Java NIO: what is it? An alternative Java IO API to the standard Java IO API Offer features for intensive I/O operations Uses a different metaphor: block I/O Block I/O: data is packaged and transmitted in blocks. Advantages: faster than standard stream-oriented I/O You can read more from Oracle s Java page Java 7 release NIO version 2 in 2011: provides extended capabilities for file system tasks,
Java I/O and New IO (NIO) Java NIO: imprtant concepts Two central concept: Channel Buffer Channels are analogous to streams in the original I/O package where you can read and write data. A Buffer is a container object, which holds some data, that is to be written to or that has just been read from. Buffer is the most significant difference between tradition stream-oriented IO and NIO In stream-oriented I/O data is directly written to, and read data from, Stream objects, but in NIO, data is read and write via Buffer
Java I/O and New IO (NIO) Java NIO: Channels details All IO in NIO starts with a Channel. Three types of network related channels: DatagramChannel: read/write data via UDP SocketChannel: read/write data via TCP. ServerSocketChannel: similar to ServerSocket to listen for incoming TCP connections Also file channel: FileChannel Similar to Streams
Java I/O and New IO (NIO) Java NIO: Differences between Channels and Streams Channels are bi-directional: you can both read and write data from/to a Channels. Streams are one-way: you need to use InputStream and OutputStream Channels can be asynchronous, that is, reading and writing data without blocking Channels always read/write data to/from a Buffer.
Java I/O and New IO (NIO) Java NIO: Buffer details Essentially a block of memory, or more precisely, an array. But more than an array: provides structured access to data and also keeps track of the system s read/write processes There are different kinds of Buffer : ByteBuffer: most common one, can be used for most of the I/O operations CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer, DoubleBuffer
Java I/O and New IO (NIO) Java NIO: How to use Buffer? Five steps: Step 1: Create a buffer ByteBuffer buffer = ByteBuffer.allocate(1024); Step 2: Write data from Channel into the Buffer inchannel.read(buffer); Step 3: Call buffer.flip() to switch the buffer from writing mode into reading mode Step 4: Read data out of the Buffer buffer.get() or buffer.array() Step 5: Call buffer.clear() or buffer.compact() to clear the buffer. clear() clears all buffer, compact() only clears the data which you have already read
Java I/O and New IO (NIO) Java NIO: Using FileChannel to read a file. Three steps: Step 1: Get a FileChannel from a FileInputStream Step 2: Create a buffer Step 3: Read from the channel into the buffer Q: How it differs from original I/O? A: Original I/O only creates a FileInputStream and read from that Some tricks to make it even faster: use Channel to Channel Transfers
Java I/O and New IO (NIO) Java NIO network programming: SocketChannel Steps for setting up SocketChannel Step 1: Open a SocketChannel SocketChannel socketchannel = SocketChannel.open(); Step 2: Connect to a server socketchannel.connect (new InetSocketAddress("google.com", 80)); Step 3: Reading/writing from/to a SocketChannel to Buffer Step 4: Close the SocketChannel
Java I/O and New IO (NIO) Java NIO network programming: ServerSocketChannel Steps for setting up ServerSocketChannel Step 1: Open a ServerSocketChannel ServerSocketChannel serversocketchannel = ServerSocketChannel.open(); Step 2: Listening for Incoming Connections while(true) { SocketChannel socketchannel = serversocketchannel.accept(); //communication using socketchannel } Step 4: Close the ServerSocketChannel serversocketchannel.close();
Java I/O and New IO (NIO) Further readings Oracle s tutorial KryoNet : API for efficient TCP and UDP client/server network communication using NIO Five ways to maximize Java NIO and NIO.2 You can ignore those bits on non-blocking or asynchronous mode and selection. I will come back to this after multi-threading programming.