在同一 unix 套接字连接中读取元素两次时出错
Error reading an element twice in the same unix socket connection
我面临着一个相当奇怪的问题。我在 ruby 和 C 之间创建了一个 unix 套接字服务器,在 C 端,主线程创建并侦听套接字,并在接受或连接时让线程(来自线程池)执行信息的读取和处理。
我看到每当我负载高时,我的 accept() 都会接受 2 或 3 次相同的连接。这通常伴随着客户端在实际上应该发送的连接上向我抛出"管道破裂"消息。
例如:
客户:
Sending 1
Sending 2
Sending 3
Error is Broken pipe
Sending 4
Sending 5
...
服务器:
New connection is 1
New connection is 2
New connection is 2 <<<< this should not be here!
New connection is 4
New connection is 5
New connection is 6
我的代码是(C++代码,服务器)
//(... create the socket with standard socket, bind and listen calls)
while(1) {
// Wait for connection
int connection = accept(streamSocket, (struct sockaddr *) &clientAddress, &clilen);
if (connection < 0){
LOG(ERROR) << "Failed to accept new client socket connection, request index = " << requests;
break;
}
else
printf("New connection is %dn", connection);
// Add connection to handler thread in pool
pool.AddJob([this, connection, requests, streamSocket]() {
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
int n = read(connection, buffer, BUFFER_SIZE-1);
//(... processing of the info)
}
close(connection);
});
}
而 ruby 中的客户端(在循环中运行):
request = "whatever json"
for count in 1..10
begin
puts "Sending #{count}"
socket = UNIXSocket.new("/tmp/mysocket.socket")
socket.puts(request)
response = socket.read
socket.close
rescue Exception => e
puts "Error is #{e}"
end
end
更新
我已经看到使用 socket.send 或 socket.write 而不是 socket.put 我没有收到任何"断管"错误,因此在 C 端没有重复接受。尽管如此,我已经在线检查过,它们之间唯一的(父级)区别似乎是socket.puts在消息末尾添加了换行符。我想知道为什么看跌不起作用以及推荐哪一个(发送或写入)。
New connection is 2
<--- You probably have closed connection 2 here. Double check your code
New connection is 2 <<<< this should not be here!
当客户端写入服务器已关闭的连接时,它将触发Broken pipe
信号。
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- 使用柯南打包时如何避免列出两次依赖?
- 为什么要执行两次位移((x >> 4)<< 4)?
- 对结构向量进行两次排序
- 在同一 unix 套接字连接中读取元素两次时出错
- (c++)调用一个curl方法两次,给出并出错