' 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.Security.Cryptography.X509Certificates
Imports System.IO
Imports System.Net
Imports System.Net.Security
Imports System.Net.Sockets
Imports System.Security.Authentication
Imports System.Text

Namespace SocketDemo.Binary.SecureHigh.SingleServer
    Public Class Program
        Private Shared Function HTON(v As Integer) As Integer
            If BitConverter.IsLittleEndian Then
                Dim buf As Byte() = BitConverter.GetBytes(v)
                Array.Reverse(buf)
                Return BitConverter.ToInt32(buf, 0)
            Else
                Return v
            End If
        End Function
        Private Shared Function NTOH(v As Integer) As Integer
            If BitConverter.IsLittleEndian Then
                Dim buf As Byte() = BitConverter.GetBytes(v)
                Array.Reverse(buf)
                Return BitConverter.ToInt32(buf, 0)
            Else
                Return v
            End If
        End Function
        Private Const PORT As Integer = 12345
        Private Const BACKLOG As Integer = 100
        Public Shared Sub Main(args As String())
            ' listen on port
            Dim srv As New TcpListener(IPAddress.Any, PORT)
            srv.Start(BACKLOG)
            While True
                ' accept connection
                Dim cli As TcpClient = srv.AcceptTcpClient()
                ' get reader and writer
                Dim stm As NetworkStream = cli.GetStream()
                Dim sslstm As New SslStream(stm)
                Dim cert As X509Certificate = New X509Certificate2("\work\sslserver.pfx", "topsecret")
                sslstm.AuthenticateAsServer(cert, False, SslProtocols.Tls12, True)
                Dim br As New BinaryReader(sslstm)
                Dim bw As New BinaryWriter(sslstm)
                Console.WriteLine("(connection from {0})", cli.Client.RemoteEndPoint)
                ' read a and b
                Dim a As Integer = NTOH(br.ReadInt32())
                Dim b As Integer = NTOH(br.ReadInt32())
                ' calculate c and send
                Dim c As Integer = a + b
                bw.Write(HTON(c))
                bw.Flush()
                ' read v
                Dim buflen As Integer = br.ReadByte()
                Dim buf As Byte() = New Byte(buflen - 1) {}
                Dim ix As Integer = 0
                While ix < buf.Length
                    ix += br.Read(buf, ix, buf.Length - ix)
                End While
                Dim v As String = Encoding.UTF8.GetString(buf)
                ' calculate v2 and send
                Dim v2 As String = v & v
                Dim v2b As Byte() = Encoding.UTF8.GetBytes(v2)
                bw.Write(CByte(v2b.Length))
                bw.Write(v2b)
                bw.Flush()
                ' close connection
                br.Close()
                bw.Close()
                cli.Close()
            End While
        End Sub
    End Class
End Namespace
