如何在客户端 Socket 上等待 ServerSocket 连接

发布于 2024-12-20 21:52:46 字数 1003 浏览 1 评论 0原文

我想知道如何在 Socket 客户端等待 ServerSocket 连接。

当我首先执行服务器(发送)时,它会使用 ServerSocket.accept 方法等待客户端连接。问题在于,如果服务器没有先执行,客户端(接收)就无法执行。我想添加一个条件以允许客户端等待来自服务器的连接。是否可以?

发送(服务器)

ServerSocket servsock = new ServerSocket(1234);
Socket sock = servsock.accept();

接收(客户端)

Socket sock = new Socket(from, 1234);

错误(客户端)

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)

谢谢。

I am wondering about, how I can wait a ServerSocket connection on Socket client side.

When I execute the server (send) first, it waits the client connection with the method ServerSocket.accept. The problem is that the client (receive) cannot be executed without the server has been executed first. I would like add a condition to allow the client to wait the connection from the server. Is it possible?

SEND (server)

ServerSocket servsock = new ServerSocket(1234);
Socket sock = servsock.accept();

RECEIVE (client)

Socket sock = new Socket(from, 1234);

ERROR (client)

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)

Thank you.

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

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

发布评论

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

评论(2

茶底世界 2024-12-27 21:52:46

没有内置的方法可以做到这一点。您必须在客户端捕获异常,并自己实现重试机制。

伪代码:

Socket sock = null;
while (retryCounter < threshold) {
  try {
   retryCounter++;
   sock = new Socket(form, 1234);
  } catch (...) {
    // handle exceptions
    // possibly add a sleep period
  }
}
if (sock == null) {
  // you failed to connect
} else {
  // you're connected
}

如果您有 GUI,请小心:您可能需要在单独的线程中实现其中一些内容或使用计时器来避免冻结您的 UI。

There is no built-in way to do that. You'll have to catch the exception on the client side, and implement a retry mechanism yourself.

Pseudo-code:

Socket sock = null;
while (retryCounter < threshold) {
  try {
   retryCounter++;
   sock = new Socket(form, 1234);
  } catch (...) {
    // handle exceptions
    // possibly add a sleep period
  }
}
if (sock == null) {
  // you failed to connect
} else {
  // you're connected
}

Be careful if you have a GUI: you might need to implement some of that in a separate thread or use timers to avoid freezing your UI.

对风讲故事 2024-12-27 21:52:46

感谢马特,它起作用了。

    Socket sock = null;
    while (true) {
      try {
       sock = new Socket(from, 1234);
       if (sock != null) { break; }
       }
      catch (IOException e) { Thread.sleep(1000); }
    }
    // rest of the code

Thank to Mat, it works.

    Socket sock = null;
    while (true) {
      try {
       sock = new Socket(from, 1234);
       if (sock != null) { break; }
       }
      catch (IOException e) { Thread.sleep(1000); }
    }
    // rest of the code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文