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.
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).
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.