如果 ServerSocket 接受连接,它会继续等待进一步的请求吗?
我正在使用 ServerSocket(8080,1,InetAddress.getByName("127.0.0.1"))
现在在accept方法中我从SS获取Socket。我的问题是,一旦我获得 Socket 并继续处理,如果在处理完成之前收到另一个请求,ServerSocket 会接受该请求吗?
更新:我有一个 while 循环,如下面的答案所示,它接受连接。我的疑问是,对于这个实例化,如果我继续处理我的请求,并且如果另一个连接请求进来,它会被接受吗?
I am using
ServerSocket(8080,1,InetAddress.getByName("127.0.0.1"))
Now in the accept method I obtain the Socket from SS. My question is once I get the Socket and continue with my processing if another request comes in before my processing is complete, will ServerSocket accept that request?
Update: I have a while loop as in the answer below which accepts the connection. My doubt is with this instantiation if I continue with processing of my request and if another connection request comes in will it be accepted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您再次调用accept方法,它只会接受请求,因此如果您想接受多个连接,您可以有一个仅旋转调用accept方法的线程,如下所示:
It will only accept requests if you call the accept method again, so if you wanted to accept multiple connections, you could have a thread that just spins calling the accept method, like so:
由于您构建的 ServerSocket 的积压为 1,因此一次可能只有一个未处理(不是
accept()
ed)的连接。所有额外的连接尝试都将被拒绝。换句话说,backlog 参数指定存储连接的队列的大小,直到它们被程序接受为止。Since you have constructed this ServerSocket with a backlog of 1 there may be only one unprocessed (not
accept()
ed) connection at a time. All additional connection attempts will be refused. In other words, the backlog parameter specifies the size of a queue which stores connections until they are accepted by your program.它将处于待处理状态,直到您再次调用
accept
为止。如果您收到多个请求,则最多将保留一定数量的请求,直到您接受
。该限制是服务器套接字的队列大小。It will be in a pending state until you call
accept
again. If you get multiple requests coming in, then up to a certain number will be kept queued until youaccept
. That limit is the queue size of the Server Socket.