Public Class TCPGateway
Public ProcessRunning As Boolean = True
Dim serverSocket As TcpListener
Dim serverSocketActive As Boolean = True
Dim WithEvents clientSocket As TcpClient
Public clientCounter As Integer = 0
Shared _EngineStatus As String = "Free"
Shared _CurrentOwner As String = ""
Public Shared Property EngineStatus() As String
Get
EngineStatus = _EngineStatus
End Get
Set(ByVal value As String)
_EngineStatus = value
End Set
End Property
Public Shared Property CurrentOwner() As String
Get
CurrentOwner = _CurrentOwner
End Get
Set(ByVal value As String)
_CurrentOwner = value
End Set
End Property
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
ProcessRunning = False
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Timer1.Start()
StartTCP_IP()
End Sub
#Region "TCP-IP"
Sub StartTCP_IP()
Dim myServerIP As IPAddress = IPAddress.Parse("xxx.xxx.xxx.xxx")
Dim myServerPort As Integer = yyyy
serverSocket = New TcpListener(myServerIP, myServerPort)
Try
serverSocket.Start()
'// Start Client Connection Listener
Dim tcpListener As New Threading.Thread(AddressOf Listener)
tcpListener.Start()
RichTextBox1.Text = RichTextBox1.Text & "Gateway running: " & myServerIP.ToString & " port: " & myServerPort & vbCrLf
Catch ex As Exception
RichTextBox1.Text = RichTextBox1.Text & ex.ToString & vbCrLf
serverSocketActive = False
End Try
End Sub
Delegate Sub WriteTextDelegate(ByVal Text As String)
Public WriteTextInstance As New WriteTextDelegate(AddressOf WriteText)
Sub WriteText(ByVal Text As String)
Writer(Text)
End Sub
Sub Writer(ByVal text As String)
Me.RichTextBox1.Text = Me.RichTextBox1.Text & text & vbCrLf
End Sub
Sub Listener()
Do While True
Threading.Thread.Sleep(10)
Try
clientCounter += 1
clientSocket = serverSocket.AcceptTcpClient()
clientSocket.LingerState.Enabled = False
clientSocket.LingerState.LingerTime = 0
clientSocket.ReceiveTimeout = 5000
clientSocket.SendTimeout = 5000
Dim client As New handleTCPClient
client.startClient(clientSocket, Convert.ToString(clientCounter))
Catch ex As Exception
RichTextBox1.Text = RichTextBox1.Text & ex.ToString & vbCrLf
End Try
If Not ProcessRunning Then
Exit Sub
End If
Loop
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = handleTCPClient.State
End Sub
#End Region
End Class