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对象