public void setupServer(String ipAddress, int port) { try { InetAddress addr = InetAddress.getByName("10.28.202.79"); ServerSocket ss = new ServerSocket(port, 50, addr); System.out.println("开始服务"); while (true) { Socket sc = ss.accept(); System.out.println("已连接 " + sc.getRemoteSocketAddress()); // 等待从客户端传过来的消息 // 调用处理连接来处理连接 communicationProcess(sc); } } catch (Exception e) { e.printStackTrace(); } }
时间: 2024-03-30 18:38:22 浏览: 74
这是一个Java程序的方法,它用于设置一个服务器,接受来自客户端的连接。参数ipAddress表示服务器的IP地址,port表示监听的端口号。在方法中,首先通过InetAddress.getByName()方法获取服务器的IP地址,然后创建一个ServerSocket对象,指定端口号和最大连接数,开始监听客户端的连接。每当有客户端连接时,就会创建一个Socket对象,并调用communicationProcess()方法来处理连接。如果发生异常,就会打印异常信息。
相关问题
实现利用InetAddress.getByName()按计算机名称获得实验室局域网中所有开机主机名称和IP地址。import java.net.InetAddress; import java.net.UnknownHostException; public class lab1 { public static void main(String[] args) { try { // 获取本机的InetAddress实例 InetAddress localhost = InetAddress.getLocalHost(); // 获取本地网络地址前缀 byte[] address = localhost.getAddress(); String prefix = ""; for (int i = 0; i < address.length - 1; i++) { prefix += (address[i] & 0xff) + "."; } // 遍历局域网中所有可能的IP地址并获取主机名和IP地址 for (int i = 1; i <= 255; i++) { String host = prefix + i; try { InetAddress inetAddress = InetAddress.getByName(host); String hostName = inetAddress.getHostName(); String hostAddress = inetAddress.getHostAddress(); System.out.println("HostName: " + hostName + ", hostAddress: " + hostAddress); } catch (UnknownHostException e) { } } } catch (UnknownHostException e) { e.printStackTrace(); } } }分析该代码
== 0) ? prefix + "255." : prefix + address[i] + "."; } // 遍历局域网中所有可能的IP地址 for (int i = 1; i < 255; i ) { String host = prefix + i; // 通过名称获取InetAddress实例 InetAddress inetAddress = InetAddress.getByName(host); // 判断是否可达 if (inetAddress.isReachable(5000)) { System.out.println(inetAddress.getHostName() + " : " + inetAddress.getHostAddress()); } } } catch (UnknownHostException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
解释下面代码import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; import org.jnetpcap.Pcap; import org.jnetpcap.PcapIf; import org.jnetpcap.packet.PcapPacket; import org.jnetpcap.protocol.network.Ip4; import org.jnetpcap.protocol.tcpip.Tcp; public class PacketCapture { public static void main(String[] args) { StringBuilder errbuf = new StringBuilder(); Pcap pcap = Pcap.openLive("eth0", 65536, Pcap.MODE_PROMISCUOUS, 1000, errbuf); if (pcap == null) { System.err.println("Failed to open device: " + errbuf); return; } Ip4 ip = new Ip4(); Tcp tcp = new Tcp(); PcapPacket packet = new PcapPacket(ByteBuffer.allocate(65536).order(ByteOrder.BIG_ENDIAN)); while (pcap.nextEx(packet) == Pcap.NEXT_EX_OK) { if (packet.hasHeader(ip) && packet.hasHeader(tcp)) { try { InetAddress srcAddr = InetAddress.getByAddress(ip.source()); InetAddress dstAddr = InetAddress.getByAddress(ip.destination()); int srcPort = tcp.source(); int dstPort = tcp.destination(); System.out.println("Source IP address: " + srcAddr.getHostAddress()); System.out.println("Destination IP address: " + dstAddr.getHostAddress()); System.out.println("Source port: " + srcPort); System.out.println("Destination port: " + dstPort); } catch (UnknownHostException e) { e.printStackTrace(); } } } pcap.close(); } }
这段代码是一个 Java 程序,它使用 jNetPcap 库实现了网络数据包捕获和解析功能。具体来说,它通过调用 Pcap 类中的 openLive() 方法打开一个网络接口设备,设置捕获数据包的最大长度为 65536 字节、捕获模式为混杂模式、超时为 1000 毫秒。然后使用 Ip4 和 Tcp 类设置了需要捕获的协议类型。在 while 循环中,使用 nextEx() 方法获取下一个数据包,如果数据包包含了 IP 和 TCP 协议头,则通过 InetAddress 类将源和目的地址转换为可读的字符串形式,获取源和目的端口号,最后将它们打印出来。最后,在程序结束时关闭了 pcap 实例。
阅读全文