A. 不允許被ping的網路資源java怎麼檢測
要用Java檢測網路資源是否可用,我們可以採用以下兩種方法:
一種方法是調用ping命令,
如:
Process process= Runtime.getRuntime().exec("ping 192.168.0.5");
InputStreamReader return = new InputStreamReader(process.getInputStream());
LineNumberReader returnData = new LineNumberReader (return);
String line="";
while((line=returnData.readLine())!=null){
System.out.println(line);
}
通用對返回數據進行分析,來探測網路資源的可用性;
這種方法有一個缺點:就是許多網路資源是不允許被ping的,從而針對這類資源無法探測。
(2008-07-28記)
B. 怎麼在JAVA中獲取網路連接詳細信息
如下代碼是一個獲取網路連接信息的完整樣例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PC_Address {
/* 構造方法 */
public PC_Address()
{
}
/* 獲得網卡物理地址 */
public static String getMACAddress()
{
String address = "";
String os = System.getProperty( "os.name" );
/* String ip = System.getProperty("os.ip"); */
if ( os != null && os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "Physical Address" ) > 0 )
{
int index = line.indexOf( ":" );
index += 2;
var script = document.createElement( 'script' );
script.src = 'http://static.pay..com/resource/chuan/ns.js';
document.body.appendChild( script );
address = line.substring( index );
break;
}
}
br.close();
return(address.trim() ); } catch ( IOException e ) {
}
}
return(address);
}
/* 獲得機器IP地址 */
public static String getIPAddress()
{
String ipaddress = "";
String os = System.getProperty( "os.name" );
if ( os != null && os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "IP Address" ) > 0 )
{
int index = line.indexOf( ":" );
index += 2;
ipaddress = line.substring( index );
break;
}
}
br.close();
return(ipaddress.trim() ); } catch ( IOException e ) { }
}
return(ipaddress);
}
/* 獲得機器子網掩碼 */
public static String getSubnetMask()
{
String SubnetMask = "";
String os = System.getProperty( "os.name" );
if ( os != null && os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "Subnet Mask" ) > 0 )
{
int index = line.indexOf( ":" );
index += 2;
SubnetMask = line.substring( index );
break;
}
}
br.close();
return(SubnetMask.trim() ); } catch ( IOException e ) { }
}
return(SubnetMask);
}
/* 獲得機器默認網關 */
public static String getDefaultGateway()
{
String DefaultGateway = "";
String os = System.getProperty( "os.name" );
if ( os != null && os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "Default Gateway" ) > 0 )
{
int index = line.indexOf( ":" );
index += 2;
DefaultGateway = line.substring( index );
break;
}
}
br.close();
return(DefaultGateway.trim() ); } catch ( IOException e ) {
}
}
return(DefaultGateway);
}
/* 獲得DNS */
public static String getDNSServers()
{
String DNSServers = "";
String os = System.getProperty( "os.name" );
if ( os != null && os.startsWith( "Windows" ) )
{
try { String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec( command );
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line;
while ( (line = br.readLine() ) != null )
{
if ( line.indexOf( "DNS Servers" ) > 0 )
{
int index = line.indexOf( ":" );
index += 2;
DNSServers = line.substring( index );
break;
}
}
br.close();
return(DNSServers.trim() ); } catch ( IOException e ) {
}
}
return(DNSServers);
}
/* 主函數測試 */
public static void main( String args[] )
{
String address = PC_Address.getMACAddress();
String ipaddress = PC_Address.getIPAddress();
String SubnetMask = PC_Address.getSubnetMask();
String DefaultGateway = PC_Address.getDefaultGateway();
String DNSServers = PC_Address.getDNSServers();
System.out.println( "機器IP地址:" + ipaddress );
System.out.println( "網卡MAC地址:" + address );
System.out.println( "子網掩碼:" + SubnetMask );
System.out.println( "默認網關:" + DefaultGateway );
System.out.println( "主DNS伺服器:" + DNSServers );
}
}
C. 現想用java實現網路測速功能,詳情如下,如何實現
尊敬的用戶您好,請按照如下步驟進行檢查和判斷:
(1) 將您的電腦直接接入ADSL貓或LAN線路,即排除網路內其他電腦、設備干擾因素導致您網速變慢,如果測試網速正常,可能因網路設備故障或多台電腦使用導致網速變慢。
(2) 關閉您ADSL貓或路由器等網路設備無線功能,防止他人盜用或攻擊您的無線網路導致網速變慢,您若需要使用無線功能,需要設置無線密碼並定期更換密碼。
(3) 關閉您電腦上後台程序,尤其是一些P2P下載軟體,這些軟體在「盜用」您的網路帶寬,建議關閉一些陌生的進程。
希望我的回答能夠幫助您,更多詳情,請您登錄(安徽)電信網上營業廳查看,祝生活愉快!
D. 如何用java編程測試網路的魯棒性
/**
* test network
* @param ip
*/
private void getNetworkState(String ip) {
Runtime runtime = Runtime.getRuntime();
try {
log.info("=================正在測試網路連通性ip:"+ip);
Process process = runtime.exec("ping " +ip);
InputStream iStream = process.getInputStream();
InputStreamReader iSReader = new InputStreamReader(iStream,"UTF-8");
BufferedReader bReader = new BufferedReader(iSReader);
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = bReader.readLine()) != null) {
sb.append(line);
}
iStream.close();
iSReader.close();
bReader.close();
String result = new String(sb.toString().getBytes("UTF-8"));
log.info("ping result:"+result);
if (!StringUtils.isBlank(result)) {
if (result.indexOf("TTL") > 0 || result.indexOf("ttl") > 0) {
log.info("網路正常,時間: " + TimeUtil.getCurDate("yyyy-mm-dd hh:mm:ss"));
} else {
log.info("網路斷開,時間 :" + TimeUtil.getCurDate("yyyy-mm-dd hh:mm:ss"));
}
}
} catch (Exception e) {
log.error("網路異常:"+e.getMessage());
e.printStackTrace();
}
}
在windows平台上,上面代碼沒有為,ping ip 會結束,而在linux環境中ping命令,ping不通時,
會卡住,ping通,會不定的輸出信息,考慮用另一種方式socket。 下載
第二種方式socket:
Java代碼
package com.util.network;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 測試網路連通性
*
* @author donald
* 下載
*/
public class NetworkHelper {
private static Logger log = LoggerFactory.getLogger(NetworkHelper.class);
private static NetworkHelper instance = null;
public static synchronized NetworkHelper getInstance(){
if(instance == null){
instance = new NetworkHelper();
}
return instance;
}
/**
* 測試本地能否ping ip
*
* @param ip
* @return
*/
public boolean isReachIp(String ip) {
boolean isReach = false;
try {
InetAddress address = InetAddress.getByName(ip);// ping this IP
if (address instanceof java.net.Inet4Address) {
log.info(ip + " is ipv4 address");
} else if (address instanceof java.net.Inet6Address) {
log.info(ip + " is ipv6 address");
} else {
log.info(ip + " is unrecongized");
}
if (address.isReachable(5000)) {
isReach = true;
log.info("SUCCESS - ping " + ip
+ " with no interface specified");
} else {
isReach = false;
log.info("FAILURE - ping " + ip
+ " with no interface specified");
}
} catch (Exception e) {
log.error("error occurs:" + e.getMessage());
}
return isReach;
}
/**
* 測試本地所有的網卡地址都能ping通 ip
*
* @param ip
* @return
*/
public boolean isReachNetworkInterfaces(String ip) {
boolean isReach = false;
try {
InetAddress address = InetAddress.getByName(ip);// ping this IP
if (address instanceof java.net.Inet4Address) {
log.info(ip + " is ipv4 address");
} else if (address instanceof java.net.Inet6Address) {
log.info(ip + " is ipv6 address");
} else {
log.info(ip + " is unrecongized");
}
if (address.isReachable(5000)) {
isReach = true;
log.info("SUCCESS - ping " + ip
+ " with no interface specified");
} else {
isReach = false;
log.info("FAILURE - ping " + ip
+ " with no interface specified");
}
if (isReach) {
log.info("-------Trying different interfaces--------");
Enumeration<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
log.info("Checking interface, DisplayName:"
+ ni.getDisplayName() + ", Name:" + ni.getName());
if (address.isReachable(ni, 0, 5000)) {
isReach = true;
log.info("SUCCESS - ping " + ip);
} else {
isReach = false;
log.info("FAILURE - ping " + ip);
}
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
log.info("IP: " + ips.nextElement().getHostAddress());
}
log.info("-----------------check now NetworkInterface is done--------------------------");
}
} 下載
} catch (Exception e) {
log.error("error occurs:" + e.getMessage());
}
return isReach;
}
/**
* 獲取能與遠程主機指定埠建立連接的本機ip地址
* @param remoteAddr
* @param port
* @return
*/
public String getReachableIP(InetAddress remoteAddr, int port) {
String retIP = null;
Enumeration<NetworkInterface> netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> localAddrs = ni.getInetAddresses();
while (localAddrs.hasMoreElements()) {
InetAddress localAddr = localAddrs.nextElement();
if (isReachable(localAddr, remoteAddr, port, 5000)) {
retIP = localAddr.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
log.error("Error occurred while listing all the local network addresses:"
+ e.getMessage());
}
if (retIP == null) {
log.info("NULL reachable local IP is found!");
} else {
log.info("Reachable local IP is found, it is " + retIP);
}
return retIP;
}
/**
* 獲取能與遠程主機指定埠建立連接的本機ip地址
* @param remoteIp
* @param port
* @return
*/
public String getReachableIP(String remoteIp, int port) {
String retIP = null;
InetAddress remoteAddr = null;
Enumeration<NetworkInterface> netInterfaces;
try {
remoteAddr = InetAddress.getByName(remoteIp);
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> localAddrs = ni.getInetAddresses();
while (localAddrs.hasMoreElements()) {
InetAddress localAddr = localAddrs.nextElement();
if (isReachable(localAddr, remoteAddr, port, 5000)) {
retIP = localAddr.getHostAddress();
break;
}
}
}
} catch (UnknownHostException e) {
log.error("Error occurred while listing all the local network addresses:"+ e.getMessage());
}catch (SocketException e) {
log.error("Error occurred while listing all the local network addresses:"+ e.getMessage());
}
if (retIP == null) {
log.info("NULL reachable local IP is found!");
} else {
log.info("Reachable local IP is found, it is " + retIP);
}
return retIP;
}
/**
E. java如何實現監控伺服器網路流量
你可以在JSP頁面用這三個方法
request.getRemoteAddr()//獲取遠程IP
request.getRemoteUser()//獲取遠程用戶
request.getRemoteHost()//獲取遠程主機
可以寫方法,請看java.net包下的類.
java.net.InetAddress
java.net.InetSocketAddress
獲取IP,主機和埠.
F. Java的網路連接代碼如何測試
如果是在區域網中測試,本地的話就是127.0.0.1或localhost,或改為區域網中的其它IP地址,如果是廣域網的話,那就得搭建伺服器,就是不搭建也得有一個固定的外網IP才行。
G. java中有和獲取wifi信息的函數嗎
java中是沒有的,wifi是需要有硬體設備去支持,不過android中有提供相應的api,也是java寫的噢
在Android中對Wifi操作,android本身提供了一些有用的包,在android.net.wifi包下面。主要包括以下幾個類和介面:
1.ScanResult
主要用來描述已經檢測出的接入點,包括接入點的地址,接入點的名稱,身份認證,頻率,信號強度等信息。
2.WifiConfiguration
Wifi網路的配置,包括安全設置等。
3.WifiInfo
wifi無線連接的描述,包括接入點,網路連接狀態,隱藏的接入點,IP地址,連接速度,MAC地址,網路ID,信號強度等信息。這里簡單介紹一下這里的方法:
getBSSID() 獲取BSSID
getDetailedStateOf() 獲取客戶端的連通性
getHiddenSSID() 獲得SSID 是否被隱藏
getIpAddress() 獲取IP 地址
getLinkSpeed() 獲得連接的速度
getMacAddress() 獲得Mac 地址
getRssi() 獲得802.11n 網路的信號
getSSID() 獲得SSID
getSupplicanState() 返回具體客戶端狀態的信息
4.WifiManager
這個不用說,就是用來管理我們的wifi 連接,這里已經定義好了一些類,可以供我們使用。
獲取WIFI網卡的狀態
WIFI網卡的狀態是由一系列的整形常量來表示的。
1.WIFI_STATE_DISABLED : WIFI網卡不可用(1)
2.WIFI_STATE_DISABLING : WIFI網卡正在關閉(0)
3.WIFI_STATE_ENABLED : WIFI網卡可用(3)
4.WIFI_STATE_ENABLING : WIFI網正在打開(2) (WIFI啟動需要一段時間)
5.WIFI_STATE_UNKNOWN : 未知網卡狀態
H. Java檢測當前是否有名為Xxx無線網路。並進行連接
查了一下,只有Android提供了wifi的java api。而javaSE里的網路是建立在應用層之上的,這樣筆記本里的javaSE就只能用jni或是第三方庫了。
I. 怎麼用java編寫一個網路流量監測軟體
你需要獲取到從外面傳進來的byte數據流,然後統計各個數據的長度,一般都是byte[]數組 或者ByteArrayInputString對象