客户端无法从另一个IP连接

发布于 2024-12-03 02:25:57 字数 1464 浏览 0 评论 0原文

我编写了一个简单的客户端-服务器应用程序。它在我的电脑上运行得很好。但是当我的朋友尝试连接我的服务器时,他无法连接。我在我的计算机上使用端口 23 创建服务器。这是创建服务器的部分:

public Server(int port_number) throws IOException{          
        create_Server(port_number);
    }
    
    public static void main(String[] args) throws IOException {         
        int port_number=23;         
        new Server(port_number);
    }
    
    private void create_Server(int port_number) throws IOException{         
        ss = new ServerSocket(port_number);         
        System.out.println("Server is ready!");         
        while(true){                
            s=ss.accept();                            
            System.out.println(s.getLocalAddress().getHostName() + " was connected!");
            send_con_mes();
            list.put(s,new DataOutputStream(s.getOutputStream()) );
            new ServerThread(s,this).start();
        }           
    }
}

这是客户端部分;

public void start_Chat() {

    try {

        Ip_addr = JOptionPane.showInputDialog("Enter the IP number of the server to connect : ");
        s = new Socket(Ip_addr, 23);
        
        Client_name = JOptionPane.showInputDialog("Enter your Nickname : ");
        
        dis = new DataInputStream(s.getInputStream());         
        dos = new DataOutputStream(s.getOutputStream());
        new Thread(Client.this).start();

好吧,我可以说话,发送私人消息等。当我作为客户端连接到我的计算机上的服务器时,但最后的问题是来自另一个IP的客户端无法连接。

I wrote a simple client-server application. It works very well on my computer. but when my friend tries to connect my server he can't. I create the server on my computer with port 23. Here is the part of creating the server:

public Server(int port_number) throws IOException{          
        create_Server(port_number);
    }
    
    public static void main(String[] args) throws IOException {         
        int port_number=23;         
        new Server(port_number);
    }
    
    private void create_Server(int port_number) throws IOException{         
        ss = new ServerSocket(port_number);         
        System.out.println("Server is ready!");         
        while(true){                
            s=ss.accept();                            
            System.out.println(s.getLocalAddress().getHostName() + " was connected!");
            send_con_mes();
            list.put(s,new DataOutputStream(s.getOutputStream()) );
            new ServerThread(s,this).start();
        }           
    }
}

and here is the client part ;

public void start_Chat() {

    try {

        Ip_addr = JOptionPane.showInputDialog("Enter the IP number of the server to connect : ");
        s = new Socket(Ip_addr, 23);
        
        Client_name = JOptionPane.showInputDialog("Enter your Nickname : ");
        
        dis = new DataInputStream(s.getInputStream());         
        dos = new DataOutputStream(s.getOutputStream());
        new Thread(Client.this).start();

well I can talk, send private messages etc. When I connect to server on my computer as clients, but the final problem is a client from another IP cannot get connected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2024-12-10 02:25:57

您必须配置网络以允许访问此端口。这意味着在您的 PC 和路由器等上启用防火墙。在 Java 中您无法做任何事情来避免必须首先做好这一点。

编辑:如果另一台计算机尝试通过 Internet 路由器连接到您,他们将必须使用您的公共 IP 地址而不是您的内部 PC 地址。如果您不知道自己的公共地址,可以使用 http://whatismyipaddress.com/ 等网站。除非您有静态 IP 地址,否则它会在您重新连接时发生变化。 (始终保持联系的原因之一)

You have to configure your network to allow this port to be accessed. This means enabling firewall on you PC, and your routers etc. There is nothing you can do in Java to avoid having to get this right first.

EDIT: If the other machine is trying to connect to you via an Internet router they will have to use your public IP address rather than your internal PC address. If you don't know your public address you can use a site like http://whatismyipaddress.com/. Unless you have a static IP address it can change when you reconnect. (One reason to stay connected all the time)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文