' 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
Imports System.Net.Sockets
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Namespace DefaultNamespace
    Public Class MainForm
    Inherits Form
        Private msgs As TextBox
        Private sendmsg As TextBox
        Private send As Button
        Private client As TcpClient
        Private rdr As StreamReader
        Private wrt As StreamWriter
        Public Sub New()
            InitializeComponent
            client = New TcpClient("localhost", 1234)
            rdr = New StreamReader(client.GetStream)
            wrt = New StreamWriter(client.GetStream)
            call (New Thread(AddressOf Reader)).Start
        End Sub
        <STAThread()> _
        Public Shared Sub Main(ByVal args As String())
            Application.Run(New MainForm)
        End Sub
        Private Sub InitializeComponent()
            sendmsg = New TextBox
            msgs = New TextBox
            send = New Button
            SuspendLayout
            msgs.Location = New Point(50, 50)
            msgs.Multiline = True
            msgs.Size = New Size(500, 300)
            sendmsg.Location = New Point(50, 400)
            sendmsg.Size = New Size(500, 25)
            send.Location = New Point(50, 450)
            send.Size = New Size(500, 25)
            send.Text = "Send"
            AddHandler send.Click, AddressOf SendClick
            ClientSize = New Size(600, 525)
            Controls.Add(sendmsg)
            Controls.Add(msgs)
            Controls.Add(send)
            Text = "Chat (TCP/IP client)"
            ResumeLayout(False)
        End Sub
        Sub SendClick(ByVal sender As Object, ByVal e As System.EventArgs)
            wrt.WriteLine(sendmsg.Text)
            wrt.Flush
            SyncLock msgs
                msgs.Text += ("mig: " + sendmsg.Text + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
            End SyncLock
        End Sub
        Private Sub Reader()
            Dim line As String
            line = rdr.ReadLine
            While Not (line Is Nothing)
                SyncLock msgs
                    msgs.Text += ("dig: " + line + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "")
                End SyncLock
                line = rdr.ReadLine
            End While
        End Sub
    End Class 
End Namespace
