Private Sub GetMessage()
' Must listen on correct port- must be same as port client wants to connect on.
Const portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
Dim intLen As Integer
Dim intThere As Integer
Dim intThere2 As Integer
Dim strType As String
Dim strHeader As String
Dim strMessage As String
tcpListener.Start()
Try
Me.Hide()
'Accept the pending client connection and return
'a TcpClient initialized for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
' Get the stream
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
'Now we have to parse out the type of message, Header and message
intLen = Len(clientdata)
'Get the message type.
intThere = InStr(clientdata, "~")
strType = Mid(clientdata, 1, 1)
'Set the form colors
If strType = "2" Then 'Critical
TextBox1.ForeColor = Color.White
TextBox1.BackColor = Color.DarkRed
PictureBox1.Visible = True
PictureBox2.Visible = False
Else 'Informational
TextBox1.ForeColor = Color.Black
TextBox1.BackColor = Color.Gold
PictureBox2.Visible = True
PictureBox1.Visible = False
End If
'Get the Window Size
strType = Mid(clientdata, 3, 1)
'Get Header text
intThere2 = InStrRev(clientdata, "~", intLen)
strHeader = Mid(clientdata, 5, intThere2 - (4 + 1))
'Get Message
strMessage = Mid(clientdata, intThere2 + 1, intLen - intThere2)
TextBox1.Text = strHeader
txtData.Text = strMessage
'Any communication with the remote client using the TcpClient can go here.
'Close TcpListener and TcpClient.
tcpClient.Close()
tcpListener.Stop()
Me.Show()
'Set the global variable that will be used in the timing.
intTick = 0
'Start the timer that will close the form after 5 minutes.
Timer2.Enabled = True
strType = Mid(clientdata, 3, 1)
If strType = "1" Then
Me.WindowState = FormWindowState.Normal
Else
Me.WindowState = FormWindowState.Minimized
End If
Timer1.Enabled = True
Catch ex As Exception
Dim strerror As String
strerror = ex.ToString
MessageBox.Show(strerror, "Error getting message. Please call the Helpdesk", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub