Threaded Socket communication problem.

D

Dominic Gatzen

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.
 
J

Jonas Pfenniger (zimbatm)

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
 
I

Iñaki Baz Castillo

2011/1/13 Dominic Gatzen said:
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. =C2=A0The thread from_client_to_service is still at the get= s
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).


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
R

Roger Pack

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top