0

我创建了两个独立的程序,一个客户端和一个服务器。我尝试在 eclipse 中测试它们并在同一台(Windows)机器(localhost)上作为可运行的 jar 运行。它完全按照应有的方式工作。

但是,当我将 clinet(以及后来的服务器)发送给我的一个朋友进行测试时,它没有用。我们确保端口是开放的(即使在客户端),但无济于事。它没有用。我只会得到一个超时ConnectException。

我使用的插座是 50178-50180。

我不知道该怎么想。有什么想法可能会出错吗?

这是套接字代码:

(serverside)
serverSocket = new ServerSocket(50178);

while (true)
{
    clientSocket = serverSocket.accept();

    streamOut = clientSocket.getOutputStream();
    objectOut = new ObjectOutputStream(streamOut);
    streamIn = clientSocket.getInputStream();
    objectIn = new ObjectInputStream(streamIn);

    (stuff)
}

(clientside)
Socket socket = new Socket(ipAddress, 50178);

OutputStream streamOut = socket.getOutputStream();
DataOutputStream dataOut = new DataOutputStream(streamOut);
DataInputStream dataIn = new DataInputStream(socket.getInputStream());

我没有包含其余的代码,因为它是 I/O 的东西(它确实有效,因为它在 localhost 上工作)。

编辑:我添加了请求的代码。我还在同一网络上的两台计算机上进行了尝试(女巫本地 ip 192.168.1.*)。有效。

4

1 回答 1

-1

You may already be doing this, but I believe that a call to serverSocket.accept() must be done. This will tell the server that it needs to wait for a connection.

If you are doing that, my best answer to you would be to perform network troubleshooting. The computers must be able to see each other on the local area network in order to be able to use just the ip address. Make sure that there is not a firewall or something like that that is preventing the connection.

If you could add the code of where the client tries to connect and where the server accepts the connection, that may be helpful.

于 2014-11-01T18:41:51.850 回答