gpt4 book ai didi

sockets - D语言Socket.receive()

转载 作者:行者123 更新时间:2023-12-02 03:47:50 31 4
gpt4 key购买 nike

我在 Windows 上使用 D 编写了一个套接字服务器,现在我想将它移植到 Linux 上。这是代码摘要:

/*
* this.rawsocks - SocketSet
* this.server - Socket
* this.clients - array of custom client worker class
*/

char[1] buff;
string input;
int nbuff = 0;

while (this.isrun) {
this.rawsocks.add(this.server);

if (Socket.select(this.rawsocks, null, null)) {
if (this.rawsocks.isSet(this.server)) {
// accepting a new connection
}

foreach (ref key, item; this.clients) {
// works for all connections
writeln("test1");

// mystically interrupts foreach loop
nbuff = item.connection.receive(buff);

// works only for the first connection.
// when the first connection is closed, it works for the next
writeln("test2");
}
}

this.rawsocks.reset();

foreach (key, value; this.clients)
this.rawsocks.add(value.connection);
}

item.connection.receive(buff) 在 Windows 上运行良好,但在 Linux 上会中断 foreach 循环。没有任何异常,下一个客户端的 test2 在第一个客户端断开连接时触发。

.receive() 方法在 Linux 中是否有一些特殊的行为,或者我的实现有一些问题?

最佳答案

这个问题的解决方案和问题本身一样奇怪:)

foreach (ref key, item; this.clients) {
/*
* The solution
* Check of client's socket activity in SocketSet queue may not be necessary in Windows, but it is necessary in Linux
* Thanks to my friend's research of this problem
*/
if (!this.rawsocks.isSet(item.connection)) continue;

// works for all connections
writeln("test1");

// after modifying not interrupts foreach loop
nbuff = item.connection.receive(buff);

// after modifying also works for all connections
writeln("test2");
}

关于sockets - D语言Socket.receive(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15717989/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com