C++ 在同一unix套接字连接中读取元素两次时出错

C++ 在同一unix套接字连接中读取元素两次时出错,c++,ruby,sockets,unix-socket,C++,Ruby,Sockets,Unix Socket,我面临一个相当奇怪的问题。我在ruby和C之间创建了一个unix套接字服务器,在C端,一个主线程创建并侦听套接字,在接受或连接时,让一个线程(从线程池)执行信息的读取和处理 我发现每当我有高负载时,我的accept()都会接受2到3次相同的连接。这通常伴随着客户端在连接上向我抛出一条本应发送的“断管”消息 例如: 客户: Sending 1 Sending 2 Sending 3 Error is Broken pipe Sending 4 Sending 5 ... 服务器: New con

我面临一个相当奇怪的问题。我在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
更新

我已经看到使用socket.send或socket.write代替socket.puts不会出现任何“断管”错误,因此在C端没有重复的接受。尽管如此,我还是在网上查过了,它们之间唯一的区别似乎是socket.put在消息末尾添加了一个换行符。我想知道为什么puts不起作用,建议使用哪一种(发送或写入)。

新连接是2
New connection is 2
<--- You probably have closed connection 2 here. Double check your code
New connection is 2 <<<< this should not be here!
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
New connection is 2
<--- You probably have closed connection 2 here. Double check your code
New connection is 2 <<<< this should not be here!