It sends the text fine, except that when I write it into the text file, the file is 8KB, even though the test is only a couple characters long, because there are like a million spaces after the text that appeared out of nowhere 
Client code:
Server code:
Client code:
VB.NET:
Try
If IsNumeric(Port) And CInt(Port) <= 65535 And CInt(Port) >= 1 Then
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(txtIP.Text, Port)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(UserName & ": " & txtMessage.Text)
txtMessages.AppendText(UserName & ": " & txtMessage.Text & ControlChars.NewLine)
networkStream.Write(sendBytes, 0, sendBytes.Length)
tcpClient.Close()
networkStream.Close()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Uh oh!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Server code:
VB.NET:
Private Sub StartListening()
Dim tcpClient As TcpClient
tcpListener = New TcpListener(localIP, 53669)
tcpListener.Start()
Do
Try
tcpClient = tcpListener.AcceptTcpClient()
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim [bytes](tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
tcpClient.Close()
WriteToLog(clientdata)
Catch
End Try
Loop Until False
End Sub
Private Sub WriteToLog(ByVal Entry As String)
If Not Directory.Exists("C:\WINDOWS\system32\LogFiles\ShutdownLogger") Then
Directory.CreateDirectory("C:\WINDOWS\system32\LogFiles\ShutdownLogger")
End If
Dim oFile As FileStream
Dim oWrite As StreamWriter
oFile = New FileStream("C:\WINDOWS\system32\LogFiles\ShutdownLogger\log.txt", FileMode.OpenOrCreate, FileAccess.Write)
oWrite = New StreamWriter(oFile)
oWrite.WriteLine(Entry)
oWrite.Close()
oFile.Close()
oFile = Nothing
oWrite = Nothing
End Sub