Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Public Class Form1
Dim Listener As New TcpListener(65535)
Dim Client As New TcpClient
Dim Message As String = ""
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
ListThread.Start()
End Sub
Private Sub Listening()
Listener.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While
rtbDisplay.AppendText(Message + vbNewLine)
End If
Catch ex As Exception
End Try
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Listener.Stop()
End Sub
Dim enterkey As Boolean
Private Sub rtbMessage_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtbMessage.KeyDown
If e.KeyCode = Keys.Enter And e.Control = False Then
If rtbMessage.TextLength > 0 Then
Client = New TcpClient("190.168.1.64", 65535)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(rtbMessage.Text)
Writer.Flush()
End If
rtbMessage.Clear()
'This variable sets the EnterKey to True indicating that the EnterKey pressed
enterkey = True
End If
' Ctrl + Enter Button
If e.KeyCode = Keys.Control And e.KeyCode = Keys.Enter Then
End If
End Sub
Private Sub rtbMessage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rtbMessage.KeyPress
'If EnterKey is Pressed than do the following action
If enterKey = True Then
rtbMessage.SelectAll() 'Select all data in TextBox
rtbMessage.Clear() 'Clear the TextBox
rtbMessage.Focus() 'Get Focus on TextBox
End If
'This variable sets the EnterKey to False indicating that the EnterKey was not pressed
enterKey = False
End Sub
End Class