Home  >  

Using UDP socket connections for low-latency and loss-tolerant scenarios in AIR 2 (Part 3)

Author photo
November 19, 2009 | | Comments (0)
AddThis Social Bookmark Button

Now that we have a client built that will send and receive packets we need a server for it to communicate with. Lets walk through a quick implementation of a basic Java UDP socket server. Some familiarity with Java is helpful here, but isn't required. The DatagramSocket class in AIR 2 closely mirrors its Java counterpart so our server implementation should look familiar in a lot of ways.

Before we go on, If you haven't done so already, install Eclipse. This will make it easy to build a Java project and run the socket application for our client to speak with. Once you've done this create a new Java Project named "SimpleUDPServer". We'll then create a DatagramSocket in Java and tell it which port to listen to for incoming packets.

public class SimpleUDPServer {
 
       public SimpleUDPServer(){ }
     
       public static void main(String[] args) throws IOException {
         
              DatagramSocket datgramSocket = new DatagramSocket(4455);
  
        }

Now that the socket has been created you can receive packets on it using the DatagramSocket.receive() method. This works a bit differently from our client side receive() because there is no event dispatched with the payload. In order continually receive packets on the Java side you'll need to nest this call within a while loop. We'll also create a packet that our receive() method will update each time the loop runs.


while(connected){
       try {
                 
              byte[] buf = new byte[512];
          		
              DatagramPacket incomingPacket = new DatagramPacket(buf, buf.length);
  
              datgramSocket.receive(incomingPacket);
  
              }
            catch (IOException e) { }

No once we've captured the incoming packet data we can examine it and use a conditional to determine how we should respond. We're expecting our client to ask "Hello, how are you?". If we do in fact get this as an input we then generate a packet with the appropriate response.


String s = new String(incomingPacket.getData(),0,incomingPacket.getLength());
                
if(s.equals("Hello, how are you?")){
                 
       String responseString = "I'm quite well, thank you";
 
}

With a response ready we can now call the DatagramSocket.send() method. We'll need to create a packet from our response and determine its destination before sending it. We could easily hard code this destination to a single address and port but in the event of multiple client communication we would be incapable of responding to each one individually. We want to inspect each incoming packet and use its port and address of origin as our return destination. Once we have that information we forge our outgoing packet and send it on its way.


InetAddress address = incomingPacket.getAddress();
int port = incomingPacket.getPort();
	                
DatagramPacket outgoingPacket = new DatagramPacket(buf, buf.length, address, port);
	                
datgramSocket.send(outgoingPacket);

Once sent we should immediately receive the packet sent in our client and we can show the response to the user.

And there you have it. As you might imagine there are a number of possibilities here. You could easily expand on the conditionals to send different responses for different inputs or implement some basic routing to forward packets from one user to another.

Download client and server source

See part 1 of this series here See part 2 of this series here

Read more from Ian McLean. Ian McLean's Atom feed ianmclean on Twitter

Comments

Leave a comment


Tag Cloud

iPad

What's your take on the iPad? (Putting aside the Flash/iPad flame war)

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.