how to make a socket raise events???

peanutman
12-06-2004, 12:43 PM
i'm pretty new to VB.NET , and have a question i haven't found an answer for yet.

In VB6 if you added a Winsock control, it would have this event that is raised every time data arives at the sock.

i am now using sockets in VB.NET. i found out how to retrieve data at any time. but this is anoying since i can't predict when data is going to come.
isn't there a way that could make a socket raise an event or something every time data arives... it would make my life easier..

Reinout,

excaliber
12-06-2004, 01:03 PM
Short answer: Not really

Long answer: Well, yes, but it works on a different theory. There are two ways you could go about it, but both take you out of "synchronous" socket programming. You could spawn a new thread that calls .Recieve (since it blocks, you dont want it on the main thread), and when it gets data it raises an event via a delegate or WithEvents/event (essentially the same thing, delegates just give you more control).

But, if you go that route, the sockets already have something like that built in. It is called asynchronous sockets. Unfortunately, this makes programming sockets far more difficult (though not impossible, and you get many many benefits) as the sockets work independantly of the main thread execution and is all done through Callback delegates (in a thread pool). Synchronizing data becomes a hassle because of the threading, you need to keep states of each socket as the same thread is not guaranteed to work on the same socket each time (due to thread pool), the callback delegates often times end up calling other callback delegates to get/send more information, and sharing data is extremely daunting.

That said, it provides extremely scalable interface, high performance, etc etc. Just more to think about.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum