当前位置:首页 > 综合资讯 > 正文
黑狐家游戏

java获取服务器ip和端口号,Java获取服务器IP地址和端口号的详细教程及实践案例

java获取服务器ip和端口号,Java获取服务器IP地址和端口号的详细教程及实践案例

Java获取服务器IP和端口号教程包括使用Java内置类获取本地IP地址,并通过网络编程获取服务器端口号。本文将详细讲解如何通过代码实现这一功能,并附上实际应用案例。...

Java获取服务器IP和端口号教程包括使用Java内置类获取本地IP地址,并通过网络编程获取服务器端口号。本文将详细讲解如何通过代码实现这一功能,并附上实际应用案例。

在Java编程中,获取服务器IP地址和端口号是一个常见的操作,无论是开发网络应用程序,还是进行网络通信调试,获取服务器IP和端口号都是必不可少的,本文将详细讲解如何在Java中获取服务器IP地址和端口号,并提供一些实践案例。

Java获取服务器IP地址和端口号的方法

1、使用InetAddress类

java获取服务器ip和端口号,Java获取服务器IP地址和端口号的详细教程及实践案例

InetAddress类是Java网络编程中常用的类之一,它提供了获取IP地址和端口号的方法,下面是使用InetAddress类获取服务器IP地址和端口号的示例代码:

import java.net.InetAddress;
public class GetServerIPAndPort {
    public static void main(String[] args) {
        try {
            // 获取本地IP地址
            InetAddress localIP = InetAddress.getLocalHost();
            System.out.println("本地IP地址:" + localIP.getHostAddress());
            // 获取远程服务器IP地址
            InetAddress remoteIP = InetAddress.getByName("www.baidu.com");
            System.out.println("远程服务器IP地址:" + remoteIP.getHostAddress());
            // 获取本地端口号
            int localPort = localIP.getPort();
            System.out.println("本地端口号:" + localPort);
            // 获取远程服务器端口号
            int remotePort = remoteIP.getPort();
            System.out.println("远程服务器端口号:" + remotePort);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2、使用Socket类

Socket类是Java网络编程中用于建立网络连接的类,通过Socket类,我们可以获取服务器IP地址和端口号,下面是使用Socket类获取服务器IP地址和端口号的示例代码:

java获取服务器ip和端口号,Java获取服务器IP地址和端口号的详细教程及实践案例

import java.net.Socket;
public class GetServerIPAndPort {
    public static void main(String[] args) {
        try {
            // 创建Socket对象
            Socket socket = new Socket("www.baidu.com", 80);
            // 获取服务器IP地址
            String serverIP = socket.getInetAddress().getHostAddress();
            System.out.println("服务器IP地址:" + serverIP);
            // 获取服务器端口号
            int serverPort = socket.getPort();
            System.out.println("服务器端口号:" + serverPort);
            // 关闭Socket连接
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3、使用Runtime类

Runtime类是Java中用于运行应用程序的类,通过Runtime类,我们可以获取服务器IP地址和端口号,下面是使用Runtime类获取服务器IP地址和端口号的示例代码:

import java.net.InetAddress;
public class GetServerIPAndPort {
    public static void main(String[] args) {
        try {
            // 获取运行Java虚拟机的进程
            Runtime runtime = Runtime.getRuntime();
            // 执行命令获取IP地址
            Process process = runtime.exec("ipconfig");
            // 读取命令执行结果
            java.util.Scanner scanner = new java.util.Scanner(process.getInputStream());
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.contains("IPv4 Address")) {
                    // 获取本地IP地址
                    String localIP = line.split(":")[1].trim();
                    System.out.println("本地IP地址:" + localIP);
                    break;
                }
            }
            // 执行命令获取服务器IP地址
            process = runtime.exec("nslookup www.baidu.com");
            scanner = new java.util.Scanner(process.getInputStream());
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.contains("Address:")) {
                    // 获取远程服务器IP地址
                    String remoteIP = line.split(":")[1].trim();
                    System.out.println("远程服务器IP地址:" + remoteIP);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

实践案例

1、使用Socket类实现TCP通信

java获取服务器ip和端口号,Java获取服务器IP地址和端口号的详细教程及实践案例

import java.io.*;
import java.net.*;
public class TCPClient {
    public static void main(String[] args) {
        try {
            // 创建Socket对象
            Socket socket = new Socket("127.0.0.1", 9999);
            // 获取输出流
            OutputStream os = socket.getOutputStream();
            // 获取输入流
            InputStream is = socket.getInputStream();
            // 创建缓冲区
            byte[] buffer = new byte[1024];
            int len;
            // 发送数据
            os.write("Hello, Server!".getBytes());
            // 接收数据
            while ((len = is.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
            }
            // 关闭资源
            os.close();
            is.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
public class TCPServer {
    public static void main(String[] args) {
        try {
            // 创建ServerSocket对象
            ServerSocket serverSocket = new ServerSocket(9999);
            // 监听客户端连接
            Socket socket = serverSocket.accept();
            // 获取输入流
            InputStream is = socket.getInputStream();
            // 获取输出流
            OutputStream os = socket.getOutputStream();
            // 创建缓冲区
            byte[] buffer = new byte[1024];
            int len;
            // 接收数据
            while ((len = is.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
            }
            // 发送数据
            os.write("Hello, Client!".getBytes());
            // 关闭资源
            os.close();
            is.close();
            socket.close();
            serverSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2、使用InetAddress类实现UDP通信

import java.io.*;
import java.net.*;
public class UDPClient {
    public static void main(String[] args) {
        try {
            // 创建DatagramSocket对象
            DatagramSocket socket = new DatagramSocket();
            // 创建数据包
            byte[] buffer = "Hello, Server!".getBytes();
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
            // 发送数据包
            socket.send(packet);
            // 接收数据包
            packet = new DatagramPacket(new byte[1024], 1024);
            socket.receive(packet);
            System.out.println(new String(packet.getData(), 0, packet.getLength()));
            // 关闭资源
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
public class UDPServer {
    public static void main(String[] args) {
        try {
            // 创建DatagramSocket对象
            DatagramSocket socket = new DatagramSocket(9999);
            // 创建数据包
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            // 接收数据包
            socket.receive(packet);
            System.out.println(new String(packet.getData(), 0, packet.getLength()));
            // 发送数据包
            buffer = "Hello, Client!".getBytes();
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            packet = new DatagramPacket(buffer, buffer.length, address, port);
            socket.send(packet);
            // 关闭资源
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

本文详细讲解了Java获取服务器IP地址和端口号的方法,包括使用InetAddress类、Socket类和Runtime类,还提供了一些实践案例,如使用Socket类实现TCP通信和使用InetAddress类实现UDP通信,希望本文对您有所帮助。

黑狐家游戏

发表评论

最新文章