Java Server/Client Class (UDP) with Automated Service Discovery

Discussion in 'Java' started by ManzZup, Jan 11, 2013.

  1. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    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"

    [​IMG]

    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.

    [​IMG]

    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.

    [​IMG]

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

    [​IMG]

    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:
    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:
    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
     
    Last edited by a moderator: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice