final/BindPort.java

24 lines
852 B
Java
Raw Normal View History

2025-12-21 21:47:25 +08:00
import java.net.ServerSocket;
import java.net.Socket;
public class BindPort {
public static void main(String[] args) {
try {
// 尝试绑定到端口8081
ServerSocket serverSocket = new ServerSocket(8081);
System.out.println("Successfully bound to port 8081");
// 等待客户端连接
System.out.println("Waiting for client connections...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getRemoteSocketAddress());
// 关闭连接
clientSocket.close();
serverSocket.close();
} catch (Exception e) {
System.err.println("Error binding to port 8081: " + e.getMessage());
e.printStackTrace();
}
}
}