menu

Friday, January 11, 2013

Java Server/Client Class (UDP) with automated service discovery


Hey everyone,

Was a bit busy checking up with the android platform so was out of the blog for a time. But got some hot stuff in return :). This class is a class had to developed to be used in my android app.

Problem

The problem occurred when i was developing this app was that the connection of the phone with the computer. For this connection to work over the Wifi network the user must enter the correct IP address of the phone/computer. It's a bit of a downfall when it comes to the users side since there can be multiple IPs for the same computer.

Solution

After some vigorous searching + forums posts on the internet I found a acceptable solution. That is to Loop through ALL THE Network Interface Devices in the computer till the server/client is found to be connected to one of it.

Here's how it's done. Let's assume a sample Server/Client chat application

1: Server is turned on and put into listening. Now since it's a UDP server, it can start a broadcast (search for more about broadcasting). Server has a defined flag, lets say "TEST_SERVER" for an example. So what the server does is to lookout for any packets coming with the String "TEST_SERVER"



2: Now the client is turned on and it Loop through every connected interface card searching. Simply saying it sends the packet to every open device with string "TEST_SERVER" and once sending is done wait for any return packet with the same flag,



3: The Server receive a valid packet with the correct flag and it store the IP/Port of the packet origination which will be the client. Now the server has to identify itself, so it send back the packet with flag "TEST-SERVER" to the client.


4: Client receive a valid return packet. Yeah connection is done. :)



Pretty easy huh? :D
Now the best part is that you dont have to do all the above code since i have already done that. Just download the UDPServer and UDPClient jar libraries from the link below.


How to use (The Demos are also included in the jar libs)

#1: Download the jar libraries or if you want more the compelte source code

Download src+jar library: https://sourceforge.net/projects/javaudpscz/files

#2: Import the org.zzl.zontek.udpserver package

UDPServer class

Constructor
To use the you can use the constructor of the UDPServer class (abstract)

public UDPServer(int port,int packetSize, String flag) throws UnknownHostException,SocketException,IOException

port - the port that the server has to listen on
packetSize - the MAXIMUM size of a packet that you will send using the server (read more about UDP servers from java doc)
flag - some identification line, ex: MY_SERVER SERVER_TEST

Methods

To send a msg to the new found client you can use the following, but if you need to use more methods just write you own in the class file or use the send(byte[] packet) methods inline with your wrapper method.

public boolean send(byte[] packet) 
public boolean send(String msg)
public boolean send(int msg)


To receive a msg from the client just use one of the following

public byte[] read()
public String readString()
public int readInt()

To get the IP of the client

public final InetAddress getClientIP()

To get the port of the client

public final int getClientPort()


Demo.java

  1. package org.zzl.zontek.udpserver;
  2. import java.io.*;
  3. import java.net.*;
  4. public class Demo {
  5.         public static void main(String args[]){
  6.                
  7.                 try{
  8.                         UDPClient client = new UDPClient(4444,256,"SERVER_TEST"){
  9.                                 public void onServerFound(){
  10.                                         System.out.println("Found client on " + this.getClientIP() + ":"+ this.getClientPort());       }
  11.                         };
  12.                         client.send("heyy");
  13.                         System.out.println(client.read());
  14.                 }catch(IOException e){
  15.                        
  16.                 }
  17.                
  18.         }
  19. }


the onClientFound() method MUST be  implemented at the time of construction of the Server object since it handles what happens after the server found a client.


UDPClient class

Constructor


public UDPClient(int port,int packetSize, String flag) throws SocketException

port - the port that the server has to listen on
packetSize - the MAXIMUM size of a packet that you will send using the server (read more about UDP servers from java doc)
flag - some identification line, ex: MY_SERVER SERVER_TEST

All the methods of the UDPServer class is also available in this

Demo.class
  1. package org.zzl.zontek.udpserver;
  2. import java.io.*;
  3. import java.net.*;
  4. public class Demo {
  5.         public static void main(String args[]){
  6.                
  7.                 try{
  8.                         UDPClient client = new UDPClient(4444,256,"SERVER_TEST"){
  9.                                 public void onServerFound(){
  10.                                         System.out.println("Found client on " + this.getClientIP() + ":"+ this.getClientPort());       }
  11.                         };
  12.                         client.send("heyy");
  13.                         System.out.println(client.read());
  14.                 }catch(IOException e){
  15.                        
  16.                 }
  17.                
  18.         }
  19. }


the onServerFound() method MUST be  implemented at the time of construction of the Server object since it handles what happens after the server found a client.

So thats basically it, but this class may have some errors on it, so if you need any kinda of help just put me a message. HAPPY Sockets :D
Cyah

No comments:

Post a Comment