🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #2295

Re: List of my Servers

#2295
From: Owen Green
Date: Wed, 21 Mar 2001 16:59
83 lines
3505 bytes
> I need to easily create a list of all computers in a net which have my
> software communication on a dedicated port.
>
> Is there a easy way?
Well, easy-ish :-)

Each instance of your software needs to have a ServerSocket, preferably
running in a background thread listening on your chosen port. Have this
accept a connection, return some glib yet entertaining message (such as
"hello") and close the connection, for example

public class ListenThread extends Thread {

  public ListenThread() {
   super();
  }
  public boolean terminated = false;

  public void run(){
   try{
     ServerSocket ss = new ServerSocket(1922);
     while(!terminated){
       Socket s = ss.accept();
       OutputStreamWriter o = new OutputStreamWriter(s.getOutputStream());
       o.write("Hello!!\n"); /*Would probably benifit from being replaced
with something a bit more meaningful*/
       s.close();
       sleep(10);
     }
    }catch(java.io.IOException ioe){
      System.out.println(ioe.getMessage()); /*Put real exception handling in
here... */
    }catch(InterruptedException ie){
      System.out.println(ie.getMessage()); /*and here*/
    }
  }
}


Then your scanning programming needs to use a socket to connect to each
address in the subnet and see what's there. To do this use
InetAddress.getByName() to iterate through your range, and connect a new
socket to that (untested, uncompiled, typed in outlook, so don't count on it
;-):

    public String[] scan(String subnet, int startIP, int endIP, int
myPort)throws
bageInGarbageOutException{ 

        /*Missing all Try{ }catch blocks...*/ 

        /*TODO: the string subnet should be parsed to make sure it makes sense before doing anything else. 
        One strategy could be to tokenize it (using StringTokenizer with "." as its token) and make sure that each number
        is a) numeric <g> and b) 0<num<=255, also that start IP and endIP fall in this range. On second thoughts it may be an 
        idea to make them bytes just to enfor
ce it */

        ArrayList res = new ArrayList();

        for(int addr = startIP; addr == endIP; addr
{ 
            InetAddress thisAddr = InetAddress.getByName(subnet + String.valueOf(addr)); 
            Socket thisCon = new Socket(thisAddr,myPort); 
            BufferedReader br = new BufferedReader(new InputStreamReader(thisCon.getInputStream()); 
            if(br.readLine().equals("Hello!!")){ 
               res.add(thisCon.toString()); 
            } 
        } 
        if (!res.isEmpty()) return (String[]) res.toArray(new String[res.size()]); 
        else return null; 
    } 


hth 
Owen 






 
Technical Details
Message-ID: <3ab8ddf1_2@dnews>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!news-out.visi.com!hermes.visi.com!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!213.86.7.136
References: <99732e$hd0$04$1@news.t-online.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,66a373cd612d97ea
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-03-23 03:34:08 PST
From: "Owen Green" <owen@norobots.unique.com>
Newsgroups: borland.public.jbuilder.java.api
References: <99732e$hd0$04$1@news.t-online.com>
Subject: Re: List of my Servers
Date: Wed, 21 Mar 2001 16:59:20 -0000
Lines: 86
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
NNTP-Posting-Host: 213.86.7.136
Message-ID: <3ab8ddf1_2@dnews>
X-Trace: 21 Mar 2001 08:59:29 -0800, 213.86.7.136
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!news-out.visi.com!hermes.visi.com!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!213.86.7.136