' 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.Text
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Class EndianUtil
    Public Shared Function Swap(ByVal v As Short) As Short
        Return (((v >> 8) And &H00FF) Or ((v << 8) And &HFF00))
    End Function

    Public Shared Function Swap(ByVal v As Integer) As Integer
        Return (((v >> 24) And &H000000FF) Or _
                ((v >> 8) And &H0000FF00) Or _
                ((v << 8) And &H00FF0000) Or _
                ((v << 24) And &HFF000000))
    End Function
End Class 

Class ServerText
    Public Shared Sub Main(ByVal args As String())
        ' lyt på port 12345
        Dim srv As TcpListener = New TcpListener(IPAddress.Any, 12345)
        srv.Start
        While True
            ' accepter connection fra client
            Dim cli As TcpClient = srv.AcceptTcpClient
            ' start tråd til at håndtere client
            Dim bch As BinaryClientHandler = New BinaryClientHandler(cli)
            Dim t As Thread = New Thread(AddressOf bch.Run)
            t.Start
        End While
    End Sub
End Class 

Class BinaryClientHandler
    Private Const ADD As Byte = 1
    Private Const DUP As Byte = 2
    Private Const RES As Byte = 3
    Private cli As TcpClient
    Private br As BinaryReader
    Private bw As BinaryWriter
    Public Sub New(ByVal cli As TcpClient)
        ' initialiser læse og skrive klasser
        Me.cli = cli
        br = New BinaryReader(cli.GetStream)
        bw = New BinaryWriter(cli.GetStream)
    End Sub
    Public Sub Run()
        Dim more As Boolean = True
        While more
            ' læs input fra client
            Dim cmd As Byte = br.ReadByte
            Select cmd
            Case ADD
                ' læg to heltal sammen
                Dim a As Integer = EndianUtil.Swap(br.ReadInt32)
                Dim b As Integer = EndianUtil.Swap(br.ReadInt32)
                Dim c As Integer = a + b
                bw.Write(RES)
                bw.Write(EndianUtil.Swap(c))
                bw.Flush
            Case DUP
                ' dupliker en streng
                Dim len As Integer = EndianUtil.Swap(br.ReadInt16)
                Dim data(len-1) As Byte
                Dim ix As Integer = 0
                While ix < len
                    ix += br.Read(data, ix, len - ix)
                End While
                Dim s As String = Encoding.Default.GetString(data)
                Dim s2 As String = s + s
                Dim data2 As Byte() = Encoding.Default.GetBytes(s2)
                bw.Write(RES)
                bw.Write(EndianUtil.Swap(CType(data2.Length, Short)))
                bw.Write(data2)
                bw.Flush
            Case Else
                Console.WriteLine("Unknown command: " + cmd)
                more = False
            End Select 
        End While
        ' luk læse og skrive klasser
        br.Close
        bw.Close
        cli.Close
    End Sub
End Class 
