Well maybe, but it is very confusing code.
He makes a client style connect to a remote system and port. He then passes the socket, without having checked whether the connection succeeded to two threads.
One of those threads labelled 'sender', sits in a tight loop reading from <> (presumably intended to be STDIN) and sends whatever it reads to the remote system. It never checks whether the socket is connected, or if it is still connected.
The other thread, labelled 'listener' despite that it does use 'listen', sits and attempts to receive anything that arrives on the the port and echo it to STDOUT. This does check whether the port is connected, but just then just falls off the end of the thread. It also just dies if the recv fails. In either case, there is no attempt to coordinate termination with the sender thread, which will just continue to attempt to read and send forever.
The OP claimed that "If I do the same test without threads it works fine", but with an absence of any calls to listen, connect or accept, and no coordination between reads and writes, it is very difficult to see how that could ever be.
Essentially, the OP appears to be leaping into threads without making any attempt to understand the basics of socket communications in a non-threaded environment. I had hoped to solicit a response that would allow me to work further in trying to assist him, but he does seem to want to start at the beginning :(
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|