java socket监听而不接受连接?

时间:2022-03-12 07:37:33

I'm new to Java, but not new to programming; I've been around computers since the days of DOS.

我是Java新手,但不是编程新手;从DOS时代开始,我就一直在电脑前工作。

I'm trying to write a simple network proxy, and I want to listen for incoming connection requests, but I don't want to accept the request just yet as the client will then immediately send data. I want the proxy to attempt to successfully open a connection to the pre-configured destination before accepting the connection, and drop the connection if the destination cannot be reached.

我正在尝试编写一个简单的网络代理,我想监听传入的连接请求,但我不想接受请求,因为客户端将立即发送数据。我希望代理在接受连接之前尝试成功打开与预配置目标的连接,如果无法到达目标,则删除连接。

This was possible in VB6 way back when, and I believe it's possible to do it in C# using events, but the problem is that the proxy machine is unix and I just don't feel masochistic enough to write anything in C++ just for one system.

这可以用VB6的方式回来的时候,我相信它可以在C#中使用事件来实现,但问题是代理机器是unix而我只是感觉不够自虐而不能只用一个系统在C ++中编写任何东西。

anyone got any ideas on how to do this, or at least a kludge to simulate such a thing? the article I saw here is too vague, and seems to be outdated as oracle docs seem to redirect to a different page.

任何人都有任何关于如何做到这一点的想法,或者至少是一个模拟这样的事情的kludge?我在这里看到的文章太模糊了,似乎已经过时,因为oracle文档似乎重定向到另一个页面。

1 个解决方案

#1


-2  

This isn't possible in Java. The way the socket libraries are written you cannot get details about an incoming connection without accepting it.

这在Java中是不可能的。编写套接字库的方式无法在不接受传入连接的情况下获取有关传入连接的详细信息。

The link you provided would only help in rejecting connections before accepting them but I don't think it would allow you to use a connection the SecurityManager rejected.

您提供的链接仅在接受连接之前有助于拒绝连接,但我认为它不允许您使用SecurityManager拒绝的连接。

Would it be possible to open the connection to the destination before accepting a connection, so then you only accept while you have a connection open to the destination?

在接受连接之前是否可以打开到目的地的连接,那么只有在连接到目的地时才接受?

#1


-2  

This isn't possible in Java. The way the socket libraries are written you cannot get details about an incoming connection without accepting it.

这在Java中是不可能的。编写套接字库的方式无法在不接受传入连接的情况下获取有关传入连接的详细信息。

The link you provided would only help in rejecting connections before accepting them but I don't think it would allow you to use a connection the SecurityManager rejected.

您提供的链接仅在接受连接之前有助于拒绝连接,但我认为它不允许您使用SecurityManager拒绝的连接。

Would it be possible to open the connection to the destination before accepting a connection, so then you only accept while you have a connection open to the destination?

在接受连接之前是否可以打开到目的地的连接,那么只有在连接到目的地时才接受?