You are declaring both your TcpClient and your NetworkStream as local variables in the Form_Load subroutine, they will go out of scope once that subroutine finishes and will be marked for garbage collection. You need to declare them at class level, and remember that it is not a good practise to give the variable the same name as the class. Here's how I'd do it:
VB.Net Code:
Public Class Form1 Private client As System.Net.Sockets.TcpClient Private clientStream As NetworkStream Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load client = New System.Net.Sockets.TcpClient("192.168.55.4", 8000) clientStream = client.GetStream() End Sub Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If NetworkStream.CanWrite And NetworkStream.CanRead Then If TextBox1.Text <> "" Then Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text) NetworkStream.Write(sendBytes, 0, sendBytes.Length) End If End If End Sub End Class




Reply With Quote