I initialize the UDP connection in the main form...
Expand|Select|Wrap|Line Numbers
- Dim CLIENT_SOCKET As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
- Dim int_PORT_NUM As Integer
- Dim IP_ADDRESS As System.Net.IPAddress
- Dim remoteEP As System.Net.IPEndPoint
- Dim UDP_CONNECTION As AsynchronousClient
- 'initialize socket:
- 'Establish local endpoint for the socket:
- ipHostInfo = Dns.Resolve(Dns.GetHostName)
- ipAddress = ipHostInfo.AddressList(0)
- localEndPoint = New IPEndPoint(ipAddress, 0)
- CLIENT_SOCKET.SetSocketOption(Sockets.SocketOptionLevel.Socket, Sockets.SocketOptionName.ReceiveBuffer, 65536)
- bind the socket to the local endpoint:
- CLIENT_SOCKET.Bind(localEndPoint)
- 'Get Remote Host:
- str_IP_ADDRESS = "135.202.8.146"
- IP_ADDRESS = IP_ADDRESS.Parse(str_IP_ADDRESS)
- 'get port number:
- int_PORT_NUM = 0
- 'assign remote endpoint:
- remoteEP = New System.Net.IPEndPoint(IP_ADDRESS, int_PORT_NUM)
- 'create new ethernet connection:
- UDP_CONNECTION = New AsynchronousClient(remoteEP)
- 'connect socket to RemoteEP:
- UDP_CONNECTION.Connect()
- 'begin receive:
- UDP_CONNECTION.Receive()
Expand|Select|Wrap|Line Numbers
- Imports System
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.Threading
- Imports System.Text
- Public Class AsynchronousClient
- 'CLASS VARIABLES:
- '===============
- 'OBJECTS:
- Private REMOTE_ENDPOINT As IPEndPoint
- Private CLIENT_SOCKET As Socket
- Public Sub New(ByVal A_REMOTE_ENDPOINT As IPEndPoint)
- Dim ipHostInfo As IPHostEntry
- Dim ipAddress As IPAddress
- Dim localEndPoint As IPEndPoint
- 'Create new UDP socket:
- CLIENT_SOCKET = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
- 'Establish local endpoint for the socket:
- ipHostInfo = Dns.Resolve(Dns.GetHostName)
- ipAddress = ipHostInfo.AddressList(0)
- localEndPoint = New IPEndPoint(ipAddress, 0)
- 'bind the socket to the local endpoint:
- CLIENT_SOCKET.Bind(localEndPoint)
- 'Assign remote server information:
- REMOTE_ENDPOINT = A_REMOTE_ENDPOINT
- End Sub
- #Region "Receive"
- Public Sub Receive()
- Try
- ' Create the BRFStateObject:
- Dim BRFStateObject As New BRFStateObject
- 'assign client socket:
- BRFStateObject.PASS_CLIENT_SOCKET = Me.CLIENT_SOCKET
- Dim tempRemote_EP As EndPoint
- tempRemote_EP = CType(REMOTE_ENDPOINT, EndPoint)
- ' Begin asynchronously receiving the data from the remote device.
- Me.CLIENT_SOCKET.BeginReceiveFrom( BRFStateObject.byte_BUFFER, 0, _
- BRFStateObject.int_BUFFER_SIZE, 0, tempRemote_EP, _
- AddressOf BeginReceiveFromCallback, BRFStateObject)
- Catch e As Exception
- Console.WriteLine(e.ToString())
- End Try
- End Sub 'Receive
- #End Region
- #Region "BeginReceiveFromCallback"
- Private Sub BeginReceiveFromCallback(ByVal ar As IAsyncResult)
- 'VARIABLES:
- '==========
- Dim int_NUM_BYTES As Integer
- 'SUB:
- '====
- Try
- ' Close BeginReceiveFrom and get bytes
- int_NUM_BYTES = CLIENT_SOCKET.EndReceiveFrom(ar, REMOTE_ENDPOINT)
- 'convert ar to state object:
- byte_BUFFER = ar.AsyncState
- If int_NUM_BYTES > 0 Then
- byte_BUFFER = ar.AsyncState
- System.Console.WriteLine(byte_BUFFER(0))
- Exit Sub
- End If
- Catch e As Exception
- Console.WriteLine(e.ToString())
- End Try
- End Sub 'BeginReceiveFromCallback
- #End Region
- Public Sub Connect()
- Try
- Me.CLIENT_SOCKET.BeginConnect(REMOTE_ENDPOINT, AddressOf BeginConnectCallback, Me.CLIENT_SOCKET)
- Catch e As Exception
- Console.WriteLine(e.ToString())
- End Try
- End Sub
- Private Shared Sub BeginConnectCallback(ByVal 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())
- Catch e As Exception
- Console.WriteLine(e.ToString())
- End Try
- End Sub 'BeginConnectCallback
- Public Sub Send(ByVal str_A_STRING As String)
- Dim byteData As Byte()
- ' Convert the string data to byte data using ASCII encoding.
- byteData = Encoding.ASCII.GetBytes(str_A_STRING)
- ' Begin sending the data to the remote device.
- Me.CLIENT_SOCKET.BeginSendTo(byteData, 0, byteData.Length, 0, Me.REMOTE_ENDPOINT, _
- AddressOf SendCallback, Me.CLIENT_SOCKET)
- End Sub 'Send
- Private Shared Sub SendCallback(ByVal 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
- Public Class BRFStateObject
- ' Client socket:
- Public PASS_CLIENT_SOCKET As Socket = Nothing
- 'Buffer Size:
- Public int_BUFFER_SIZE As Integer = 4096
- ' Buffer:
- Public byte_BUFFER(4096 - 1) As Byte
- 'Recieved data string:
- Public sb As New StringBuilder
- End Class 'BRFStateObject