' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Net
Imports System.Net.Sockets

Namespace UDP.Client
    Public Class Program
        Private Const SERVER As String = "localhost"
        Private Const SERVER_PORT As Integer = 12345
        Private Const CLIENT_PORT As Integer = 12346
        Private Const SIZE As Integer = 7
        Private Const MAX_SIZE As Integer = 1000
        Public Shared Sub Main(args As String())
            Using cli As New UdpClient(CLIENT_PORT)
                For i As Integer = 0 To 2
                    ' setup data
                    Dim buf1 As Byte() = New Byte(SIZE - 1) {}
                    For j As Integer = 0 To buf1.Length - 1
                        buf1(j) = CByte(i + j)
                    Next
                    ' send packet
                    cli.Send(buf1, buf1.Length, SERVER, SERVER_PORT)
                    ' receive packet
                    Dim from As New IPEndPoint(IPAddress.Any, 0)
                    Dim buf2 As Byte() = cli.Receive(from)
                    ' print data
                    For j As Integer = 0 To buf2.Length - 1
                        Console.Write(" " & buf2(j))
                    Next
                    Console.WriteLine()
                Next
            End Using
        End Sub
    End Class
End Namespace
