Re: DataReceived type event for tcpclient network stream?



On Sun, 17 Feb 2008 03:27:15 -0800, JamesB <jamesb@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I have a service and a client forms app - I want to be able to have the service send data back to the client app intermittently, and I've established a connection between the two using tcpclient.
What I can't see is any sort of event I can use that will be triggered when data is received at the client. Is there such a thing?
Or should I create a separate "listening" thread that polls the connection for data and then triggers whatever I need to happen? Or is there a better way?

IMHO, the "best" way is to use the async API, which provides something sort of like events, albeit with a bit more complexity. It works in a very similar way though, at least when you are using the callback aspect of it.

In particular, the various classes -- TcpClient, NetworkStream, Socket -- include methods that start with the word "Begin" or "End". These methods all represent an asynchronous version of a synchronous method. You start by calling the "Begin" method, and when the operation completes, you are notified via some mechanism at which point you call the "End" method, providing your own code with the results of the operation.

The standard .NET async model provides three different ways to know that an operation is completed: IAsyncResult.IsCompleted property, which you can poll (generally the wrong thing to do); IAsyncResult.AsyncWaitHandle property, which you can wait on (better than polling, but blocks a thread and so doesn't usually wind up better than just dedicating a thread using the synchronous method); and a callback delegate, which you provide when you call the "Begin" method, and which is called when then operation has actually completed.

The last technique is IMHO the best, and it's pretty much the same behavior that an event would provide. The main differences being that you pass the delegate to a method (the "Begin" method) rather than adding it to an event, and that because of that you can only have one delegate "subscribed" to the "event" at a time.

Pete
.



Relevant Pages

  • Re: Simple TCP Socket Communicator - Please Help
    ... An established connection was aborted by the software in your host ... TcpClient to receive data from server, ... private StreamReader _reader; ... Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework)
  • Re: Using Winsock / TCP Client
    ... You might be needing to dispose of the tcpClient fully prior opening a new ... build and release the connection between sessions. ... > I can stream code to the printer multiple times with no delays. ... > lose the immediate printing until I shut down the application. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: TcpClient Problem
    ... Paul T. ... can ping the host PC from the device successfully using the same IP ... address as I am using in the TcpClient I have checked the wireless ... listener object waiting for a connection on the same port number. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: TcpClient close() method socket leak
    ... TIME_WAIT is normal behaviour for proper TCP session closure. ... implementation of a socket states Maximum Segment Lifetime to be 30 seconds, ... The protocol places no restriction on a particular connection being ... > limitation w/ the TcpClient or is there something else i could try? ...
    (microsoft.public.dotnet.general)
  • Re: Using ad.domain.com instead of just domain.com or domain.local?
    ... > wouldn't resolve public IP addresses if on the VPN. ... delegate the ad subdomain in the public zone there is little change because ... If you use ad.domain.com and the name is delegated in the public domain to ... connection, it just becomes a dead end connection. ...
    (microsoft.public.windows.server.dns)