> Dim ClientSocket As Socket = Me.Client
> which should(?) be
> Dim ClientSocket As Socket = Myclient.Client

> but then I get property client() is not accessible in this
> context because it's protected
> 
> Can you tell me how to get the socket in this context, or do
> I need to pick it up from the TcpListener?

I think the important thing to understand here is that there isn't
really one right way of doing this.  This makes it hard to provide an
answer to general questions like "how do I get the socket"?  The answer
is usually: "That depends. How would you like to get the socket?"

The whole idea behind the AsyncState property is that you can build
whatever you like around it.

The fact that you're getting that compiler error implies to me that
you've got your handler function on the wrong class. If you write an
async completion handler that doesn't have access to the socket it is
using, then something is wrong with your design - you shouldn't be
trying to handle completion notifications for a socket on a class that
doesn't actually have access to that socket!  Have you considered moving
the completion handler function into the class that owns the socket, so
you won't get that "not accessible" error?

I realise that this advice is far to generalized to provide you with a
specific answer to your specific question, but unfortunately it's the
best I can do with the information available.  We can only see small
fragments of your code, and I'm having trouble relating the snippets to
each other, so it's pretty hard to guess what the right thing to do is.
(Especially since my main language is C#, not VB.NET...)  But regardless
of that, the important thing is to make sure that your design is clear
about which object 'owns' the socket, and to make sure that all the code
that needs to interact with that socket lives in that code.

If you need other classes to do things when certain socket events occur,
it might be best to have the class that owns the socket raise events,
and make those other objects subscribe to those events. I'm not sure,
but I'm getting the impression that you're trying to write completion
handlers for socket events directly in these other classes. This is
going to get you into trouble in the long run, because it means that you
have classes which make use of each others' internal implementation
details, which always causes maintenance headaches.  It's usually better
to provide some kind of insulation here, and events are a good way of
doing that.  So the object that owns the socket will provide the
completion handler routines.  If other objects care about the completion
of these socket operations, then make the socket-owning object expose
events, and have it raise those when these operations complete.  That
way, there's no requirement for the other classes to interact directly
with the socket - they can just write handlers for "We just got some
data" or "We just got connected" events without needing to deal with the
low level details. 



-- 
Ian Griffiths - DevelopMentor
http://www.interact-sw.co.uk/iangblog/

> -----Original Message-----
> From: Peter Suter
> 
> Thanks, that helps a lot.
> My biggest problem is probably the MS example I chose to build from.
> - passing nothing in the state parameter
> - I was inside a class called 'client', making it a bit tough to get
the
> TCPClient.client (socket)
> 
> So, I think I've sorted that out with
> 'class UserConnection, the class that was called "client"
> Public Sub New(ByVal Myclient As TcpClient)
> Me.clientConn = Myclient
> Dim ClientSocket As Socket = Me.Client  'requires Inherits TcpClient
> Me.clientConn.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
> AddressOf
> StreamReceiver, ClientSocket)
> End Sub
> 
> Private Sub StreamReceiver(ByVal ar As IAsyncResult)
> Dim ClientSocket As Socket = CType(ar.AsyncState, Socket)
> 
> but I still get a "null" exception error here
> Dim Clientaddress As String = ClientSocket.RemoteEndPoint.ToString()
> 
> which makes sense if the problem is "me", in
> Dim ClientSocket As Socket = Me.Client
> which should(?) be
> Dim ClientSocket As Socket = Myclient.Client
> but then I get property client() is not accessible in this context
because
> it's protected
> 
> Can you tell me how to get the socket in this context, or do I need to
> pick
> it up from the TcpListener?
> 
> Private Sub DoListen()
> listener = New TcpListener(System.Net.IPAddress.Any, PORT_NUM)
> listener.Start()
> Do
> Dim Connection As New UserConnection(listener.AcceptTcpClient)
> AddHandler Connection.LineReceived, AddressOf OnLineReceived
> Loop Until False

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to