[英]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. 请检查以下内容。
Is the server computer behind a network proxy ? 服务器计算机位于网络代理之后吗?
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中访问它?
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.