OK, you're now showing us a different bit of code from before. The first snippet was calling EndConnect, so you want to find the corresponding call to BeginConnect. (That's why I asked what you were passing to BeginConnect in my previous post.) However, you've got the same problem in the code example you've posted here as I think you've probably got in your connect code, so with luck I can kill two birds with one stone here.
What you're using here is an example of the 'async pattern' in .NET. Whenever you see a pair of methods such as BeginConnect and EndConnect, or BeginRead and EndRead, it's usually an example of this pattern, and they all use the AsyncState property in the same way. The idea is this: you call the BeginXxx method, passing input parameters for the operation plus two extra parameters. One of these is the completion callback - the function that will be called when the operation is complete. In your most recent example, that's what the "AddressOf StreamReceiver" is. But the code that will be relevant to the problem you described in your first post will be using AddressOf ConnectCallback (and it will be called BeginConnect). The final parameter, the one that goes after the completion callback, is usually called 'state'. You can pass in any object you like. That object will then be available to you on the AsyncState property of the IAsyncResult object passed to your completion callback. .NET doesn't attribute any particular significance to this object, and doesn't care what you pass in. It just makes sure that whatever you pass in as the state parameter is the same thing that it hands back from the AsyncState property. It's a place for you to put stuff. In your most recent example, your code looks like this: > Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf > StreamReceiver, Nothing) That final parameter is the state parameter. You're passing in Nothing here, so in the StreamReceiver function, Nothing is what you'll get back out from ar.AsyncState. Since your StreamReceiver appears to expect ar.AsyncState to contain the socket, it's your job to put the socket in there when you call BeginRead. So your call should look like this: Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, _ AddressOf StreamReceiver, client) So rather than passing Nothing as the final state parameter, pass the socket. Now, when your StreamReceiver function runs, and retrieves the AsyncState parameter with this code here: > Dim ClientSocket As Socket = CType(ar.AsyncState, Socket) this will get back the socket as you require, because that's now what you've passed in as the state parameter. (So the code that throws an exception is actually correct - the problem lies elsewhere. It's failing because you passed in 'Nothing' at the call to BeginRead - fix that, and the StreamReceiver code will now be able to do its job correctly.) Since the original problem you posted indicated that you were having problems at the Connect phase, you will also need to find the call to BeginConnect, and make sure that you also pass the socket as the state parameter there. You said in your original post that you were working from the "Using an Asynchronous Client Socket" sample in the help. Here's the code that it tells you to use when calling BeginConnect: client.BeginConnect(remoteEP, _ AddressOf ConnectCallback, client) That final parameter is the state parameter, and as you can see, they're passing in the socket. Likewise, here's the code they present for the send: client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, _ AddressOf SendCallback, client) Again, they've passed in the socket as the final state parameter here. I'm not sure why in the code snippet you've posted that you've decided to modify Microsoft's sample code by passing Nothing instead of passing the socket, but that's the reason you're getting the exception here. -- Ian Griffiths - DevelopMentor http://www.interact-sw.co.uk/iangblog/ > -----Original Message----- > From: Peter Suter > > I'd say stick with the obvious, but please back up a bit to where it's not > obvious to a newbie. > ar is System.Net.Sockets.OverlappedAsyncResult > ar.AsyncState is null > > how do I get a socket/state and pass it? > > Public Sub New(ByVal client As TcpClient) > Me.client = client > Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf > StreamReceiver, Nothing) > End Sub > > Private client As TcpClient > Private readBuffer(READ_BUFFER_SIZE) As Byte > Private strName As String > > Private Sub StreamReceiver(ByVal ar As IAsyncResult) > 'get the clients ip address > Dim ClientSocket As Socket = CType(ar.AsyncState, Socket) > Dim ClientSocket As Socket = client.??? > Dim ClientIPAddress as string =ClientSocket .RemoteEndPoint.ToString()) > > ----- Original Message ----- > From: "Ian Griffiths" <[EMAIL PROTECTED]> > > At the risk of going for the obvious question, are you actually passing > in the socket as the 'state' parameter when you call BeginConnect? If > 'client' is null in this situation, that implies that ar.AsyncState was > also null, which implies that you passed in null as the 'state' > parameter when you called BeginConnect. > > > > -----Original Message----- > > From: Peter Suter > > > > After "Dim client As Socket = CType(ar.AsyncState, Socket)", > > client = nothing, and any attempt to use client throws > > > > "An unhandled exception of type System.NullReferenceException occurred > in > > ... > > Object reference not set to an instance of an object' > > > > So, my issue seems to be not that I get an exception, but that > > client=Nothing in the first place. > > > > Thanks for any suggestions > > > > ----- Original Message ----- > > From: "Ian Griffiths" <[EMAIL PROTECTED]> > > > > What is the exception that you're getting? > > > > > -----Original Message----- > > > From: Peter Suter > > > > > > Hi All, > > > In a sub much like this one, from "Using an Asynchronous Client > Socket > > > [Visual Basic]", I keep getting an exception rather than a socket. > > > Is there a problem getting the socket using this technique? > > > (I'm trying to get the Client IP Address) > > > > > > Private Shared Sub ConnectCallback(ar As IAsyncResult) > > > Try > > > ' Retrieve the socket from the state object. > > > Dim client As Socket = CType(ar.AsyncState, Socket) > > > > > > ' Complete the connection. > > > client.EndConnect(ar) > > > > > > Console.WriteLine("Socket connected to {0}", _ > > > client.RemoteEndPoint.ToString()) > > > > > > ' Signal that the connection has been made. > > > connectDone.Set() > > > Catch e As Exception > > > Console.WriteLine(e.ToString()) > > > End Try > > > End Sub 'ConnectCallback =================================== 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