I have an application that listens in the background to incoming string, but I need to add in the abillity to send 2 strings upon a button click. The code I have uses a streamwriter to try and send the string, but I'm getting a "System.NullReferenceException: Object reference not set to an instance of an object" on the line
Code:
opensock2 = New IO.StreamWriter(client.GetStream)
.

Any help or a pointer in the right direction would be greatly appreciated.

Cheers


Code:
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Private client As System.Net.Sockets.TcpClient

    Dim ipaddress(3) As Byte

    Dim swon As String = "A55A6B0550000000FFFBDE0030C8" 'switch on
    Dim swoff As String = "A55A6B0570000000FFFBDE0030E8" 'switch off
    Dim but0 As String = "A55A6B0500000000FFFBDE002066" 'button release
    Dim msg As String = "0000000000000000000000000000"
    Dim t As Integer = "00"
    Dim returndata As String = msg

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Listener.RunWorkerAsync()
        Button2.Enabled = False
    End Sub


    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Listener.DoWork
        ipaddress(0) = 10
        ipaddress(1) = 0
        ipaddress(2) = 0
        ipaddress(3) = 14

        Dim opensock As New Net.Sockets.TcpListener(New Net.IPAddress(ipaddress), 4000)

        Try
            opensock.Start()
        Catch ex As System.Net.Sockets.SocketException
            ' mess.Text = "cannot start connection"
        End Try
        Dim tcpClient As TcpClient = opensock.AcceptTcpClient()

        While True
            If Listener.CancellationPending Then
                e.Cancel = True
                opensock.Stop()

                Exit While
            End If
            Listener.ReportProgress(1)
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                Listener.ReportProgress(2)
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                Try

                    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                Catch ex As Exception
                    Listener.ReportProgress(3)

                End Try
                returndata = Encoding.ASCII.GetString(bytes)
                Listener.ReportProgress(4)

            Else
                'returndata = msg
            End If
        End While
    End Sub


    Private Sub Listener_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Listener.RunWorkerCompleted

        t = 40 - (40 * CInt("&H" & (returndata.Substring(12, 2))) / 255)
        temp.Text = t
        comm.Text = returndata
        If e.Cancelled Then
            mess.AppendText("cancelled")
        End If

    End Sub

    Private Sub Listener_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Listener.ProgressChanged
        Select Case e.ProgressPercentage
            Case 1
                mess.AppendText("connected")
                'comm.Text = msg
            Case 2
                mess.AppendText("Can read and write")
                mess.AppendText("data received")
            Case 3
                mess.AppendText("error")
            Case 4
                t = 40 - (40 * CInt("&H" & (returndata.Substring(12, 2))) / 255)
                temp.Text = t
                comm.Text = returndata
                mess.AppendText("Last temp")
        End Select

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim opensock2 As IO.StreamWriter
        Try

            opensock2 = New IO.StreamWriter(client.GetStream)
            opensock2.WriteLine(swon)
            opensock2.WriteLine(but0)
            opensock2.Flush()

        Catch ex As Exception
            mess.AppendText(ex.ToString)
        End Try
    End Sub
End Class