如何限制客户端服务器程序连接数 [英] how to restrict number of connections in client server program

查看:188
本文介绍了如何限制客户端服务器程序连接数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这应该接受一个连接的唯一最大的,它应该放弃其他连接的服务器程序。我怎样才能做到这一点?

I want a server program which should accept only maximum of one connection and it should discard other connections. How can I achieve this?

推荐答案

只有接受()的单一连接。

下面是一个典型的服务器例行程序:

Here is a typical server routine:

s = socket(...);
bind(s, ...);
listen(s, backlog);
while (-1 != (t = accept(s, ...))) {
    // t is a new peer, maybe you push it into an array
    // or pass it off to some other part of the program
}

每完成 接受() 通话,返回一个新的连接的文件描述符。如果你只是想收到的的连接,只有接受()一次。大功告成之后,这个听presumably,因此,关闭您的服务器太:

Every completed accept() call, returns the file descriptor for a new connection. If you only wish to receive a single connection, only accept() once. Presumably you're done listening after this, so close your server too:

s = socket(...);
bind(s, ...);
listen(s, backlog);
t = accept(s, ...);
close(s);
// do stuff with t

如果您希望只一次处理一个单一的连接,该连接结束后,恢复听力,然后执行上面的接受()循环,不接受直到进一步的连接 T 已关闭。

If you wish to only handle a single connection at a time, and after that connection closes, resume listening, then perform the accept() loop above, and do accept further connections until t has closed.

这篇关于如何限制客户端服务器程序连接数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆