Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Dec : Re: Thread, blocking socket
| Subject: | Re: Thread, blocking socket |
| Posted by: | "Dirk Claessens" (now..@invalid) |
| Date: | Sat, 30 Dec 2006 22:18:32 |
On 2006-12-30, Richard Carpenter (212.59.199.49) wrote in
message <en63g1$it9$1$8302bc10@news.demon.co.uk>
> Hi all,
>
> I have some code that I have written that *shouldn't* work but it does
> beautifully... which obviously concerns me. I'll explain it in pseudocode but
> if anyone needs to see the actual code then I'll post that too.
>
> I've created a TThread descendent, its Execute method does one thing and that
> is call the "ReadData" private method in a "While not terminated" loop. So
> it's
>
> While not terminated do
> ReadData;
>
> The ReadData method is as follows;
>
> If (RecvFrom(fSocket, fBuffer[0],1,0,fFrom, size) <> SOCKET_ERROR) then
> WriteDataToFile(fBuffer[0])
>
> That's all that's in the ReadData method, very simple and works well. The
> socket is a blocking datagram socket set up for broadcasting on UDP.
>
> I also have a public method called SendData(const value:string) in the
> TThread that takes a string parameter, creates a new socket and broadcasts
> that data across the network then closes the socket.
>
> Now the confusing thing is that when the thread enters the RecvFrom it waits
> like a blocking socket should. *If*, however I call the SendData method from
> my app the thread jumps out of the RecvFrom, actions the SendData method then
> returns back to the RecvFrom to continue waiting for data!
>
> I would have expected the thread to be "locked up" in the RecvFrom until data
> arrives but that doesn't appear to be the case. The whole thing works
> beautifully in that I can send any string data from my app and still be
> receiving data in the RecvFrom function... and I cannot for the life of me
> fathom out why this is happening.
I guess what you observe is simply Delphi's debugger behaviour.
( I assume you have breakpoint on the RecvFrom call or some such)
- ReadData() uses a socket, blocks on RcvFrom(), and runs in the
thread's context;
- Each call to SendData() creates a *new* socket, broadcasts the data,
closes the socket, but runs in the *main* thread's context, not in the
thread's context as I suspect you are assuming.
-Both sockets are unrelated, as long as there no port conflicts.
IOW, this should work...
--
Dirk.
No trees were killed in the creation of this message;
however, many electrons were terribly inconvenienced.
http://users.pandora.be/dirk.claessens2