简体   繁体   English

如何连接到运行Java服务器的另一台计算机?

[英]How to connect to a different computer running a Java server?

I created a simple echo server in Java. 我用Java创建了一个简单的回显服务器。 When I try it locally, it works as it should. 当我在本地尝试时,它可以正常工作。 However, when I try to connect it from a different computer using the IP address and the port number the server is running on, it never connects. 但是,当我尝试使用服务器正在运行的IP地址和端口号从另一台计算机连接它时,它永远不会连接。 Is there anything else that should be done to connect to a server from a different computer? 要从其他计算机连接到服务器,还需要做其他事情吗?

import java.net.Socket;
import java.net.ServerSocket;

public class EchoServer {

    public static void main(String[] args) throws Exception {

        // create socket
        int port = 4444;
        ServerSocket serverSocket = new ServerSocket(port);
        System.err.println("Started server on port " + port);

        // repeatedly wait for connections, and process
        while (true) {

            // a "blocking" call which waits until a connection is requested
            Socket clientSocket = serverSocket.accept();
            System.err.println("Accepted connection from client");

            // open up IO streams
            In  in  = new In (clientSocket);
            Out out = new Out(clientSocket);

            // waits for data and reads it in until connection dies
            // readLine() blocks until the server receives a new line from client
            String s;
            while ((s = in.readLine()) != null) {
                out.println(s);
            }

            // close IO streams, then socket
            System.err.println("Closing connection with client");
            out.close();
            in.close();
            clientSocket.close();
        }
    }
}

Please check the following things. 请检查以下内容。

  1. Is the server computer behind a network proxy ? 服务器计算机位于网络代理之后吗?

  2. Does it have an independent public IP Address by which it is accessible from anywhere ? 它有一个独立的公共IP地址,可以从任何地方访问它吗? Or, does it have an internal IP, by which it can be accessed in your LAN ? 或者,它是否具有内部IP,可以在您的LAN中访问它?

  3. Make sure FireWalls has an exception for port 4444. Or you may turn it of in both client and server. 确保FireWalls具有端口4444的例外。或者您可以在客户端和服务器中都将其关闭。

If it does not help, post the exception you are getting (by editing the question). 如果没有帮助,请发布您正在获取的异常(通过编辑问题)。 Or the server program is just freezing without any error ? 还是服务器程序只是冻结而没有任何错误?

If this is on your LAN refer to the machine running your EchoServer by name (the actual machine name, I believe they show you to do it this way on the Sun Tutorial that posted this echo server excercise correct?). 如果这是在您的LAN上,请参考按名称运行EchoServer的计算机(实际的计算机名称,我相信他们会在发布了此echo服务器练习正确的Sun教程中向您显示以这种方式进行操作?)。 If that works it would help a lot in troubleshooting the issue. 如果可行,将有助于解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java SQL连接器连接到另一台计算机上的数据库 - How can the java SQL connector be used to connect to a database on a different computer 如何使Java客户端使用套接字连接到不在我的计算机上的服务器? - How to make a java client use a socket to connect to a server not on my computer? 如何使用Java编程连接客户端计算机以使用安装在服务器计算机中的Sqlite数据库? - how to connect client computer to use Sqlite database which is installed in the server computer using java programming? Java,使用GUI连接到另一台计算机/服务器的文件系统 - java, connect to filesystem of another computer/server with a gui 如何通过网络从计算机2连接到位于计算机1上的mysql服务器? - How to connect to mysql server located on computer 1 from computer 2 through the network? 如何从另一台计算机连接到SQL Server - How to connect to a sql server from another computer 如何使用Java中的密码保护连接到系统上运行的服务器? - How to connect to server running on the system with password protection in Java? 如何在java中用密码保护连接到系统上运行的服务器? - How to connect to server running on the system with password protection in java? 如何检查进程是否在客户端计算机上(在服务器端)运行? [Java] - How to check if a process is running on a client's computer (on server-side)? [Java] 使用一台计算机在 java 中多次连接到服务器 - Connect to a server multiple times in java using one computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM