dcsimg
Java application to check if machine is connected to internet
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rahul_Singh
Posted On:   Tuesday, June 12, 2001 03:20 AM

Hi,
I want to know if i can find out if machine is connected to internet usine a java application using net package

Rahul

Re: Java application to check if machine is connected to internet

Posted By:   Luigi_Viggiano  
Posted On:   Wednesday, June 13, 2001 05:43 AM

sorry, the subbmitted code contains a bug (it is only able to ping itself...) if someone's interrested I can post the corrected one.

Re: Java application to check if machine is connected to internet

Posted By:   Luigi_Viggiano  
Posted On:   Tuesday, June 12, 2001 05:09 AM

the Ping service (on port 7) is active on all machine who want to be seen. Simply ping the machine to check if it responds. Check this code

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

/**
* @author Luigi
* @version 1.0
*/
public class Pinger {
static final int PING_PORT = 7;
static final int PING_INTERVAL = 1000;
static final int PING_TIMEOUT = 3000;
static final int PING_NUMBER = 3;
static final int PING_SIZE = 32; //byte size of the "ping" message

InetAddress address = null;

public Pinger(InetAddress newAddress) {
address = newAddress;
}

public void doPing() throws SocketException {
byte[] ping = new byte[PING_SIZE];
byte[] pong = new byte[PING_SIZE];

DatagramSocket socket = new DatagramSocket(PING_PORT, address);
socket.setSoTimeout(PING_TIMEOUT);
Random random = new Random(System.currentTimeMillis());

for (byte i = 0; i < PING_NUMBER; i++) {
random.nextBytes(ping);

DatagramPacket pingPaket =
new DatagramPacket(ping, ping.length, address, PING_PORT);

DatagramPacket pongPaket =
new DatagramPacket(pong, pong.length);

try {
long time1 = System.currentTimeMillis();
socket.send(pingPaket);
socket.receive(pongPaket);
long elapsed = System.currentTimeMillis() - time1;
System.out.println("Reply from " + address.getHostAddress() + " "
+ elapsed + "ms elapsed, "
+ PING_SIZE + " byte(s) sent...");
} catch (IOException ioe) {
System.out.println("Error : " + ioe.getMessage());
//ioe.printStackTrace(System.err);
}
try {
Thread.sleep(PING_INTERVAL);
} catch (InterruptedException ie) {
// ie.printStackTrace(System.err);
}
}
socket.close();
socket = null;
}

public static void main(String[] args)
throws UnknownHostException, SocketException
{
try {
InetAddress addr = InetAddress.getByName(args[0]);
System.out.println("Pinging " + addr.getHostName() +
" ["+ addr.getHostAddress() + "] with " +
PING_SIZE + " bytes:
");
Pinger pinger = new Pinger(addr);
pinger.doPing();
} catch (ArrayIndexOutOfBoundsException aiobEx) {
System.err.println("Usage: java Pinger [hostname]");
}
}
}

This is an example of execution:

$ java Pinger localhost

Pinging localhost [127.0.0.1] with 32 bytes:

Reply from 127.0.0.1 0ms elapsed, 32 byte(s) sent...
Reply from 127.0.0.1 0ms elapsed, 32 byte(s) sent...
Reply from 127.0.0.1 0ms elapsed, 32 byte(s) sent...

Re: Java application to check if machine is connected to internet

Posted By:   Luigi_Viggiano  
Posted On:   Tuesday, June 12, 2001 04:48 AM

Simply... ping the machine. Check this code:


import java.net.*;
import java.io.*;
import java.util.Random;

/**
* @author Luigi
* @version 1.0
*/
public class Pinger {
static final int PING_PORT = 7;
static final int PING_INTERVAL = 1000;
static final int PING_TIMEOUT = 3000;
static final int PING_NUMBER = 3;
static final int PING_SIZE = 32; //byte size of the "ping" message

InetAddress address = null;

public Pinger(InetAddress newAddress) {
address = newAddress;
}

public void doPing() throws SocketException {
byte[] ping = new byte[PING_SIZE];
byte[] pong = new byte[PING_SIZE];

DatagramSocket socket = new DatagramSocket(PING_PORT, address);
socket.setSoTimeout(PING_TIMEOUT);
Random random = new Random(System.currentTimeMillis());

for (byte i = 0; i < PING_NUMBER; i++) {
random.nextBytes(ping);

DatagramPacket pingPaket =
new DatagramPacket(ping, ping.length, address, PING_PORT);

DatagramPacket pongPaket =
new DatagramPacket(pong, pong.length);

try {
long time1 = System.currentTimeMillis();
socket.send(pingPaket);
socket.receive(pongPaket);
long elapsed = System.currentTimeMillis() - time1;
System.out.println("Reply from " + address.getHostAddress() + " "
+ elapsed + "ms elapsed, "
+ PING_SIZE + " byte(s) sent...");
} catch (IOException ioe) {
System.out.println("Error : " + ioe.getMessage());
//ioe.printStackTrace(System.err);
}
try {
Thread.sleep(PING_INTERVAL);
} catch (InterruptedException ie) {
// ie.printStackTrace(System.err);
}
}
socket.close();
socket = null;
}

public static void main(String[] args)
throws UnknownHostException, SocketException
{
try {
InetAddress addr = InetAddress.getByName(args[0]);
System.out.println("Pinging " + addr.getHostName() +
" ["+ addr.getHostAddress() + "] with " +
PING_SIZE + " bytes:
");
Pinger pinger = new Pinger(addr);
pinger.doPing();
} catch (ArrayIndexOutOfBoundsException aiobEx) {
System.err.println("Usage: java Pinger [hostname]");
}
}
}


example of output:

$ java Pinger localhost

Pinging localhost [127.0.0.1] with 32 bytes:

Reply from 127.0.0.1 0ms elapsed, 32 byte(s) sent...
Reply from 127.0.0.1 0ms elapsed, 32 byte(s) sent...
Reply from 127.0.0.1 0ms elapsed, 32 byte(s) sent...
About | Sitemap | Contact