Saturday, March 24, 2012

system.net.socket question

I have the following code

Public Sub StartListening()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](128000) {}

' Establish the local endpoint for the socket.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, XXXXX)

' Intializes a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)

' Bind the socket to the local endpoint and listen for incoming
' connections.
Try
listener.Bind(localEndPoint)
listener.Listen(100)

While True
' Set the event to nonsignaled state.
allDone.Reset()

' Start an asynchronous socket to listen for connections.
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), _
listener)

' Wait until a connection is made before continuing.
allDone.WaitOne()
End While
Catch e As Exception
'Print out error message to log file
' Console.WriteLine(e.ToString()) '************
End Try
End Sub

Private Sub AcceptCallback(ByVal ar As IAsyncResult)
' Signal the main thread to continue.
allDone.Set()

' Get the socket that handles the client request.
Dim listener As Socket = CType(ar.AsyncState, Socket)
Dim handler As Socket = listener.EndAccept(ar)

' Create the state object.
Dim state As New StateObject
state.xSocket = handler
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, _
New AsyncCallback(AddressOf readCallback), state)

End Sub

how do I get the IP address from the clients that connect to my sever?nevermind...i figured it out

Trey

0 comments:

Post a Comment