' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Net.Sockets
Imports System.Threading

Public Class ChatClient
    Public Shared Sub Main(args As String())
        Dim client As New TcpClient("localhost", 50000)
        Dim wrt As New StreamWriter(client.GetStream())
        Dim t As Thread = New Thread(AddressOf (New Reader(client)).Run)
        t.Start()
        Dim line As String
        line = Console.ReadLine()
        While line IsNot Nothing
            wrt.WriteLine("SEND " & line)
            wrt.Flush()
            line = Console.ReadLine()
        End While
        wrt.WriteLine("EXIT")
        wrt.Flush()
        wrt.Close()
        client.Close()
    End Sub
End Class

Public Class Reader
    Private rdr As StreamReader
    Public Sub New(cli As TcpClient)
        rdr = New StreamReader(cli.GetStream())
    End Sub
    Public Sub Run()
        Try
            Dim line As String
            line = rdr.ReadLine()
            While line IsNot Nothing
                Console.WriteLine(line)
                line = rdr.ReadLine()
            End While
        Catch generatedExceptionName As Exception
            ' nothing
        End Try
    End Sub
End Class
