ping

illaX

Erfahrenes Mitglied
Hi,

ich moechte andere rechner pingen, habe gelesen, dass dies in java nicht moeglich sei. Aber das kann ich nicht glauben :D

Hat da jemand einen Rat fuer mich?
 
Vielen Dank fuer eure Antworten.

Ich habe es jetzt mit einer schlechten, aber ausreichenden Methode geloest. Habe die cmd ausgelesen.
 
Hallo!

Wie wär's denn damit:
Code:
/**
 * 
 */
package de.tutorials;

import java.net.InetAddress;

/**
 * @author Darimont
 */
public class PingExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		String host = "10.23.80.244";
		if (InetAddress.getByName(host).isReachable(1000)) {
			System.out.println(host + " is reachable...");
		} else {
			System.out.println(host + " is not reachable...");
		}
	}

}

Gruß Tom
 
jo tausend dank

ich habe es auch schon mit .getByName ausprobiert, aber ich kam nicht auf .isReachable.

Vielen Dank
 
Aber damit pinge ich doch jetzt oder nicht?

Ich verstehe jetzt nicht mehr, warum alle sagen, dass das mit java nicht moeglich ist.
 
Hallo!

Die Methode isReachable an InetAddress gibts auch erst seit Java 5.
public boolean isReachable(int timeout)
throws IOExceptionTest whether that address is reachable. Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
The timeout value, in milliseconds, indicates the maximum amount of time the try should take. If the operation times out before getting an answer, the host is deemed unreachable. A negative value will result in an IllegalArgumentException being thrown.

gruß Tom
 
Hi,

ich habe mal ein ähnliches Problem gehabt. Es ging darum zu testen, ob der Inhouse Updateserver verfügbar ist um ggfs. Updates zu laden.

Code:
	SocketAddress sa =null;
	InetAddress ia=null;
	Socket clientSocket=null; 
	int inetPort = 0;
	

	try{

	  		URL url = new URL("195.128.56.56");		
			ia=InetAddress.getByName(url.getHost());	
                                                //445 ist der Windows Netzwerkport
			sa=new InetSocketAddress(ia,445);	
			clientSocket=new Socket();	
			long btime = System.currentTimeMillis();
			clientSocket.connect(sa,2500);
			clientSocket.close();
			long etime = System.currentTimeMillis() - btime;
			
			System.out.println("Zeit ms:"+ etime);	
				
		}catch(UnknownHostException uhe){	
			uhe.printStackTrace();		
		}catch(SocketTimeoutException stoe){
			System.out.println("Ping Exception " + stoe.getMessage());
			return false;
		}catch(IOException ioe){
			ioe.printStackTrace();
	}
 

Neue Beiträge

Zurück