我正在设计一个新服务器,它需要支持数千个UDP连接(大约100,000个会话).有哪些输入或建议可供使用?
我看过一些select()与poll()or相比的文章epoll(),我看到很多指南讨论了select()多个套接字的实际用法.
但是,我似乎无法找到的是与没有阻塞的无阻塞recv()呼叫的比较select().如果只有1个套接字要读取,1个套接字要写入,是否有任何理由使用该select()呼叫?该recv()方法可以设置为WSAEWOULDBLOCK在没有可用数据的情况下不阻塞并返回错误(),那么select()当您没有其他套接字要检查时,为什么还要打扰?非阻塞recv()调用是否慢得多?
我看着之间的差异poll,epoll和select。我不知道是什么时候应该使用select。鉴于此poll,我看不到任何优势,并且epoll拥有一切,select还有更多。
我正在使用非阻塞套接字(C/C++)编写网络通信程序select.该程序非常大,所以我无法上传源代码.在非常激进的测试会话中,我使用测试代码频繁地打开和关闭TCP和UDP.总是最终一端没有响应并且CPU使用率超过98或99%.然后我用gdb来附加."bt"显示以下内容:
0x00007f1b71b59ac3 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:82
82 ../sysdeps/unix/syscall-template.S: No such file or directory.
in ../sysdeps/unix/syscall-template.S
Run Code Online (Sandbox Code Playgroud)
它可能是什么类型的错误?
$ uname -a
Linux kiosk2 2.6.32-34-generic #77-Ubuntu SMP Tue Sep 13 19:39:17 UTC 2011 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud) $ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.poll
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'poll'
Run Code Online (Sandbox Code Playgroud) 这个想法是创建一个二叉进程树,将信息沿着树发送,然后将值发送回树,在树上升时聚合信息。
我遇到的问题是使用来select()确定管道何时准备好读取。在我到目前为止所写的内容中,第一个管道(第一个左子管道)能够接收信息并打印它;然而,第二个管道(第一个右子管道)在接收任何信息之前超时。我不知道为什么,因为第一根管道工作得很好。
每个孩子最终都会创建自己的孩子,如果没有更好的处理,我什至无法开始这部分过程select()。
int bit_count(char *passed, int len){
//initialize file descriptors
int fd1[2] = {1, 2};
int fd2[2] = {3, 4};
fd_set read_set;
fd_set write_set;
//set timeval structure for timeout
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;
FD_ZERO(&read_set); //clear the set
FD_SET(fd1[0], &read_set); //add first file descriptor to set
FD_SET(fd2[0], &read_set); //add second file descriptor to set
//open the pipes
pipe(fd1);
pipe(fd2);
//fork a child process
pid_t kid = fork();
if(kid == -1) printf("forking …Run Code Online (Sandbox Code Playgroud) 根据我从这个帖子得到的答案,我创建了这个:
//Server
sock_init(); //from SFL, see http://legacy.imatix.com/html/sfl/
timeout = 50000;
serv_sock_input[0] = TCP(1234);
serv_sock_input[1] = UDP(9876);
input_protocols[0] = "tcp";
input_protocols[1] = "udp";
while (1)
{
FD_ZERO(&sock_set);
for (x = 0; x<number_of_inputs; x++)
{
FD_SET(serv_sock_input[x], &sock_set);
}
select_timeout.tv_sec = timeout;
select_timeout.tv_usec = 0;
if (select(0, &sock_set, NULL, NULL, &select_timeout) == 0)
printf("No requests");
else
{
for (x = 0; x<number_of_inputs; x++)
{
if (FD_ISSET(serv_sock_input[x],&sock_set))
{
printf("\nRequest on port %d: \n", x);
if ((strcmp(input_protocols[x],"tcp")) == 0) //in this case, 0 …Run Code Online (Sandbox Code Playgroud) 我正在读一本关于C中网络编程的书.它是从2004年开始的.在示例代码中,作者使用select C函数接受来自客户端的多个连接.这个功能今天已被弃用吗?
我看到有不同的方法可以接受多边形I/O,比如poll和epoll.有什么好处?
我有一个客户端和服务器,客户端运行一个select循环以在TCP和UDP连接之间进行多路复用。我正在尝试将我的TCP连接文件描述符同时添加到read和write集中,然后使用writeset和1使用readset 发起一个消息交换。我与设备的消息通信write正常,但与read设备无法通信。
客户代码:
char buf[256] = {};
char buf_to_send[256] = {};
int nfds, sd, r;
fd_set rd, wr;
int connect_init = 1;
/* I do the Connect Command here */
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_SET(sd, &rd);
FD_SET(sd, &wr);
nfds = sd;
for(; ;){
r = select(nfds + 1, &rd, &wr, NULL, NULL);
if(connect_init == 0){
if(FD_ISSET(sd, &rd)){ // this is not working, if I change rd to wr, …Run Code Online (Sandbox Code Playgroud)