Writing a Network Chat Application

Server and Client GroupBox

Create a form with 2 GroupBox controls near the top. Set the Text properties of both GroupBox controls to "Server" and "Client".

Add Controls to Server Groupbox

In the Server Groupbox, add a single control, a Button, and set its Name property "StartServerButton". Also, set its Text propert to "Start Server".

Add Controls to Client Groupbox

In the Client GroupBox, add a Label control and set its Text to "Server IP Address". add a TextBox control and set its Name property to "ServerIPAddressTextBox". Finally, add a Button control, and set its Name property to "ConnectButton" and its Text property to "Connect."

Add the Chat History Groupbox, controls, and Scrollbars

Add another groupbox control, and set its Text property to "Chat History". Inside this groupbox, add a textbox. Set the Name property of the text box to "ChatHistoryTextBox". Then, find its MultiLine property and set it to True. Resize the textbox to give it a nice rectangular shape. Make the Textbox so that it almost fills the group box.

Finally, find and set the ScrollBars property of the ChatHistoryTextBox to "Both". This will allow it to have both vertical and horizontal scroll bars.

Add Chat Message Groupbox

Add another group box, and set its Text to "Chat Message"

Inside this last group box, add a TextBox and set its Name property to "SendMessageTextBox". Then add a Button and set its Name property to "SendMessageButton". Finally, set the Text property on the button to "Send Message".

Add Imports Statements and some Variables

Double-Click on the form to go into code view. Modify the code so it looks like this

Imports System.Net.Sockets
Imports System.IO
Imports System.Net

Public Class Form1

    Dim mySocket As Socket
    Dim reader As StreamReader
    Dim writer As StreamWriter
    Dim myNetworkStream As NetworkStream

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

Add A timer

Next we need to add a timer to start checking for input from the other side, that is, on the reader. In the toolbox, find the Timer, click on it, and then click on the form. This will add a timer to your program, and your will see a little timer icon at the bottom of the IDE screen.

Add TcpListener

Now we want to do the part where you set up the TCPListener. This will occur in response to the user clicking the StartServer Button in the Server groupbox. Double-click on the StartServerButton to get a click handler for it:

 Private Sub StartServerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartServerButton.Click
    'Make the network connections on the server side
     Dim myTcpListener As New TcpListener(IPAddress.Any, 50000)
     myTcpListener.Start()
     mySocket = myTcpListener.AcceptSocket()
     myNetworkStream = New NetworkStream(mySocket)
     reader = New StreamReader(myNetworkStream)
     writer = New StreamWriter(myNetworkStream)
     writer.AutoFlush = True

     'Start the timer to get input from the remote side
     Timer1.Interval = 100
     Timer1.Start()
     MessageBox.Show("We are connected and ready to go!")
  End Sub

Add TcpClient

Next we want to do the part where you connect to the TCPListener. This will occur in response to the user filling in the IP address and clicking the connect button. Double-click on the ConnectButton to get a click handler for it:

    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
        'Get IPAddress and connect to port 50000   
        Dim s As TextBox = ServerIPAddressTextBox
        Dim ipAddr As IPAddress = IPAddress.Parse(s.Text)
        Dim myTcpClient As TcpClient = New TcpClient
        myTcpClient.Connect(ipAddr, 50000)

        myNetworkStream = myTcpClient.GetStream()
        reader = New StreamReader(myNetworkStream)
        writer = New StreamWriter(myNetworkStream)
        writer.AutoFlush = True

        'Start the timer to get input from the remote side
        Timer1.Interval = 100
        Timer1.Start()
        MessageBox.Show("We are connected and ready to go!")
    End Sub

Add Timer Handler

Double-click on the timer icon at the bottom of the IDE screen to get a timer handler. The timer will be called timer1. We need to write the handler so that the the application can get messages from the remote user.

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Not myNetworkStream.DataAvailable Then
            Return
        End If
        'read the reader and update the chat history textbox
        Dim input As String = reader.ReadLine()
        ChatHistoryTextBox.AppendText("Remote:")
        ChatHistoryTextBox.AppendText(input + Environment.NewLine)
    End Sub

Add SendMessage Button Handler

Finally, we need to add a handler for the SendMessageButton so the local user can send messages. Double-click on the SendMessageButton, and modify the program to add

    Private Sub SendMessageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendMessageButton.Click
        'Get text from sendmessagetexbox, echo it to chathistorytextbox, and send to remote user
        Dim input As String = SendMessageTextBox.Text
        ChatHistoryTextBox.AppendText(input + Environment.NewLine)
        writer.WriteLine(input)
    End Sub

Find a partner and test it!