PowerTCP Sockets for .NET
Start Method (Udp)
Example 




System.Threading.WaitCallback delegate specifying the function to execute.
Information to pass to the worker delegate function; can be null.
Starts a worker thread on which the specified delegate method executes.
Syntax
'Declaration
 
Public Sub Start( _
   ByVal worker As WaitCallback, _
   ByVal state As Object _
) 
'Usage
 
Dim instance As Udp
Dim worker As WaitCallback
Dim state As Object
 
instance.Start(worker, state)
public void Start( 
   WaitCallback worker,
   object state
)
public: void Start( 
   WaitCallback* worker,
   Object* state
) 
public:
void Start( 
   WaitCallback^ worker,
   Object^ state
) 

Parameters

worker
System.Threading.WaitCallback delegate specifying the function to execute.
state
Information to pass to the worker delegate function; can be null.
Remarks

This method provides an easy means for executing blocking functions on a worker thread. Applications with a UI thread should use this technique to execute processes (like Receive) that can block the UI thread.

The state parameter can be null for cases in which no state information needs to be passed to the worker delegate function.

Unhandled exceptions occurring on the worker thread will be caught and reported by the Error event.

Example
The following example demonstrates sending and receiving between two Udp components.
private void buttonStartServer_Click(object sender, EventArgs e)
{
    //Start worker thread to bind to the socket and start echoing data.
    udpServer.Start(udpServer_Open, null);
}

private void buttonStopServer_Click(object sender, EventArgs e)
{ 
    udpServer.Close();
}

private void buttonDoEcho_Click(object sender, EventArgs e)
{
    //Start a Udp component to act as client
    udpClient.Start(udpClient_DoEcho, null);
}

private void udpServer_Open(object state)
{
    byte[] buffer = new byte[1024];
    udpServer.Open(7);
    Datagram datagram = udpServer.Receive(buffer);
    //Datagram is null when local socket has been closed
    while (datagram != null)
    {
        if (datagram.Count > 0)
            udpServer.Send(buffer, datagram.Origin);
        datagram = udpServer.Receive(buffer);
    }
}

private void udpClient_DoEcho(object state)
{
    //Send message, receive response and close
    byte[] buffer = new byte[1024];
    udpClient.Open(0);
    udpClient.Send("hello world!", "127.0.0.1", 7);
    Datagram datagram = udpClient.Receive(buffer);
    //marshal data to UI thread
    udpClient.Marshal(datagram.ToString(), null);
    udpClient.Close();
}

private void udpClient_UserState(object sender, UserStateEventArgs e)
{
    textBox1.AppendText(e.Message);
}
Private Sub buttonStartServer_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Start worker thread to bind to the socket and start echoing data.
    udpServer.Start(AddressOf udpServer_Open, Nothing)
End Sub

Private Sub buttonStopServer_Click(ByVal sender As Object, ByVal e As EventArgs)
    udpServer.Close()
End Sub

Private Sub buttonDoEcho_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonDoEcho.Click
    'Start a Udp component to act as client
    udpClient.Start(AddressOf udpClient_DoEcho, Nothing)
End Sub

Private Sub udpServer_Open(ByVal state As Object)
    Dim buffer(1023) As Byte
    udpServer.Open(7)
    Dim datagram As Datagram = udpServer.Receive(buffer)
    'Datagram is null when local socket has been closed
    Do While datagram IsNot Nothing
        If datagram.Count > 0 Then
            udpServer.Send(buffer, datagram.Origin)
        End If
        datagram = udpServer.Receive(buffer)
    Loop
End Sub

Private Sub udpClient_DoEcho(ByVal state As Object)
    'Send message, receive response and close
    Dim buffer(1023) As Byte
    udpClient.Open(0)
    udpClient.Send("hello world!", "127.0.0.1", 7)
    Dim datagram As Datagram = udpClient.Receive(buffer)
    'marshal data to UI thread
    udpClient.Marshal(datagram.ToString(), Nothing)
    udpClient.Close()
End Sub

Private Sub udpClient_UserState(ByVal sender As Object, ByVal e As UserStateEventArgs)
    textBox1.AppendText(e.Message)
End Sub
See Also

Reference

Udp Class
Udp Members


PowerTCP Sockets for .NET Documentation Version 4.5
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic