Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Java Server/Client Class (UDP) with Automated Service Discovery (http://www.go4expert.com/articles/java-server-client-class-udp-automated-t29419/)

ManzZup 11Jan2013 23:23

Java Server/Client Class (UDP) with Automated Service Discovery
 
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 - Algorithm



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"

http://imgs.g4estatic.com/automated-...iscovery-1.png

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.

http://imgs.g4estatic.com/automated-...iscovery-2.png

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.

http://imgs.g4estatic.com/automated-...iscovery-3.png

4: Client receive a valid return packet. Yeah connection is done. :)
Pretty easy huh? :D

http://imgs.g4estatic.com/automated-...iscovery-4.png

Solution: Code



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

package org.zzl.zontek.udpserver;

Code: java

import java.io.*;
import java.net.*;

public class Demo {
    public static void main(String args[]){
       
        try{
            UDPClient client = new UDPClient(4444,256,"SERVER_TEST"){
                public void onServerFound(){
                    System.out.println("Found client on " + this.getClientIP() + ":"+ this.getClientPort());       }
            };
            client.send("heyy");
            System.out.println(client.read());
        }catch(IOException e){   
        }       
    }
}


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

Code: java

package org.zzl.zontek.udpserver;
 
import java.io.*;
import java.net.*;
 
public class Demo {
    public static void main(String args[]){
        try{
            UDPClient client = new UDPClient(4444,256,"SERVER_TEST"){
                public void onServerFound(){
                    System.out.println("Found client on " + this.getClientIP() + ":"+ this.getClientPort());       }
            };
            client.send("heyy");
            System.out.println(client.read());
        }catch(IOException e){
        }
    }
}


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 that's 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


All times are GMT +5.5. The time now is 04:52.