Threaded Socket communication problem

Hi all.
I have in my code two socket connections, one to a server and one
TCPServer myself.
the script does relaying the traffic from one entry to the other and
vice versa.

socket_forward = TCPSocket.open(“localhost”, local_port)
loop {
Thread.start(listen_serv.accept) do |listen_client|

          from_client_to_service = Thread.start do
            loop {
              message = listen_client.gets
              if message != nil
                if message != "" && message != "\n" &&

message.length > 3
… do something
socket_forward.puts(forward_str)
end
end
}
end

          from_service_to_client = Thread.start do
            loop {
              response_str = socket_forward.gets
              if response_str != nil
                 ... do sometihng
                listen_client.puts(dec_resp)
              end

            }
          end
      end
     }

My problem ist that i run into an odd situation. A client send messages
to the socket “listen_client” on the other side, but my script does not
recognize it. The thread from_client_to_service is still at the gets
line. But when i terminate the client, the messages will be received.
This happens every time after a different number of messages. I Think
it’s a kind of deadlock, but i can’t see it.

Yeah, thread debugging is quite hard. Did you try replacing gets/puts
with sysread(4096)/write + a buffer ? Also, don’t forget to set
“Thread.abort_on_exception = true” to not miss any errors.

Cheers,
zimbatm

2011/1/13 Dominic G. [email protected]:

My problem ist that i run into an odd situation. A client send messages
to the socket “listen_client” on the other side, but my script does not
recognize it. The thread from_client_to_service is still at the gets
line. But when i terminate the client, the messages will be received.
This happens every time after a different number of messages. I Think
it’s a kind of deadlock, but i can’t see it.

Your code could be easily implemented with EventMachine in a single
thread (non blocking reactor fashion). It would be really easier than
handling threads (and debugging them).

ok. How would i do it?

“Iñaki Baz C.” [email protected] wrote in post #974483:

2011/1/13 Dominic G. [email protected]:

My problem ist that i run into an odd situation. A client send messages
to the socket “listen_client” on the other side, but my script does not
recognize it. The thread from_client_to_service is still at the gets
line. But when i terminate the client, the messages will be received.
This happens every time after a different number of messages. I Think
it’s a kind of deadlock, but i can’t see it.

Your code could be easily implemented with EventMachine in a single
thread (non blocking reactor fashion). It would be really easier than
handling threads (and debugging them).

No, but i will give it a try…
zimbatm … wrote in post #974464:

Yeah, thread debugging is quite hard. Did you try replacing gets/puts
with sysread(4096)/write + a buffer ? Also, don’t forget to set
“Thread.abort_on_exception = true” to not miss any errors.

Cheers,
zimbatm

My problem ist that i run into an odd situation. A client send messages
to the socket “listen_client” on the other side, but my script does not
recognize it. The thread from_client_to_service is still at the gets
line.

Make sure it is flushing its buffers. You could use wireshark to “make
sure” the right bytes are transmitted. Make sure it is transmitting a
string with “\n” at the end, too.
GL.
-r