Nabin Bhandari
Nabin Bhandari

Reputation: 16429

How to find online users in a local area peer to peer network?

I am currently working on a small peer to peer application in which users can chat with each other within a LAN. I have currently implemented the following code to broadcast by a user that s/he is online.

import java.io.*;
import java.net.*;
class BroadcastOnline extends Thread{
    public void run(){
        try{
            String string = "a";
            DatagramSocket serverSocket = new DatagramSocket();
            InetAddress IPAddress = InetAddress.getByName("255.255.255.255");
            byte[] sendData = new byte[1];
            sendData = string.getBytes();
            for(;;){
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
                serverSocket.send(sendPacket);
                Thread.sleep(1000);
            }    
        } catch (Exception e){}
    }
}

And I have used following code to find who are online.

import java.io.*;
import java.net.*;
class FindUsers { 
    InetAddress ad;
    String ipaddress;
    String onlineUsers[] = new String [10];
    FindUsers() throws Exception{
        DatagramSocket clientSocket = new DatagramSocket(9877);
        int count=0;
        byte[] receiveData = new byte[1];
        for(int i=0;i<=9;i++){                   
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            String sentence = new String( receivePacket.getData());
            ad = receivePacket.getAddress();
            ipaddress = ad.getHostAddress();
            onlineUsers[i]=ipaddress;
            count++;
        }
    }
}

But the problem is that the above codes are running in infinite loop. And I think the implementation is a bit silly.

Are there any other ways to implement this feature?

EDIT: I got the solution to list IP addresses in the list. How can I get and keep the user friendly names in the list?

Upvotes: 2

Views: 221

Answers (1)

Nabin Bhandari
Nabin Bhandari

Reputation: 16429

You need to broadcast the name like following in the BroadcastOnline class:

byte[] sendData = new byte[15];
String name = nameField.getText();
sendData = name.getBytes();
        for(;;){
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
            serverSocket.send(sendPacket);
            Thread.sleep(1000);
        }  

**of course it is needed to catch exception above

And to find users who are online, implement the following code.

import java.io.*;
import java.net.*;
import java.util.List;
import java.util.ArrayList;

public class FindUsers{
    InetAddress inetAddress;
    List<String> ipList = new ArrayList<String>();
    List<String> nameList = new ArrayList<String>();
    String ipAddress;
    String name;
    DatagramSocket  clientSocket;
    DatagramPacket receivePacket;
    int count=0;
    byte[] receiveData;
    long futureTime;
    Thread collect;
    boolean refresh = true;

    public FindUsers(){
        futureTime = System.currentTimeMillis()+1100;

        try{
            clientSocket = new DatagramSocket(9877);
        }
        catch(IOException e){
            e.printStackTrace();
            System.out.println(e.toString());
        }

        collect = new Thread(new Runnable(){
            public void run(){
                for(;;){
                    receiveData = new byte[15];
                    receivePacket = new DatagramPacket(receiveData, receiveData.length);
                    try{
                    clientSocket.receive(receivePacket);
                    inetAddress = receivePacket.getAddress();
                    ipAddress = inetAddress.getHostAddress();
                    }
                    catch(IOException e){
                        //e.printStackTrace();
                    }
                    if(!ipList.contains(ipAddress)){
                        name = new String( receivePacket.getData());
                        ipList.add(ipAddress);
                        nameList.add(name);
                        receiveData = null;
                    }
                    try{
                        Thread.sleep(10);
                    }
                    catch(InterruptedException e){
                        //System.out.println("\nInterrupted!");
                        return;
                    }
                }
            }
        });
        collect.start();

        while(System.currentTimeMillis()<=futureTime){
            //do nothing.
        }
        clientSocket.close();
        collect.interrupt();

        int size = nameList.size();
        if (size==0){
            System.out.println("No online servers.");
        }
        else{
            for(int i = 0; i< nameList.size();i++){
                System.out.println(nameList.get(i)+ ": "+ ipList.get(i));
            }
        }
    }

    public static void main(String[] args){
        FindUsers f = new FindUsers();
    }
}

Hope this will help.

Upvotes: 1

Related Questions