我在Windows上用D写了一个socket服务器,现在我想把它移植到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中有没有什么特殊的行为,或者在我的实现中有什么问题?
发布于 2013-04-01 03:59:25
这个问题的解决方案和问题本身一样奇怪:)
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");
}https://stackoverflow.com/questions/15717989
复制相似问题