Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
State object for receiving data from remote device.
Public Class StateObject
Public workSocket As Socket = Nothing Client socket.
Public BufferSize As Integer = 256 Size of receive buffer.
Public buffer(256) As Byte Receive buffer.
Public sb As New StringBuilder() Received data string.
End Class StateObject
Public Class AsynchronousClient
The port number for the remote device.
Private Shared port As Integer = 11000
ManualResetEvent instances signal completion.
Private Shared connectDone As New ManualResetEvent(False)
Private Shared sendDone As New ManualResetEvent(False)
Private Shared receiveDone As New ManualResetEvent(False)
The response from the remote device.
Private Shared response As [String] = [String].Empty
Private Shared Sub StartClient()
Connect to a remote device.
Try
Establish the remote endpoint for the socket.
The name of the
remote device is "host.contoso.com".
Dim ipHostInfo As IPHostEntry = Dns.Resolve("host.contoso.com")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, port)
Create a TCP/IP socket.
Dim client As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
Connect to the remote endpoint.
client.BeginConnect(remoteEP, AddressOf ConnectCallback, client)
connectDone.WaitOne()
Send test data to the remote device.
Send(client, "This is a test<EOF>")
sendDone.WaitOne()
Receive the response from the remote device.
Receive(client)
receiveDone.WaitOne()
Write the response to the console.
Console.WriteLine("Response received : {0}", response)
Release the socket.
client.Shutdown(SocketShutdown.Both)
client.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub StartClient
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
Private Shared Sub Receive(client As Socket)
Try
Create the state object.
Dim state As New StateObject()
state.workSocket = client
Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
AddressOf ReceiveCallback, state)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub Receive
Private Shared Sub ReceiveCallback(ar As IAsyncResult)
Try
Retrieve the state object and client socket
from the async state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
Read data from the remote device.
Dim bytesRead As Integer = client.EndReceive(ar)
If bytesRead > 0 Then
There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
bytesRead))
Get the rest of the data.
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
AddressOf ReceiveCallback, state)
Else
All the data has arrived; put it in response.
If state.sb.Length > 1 Then
response = state.sb.ToString()
End If
Signal that all bytes have been received.
receiveDone.Set()
End If
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub ReceiveCallback
Private Shared Sub Send(client As Socket, data As [String])
Convert the string data to byte data using ASCII encoding.
Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, _
AddressOf SendCallback, client)
End Sub Send
Private Shared Sub SendCallback(ar As IAsyncResult)
Try
Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
Complete sending the data to the remote device.
Dim bytesSent As Integer = client.EndSend(ar)
Console.WriteLine("Sent {0} bytes to server.", bytesSent)
Signal that all bytes have been sent.
sendDone.Set()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub SendCallback
Entry point that delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As [String]) As Integer
StartClient()
Return 0
End Function Main
End Class AsynchronousClient