Ping a server

本文介绍了一种使用Java实现的伪Ping方法,通过与服务器的Echo端口交互来判断服务器是否可达。此外,还展示了如何利用JDK 1.5中InetAddress类的isReachable方法进行更高效的服务器可达性检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ping a server
It's not possible to really "ping" a machine to check if it's alive or not (it's a long story, but to keep it short I will just say that the Socket class is not low-level enough for that operation). But we can emulate a ping by talking the "echo port". On a server, the echo port is always port 7. We write a string to that port and the server will echo the string.
import java.io.*;
import java.net.*;

public class PseudoPing {
  public static void main(String args[]) {
    try {
      Socket t = new Socket(args[0], 7);
      DataInputStream dis = new DataInputStream(t.getInputStream());
      PrintStream ps = new PrintStream(t.getOutputStream());
      ps.println("Hello");
      String str = is.readLine();
      if (str.equals("Hello"))
        System.out.println("Alive!") ;
      else
        System.out.println("Dead or echo port not responding");              
      t.close();
      }
    catch (IOException e) {
      e.printStackTrace();}
      }
     }
NOTE: To make this a more "complete PING", you may want to check this How-to to display the response time.

Since JDK1.5, java.net.InetAddress.isReachable(int) can be used to check if a server is reachable or not.

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

public class ReachableTest {
 public static void main(String args[]) {
     try {
       InetAddress address = InetAddress.getByName("web.mit.edu");
       System.out.println("Name: " + address.getHostName());
       System.out.println("Addr: " + address.getHostAddress());
       System.out.println("Reach: " + address.isReachable(3000));
     }
     catch (UnknownHostException e) {
       System.err.println("Unable to lookup rgagnon.com");
     }
     catch (IOException e) {
       System.err.println("Unable to reach rgagnon.com");
     }
   }
}
isReachable() 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. But most Internet sites have disabled the service or blocked the requests (except some university web sites like the example above) .

### LAN8720A网络芯片的连通性或配置问题分析 LAN8720A是一款广泛应用于嵌入式系统的以太网PHY芯片。当遇到其ping配置或连通性问题时,可以从以下几个方面进行排查: #### 1. **硬件连接** 确保LAN8720A与主机控制器之间的物理连接正常。通常情况下,该芯片通过RMII接口与MCU或其他处理器通信。如果存在任何松动或者不匹配的情况,则可能导致无法建立有效的链路状态。 #### 2. **驱动程序初始化** 检查设备树(Device Tree)或固件中的相关设置是否正确加载了LAN8720A所需的驱动支持。例如,在Linux环境下可能需要确认如下参数被正确定义并应用到实际运行环境中[^3]: ```bash config ethernet 'eth0' option phyaddr '0x0' option mii 'rmii' ``` #### 3. **IP地址分配** 对于IPv4环境来说,如果没有静态设定,默认应该由DHCP服务器提供动态IP;而在IPv6场景下除了无状态自动配置外还可能存在有状态dhcpv6服务参与其中。假如PC-A未能成功获取来自stateful dhcpv6 server 的地址可能是由于客户端请求失败或者是server端存在问题所致[^1]。 #### 4. **路由表及默认网关** 即使本地子网内的机器间可以互相访问(ping),但如果要测试外部互联网资源则需依赖于合适的路由规则以及指定好的default gateway 。假设当前使用的下一跳地址方案中某特定节点发生故障(如192.168.101.2失效),那么依据预设策略会选择其他可用选项作为新的active next hop ,比如给出的例子里面提到的选择之一会成为替代路径的一部分[^2]. #### 5. **多播组加入情况** 考虑到ipv6协议特性,每当一个新接口启用带有相应类型的ip v6 地址时候它就会自动订阅某些特殊的multicast 组成员资格其中包括但不限于all-nodes 和solicited-node 这些都是为了实现基础功能所必需的过程因此也可以作为一个诊断方向来验证是否存在这方面异常状况影响到了整体通讯效果[^4] --- 以下是针对上述几个主要方面的具体操作建议: - 使用命令`ethtool ethX`(替换为具体的网卡名称)查看link status 是否up. - 查阅日志文件寻找错误提示:`dmesg | grep lan8720a`. - 验证tcpdump捕获的数据包是否有预期回应. 最后提醒一下注意版本兼容性和官方文档指导因为不同厂商可能会有所差异.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值