I have a program I'm setting up that accepts incoming socket requests from a client. Basically, this is the first half. When the client starts up the service (I have it in a windows app right now for testing and debugging), it will send a "subscribe" message to my server and my server will start notifying it of alarms and such. This code is for the subscribing...

Server Code (Listener end)...
VB Code:
  1. Private tcpListener As Net.Sockets.TcpListener
  2.     Private intPort As Int32 = 32767
  3.     Private WithEvents tmrListen As New System.Timers.Timer(100)
  4.  
  5.     Private frmStatus As New frmMain
  6.  
  7.     Protected Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.         tcpListener = New Net.Sockets.TcpListener(Net.Dns.GetHostByName(System.Environment.MachineName).AddressList(0), intPort)
  9.         frmStatus.lblListening.Text = "Listening on: " & intPort
  10.  
  11.         frmStatus.Show()
  12.  
  13.         tcpListener.Start()
  14.         tmrListen.Start()
  15.     End Sub
  16.  
  17.     Private Sub tmrListen_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmrListen.Elapsed
  18.         Dim CurThreadStart As System.Threading.ThreadStart
  19.         Dim CurThread As System.Threading.Thread
  20.         Dim ThreadCount As Integer
  21.         Dim i As Integer
  22.  
  23.         If Not tcpListener.Pending() Then
  24.             Exit Sub
  25.         End If
  26.  
  27.         tmrListen.Enabled = False
  28.  
  29.         CurThreadStart = New System.Threading.ThreadStart(AddressOf ProcessRequest)
  30.         CurThread = New System.Threading.Thread(CurThreadStart)
  31.  
  32.         CurThread.Start()
  33.  
  34.         tmrListen.Enabled = True
  35.     End Sub
  36.  
  37.     Private Sub ProcessRequest()
  38.         Dim CurSocket As System.Net.Sockets.Socket
  39.         Dim Buffer(100) As Byte
  40.         Dim Bytes As Integer
  41.  
  42.         CurSocket = tcpListener.AcceptSocket
  43.         Try
  44.             If CurSocket.Available > 0 Then
  45.                 Bytes = CurSocket.Receive(Buffer)
  46.                 Select Case System.Text.Encoding.Default.GetString(Buffer)
  47.                     Case "Send Data"
  48.                         frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7))
  49.                     Case "Unsubscribe"
  50.                         frmStatus.lbSubscribed.Items.RemoveAt(frmStatus.lbSubscribed.Items.IndexOf(frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7))))
  51.                 End Select
  52.             End If
  53.         Catch ex As Exception
  54.             Stop
  55.         Finally
  56.             If CurSocket.Connected Then
  57.                 CurSocket.Close()
  58.             End If
  59.         End Try
  60.     End Sub
Client Code (Sending End)...
VB Code:
  1. Private WithEvents tmrDelay As System.Timers.Timer
  2.  
  3.     'Send Objects
  4.     Private remEP As Net.IPEndPoint
  5.     Private socClient As Net.Sockets.TcpClient
  6.     Private NewThread As System.Threading.Thread
  7.     Private Temp As String
  8.     Private Buffer() As Byte
  9.  
  10.     Private Sub SendSubscript()
  11.         socClient = New Net.Sockets.TcpClient
  12.         socClient.Connect(remEP)
  13.  
  14.         Temp = "Send Data"
  15.         Buffer = System.Text.Encoding.ASCII.GetBytes(Temp.ToCharArray)
  16.  
  17.         socClient.GetStream.Write(Buffer, 0, Buffer.Length)
  18.     End Sub
  19.  
  20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  21.         'Initialize
  22.         Dim strIP As String = "192.168.187.187"      'DEFAULT
  23.         Dim intPort As Int32 = 32767                'DEFAULT
  24.         Dim intDelay As Int32 = 2000                'DEFAULT  
  25.         Dim Temp As String
  26.         Dim Buffer() As Byte
  27.  
  28.         Try
  29.             NewThread = System.Threading.Thread.CurrentThread
  30.  
  31.             tmrDelay = New System.Timers.Timer(intDelay)
  32.             remEP = New Net.IPEndPoint(Net.IPAddress.Parse(strIP), intPort)
  33.  
  34.             Dim thrdStart As New System.Threading.Thread(AddressOf SendSubscript)
  35.             thrdStart.Start()
  36.  
  37.             tmrDelay.Start()
  38.         Catch ex As Exception
  39.  
  40.         Finally
  41.  
  42.         End Try
  43.     End Sub
  44.  
  45.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  46.         socClient = New Net.Sockets.TcpClient
  47.         socClient.Connect(remEP)
  48.  
  49.         Temp = "Unsubscribe"
  50.         Buffer = System.Text.Encoding.ASCII.GetBytes(Temp.ToCharArray)
  51.         socClient.GetStream().Write(Buffer, 0, Buffer.Length)
  52.         socClient.Close()
  53.  
  54.         tmrDelay.Stop()
  55.         tmrDelay.Dispose()
  56.  
  57.         tmrDelay = Nothing
  58.         socClient = Nothing
  59.         remEP = Nothing
  60.  
  61.         GC.Collect()
  62.     End Sub
The tmrDelay is used for finding out if there's pending information waiting. It's not done, so I didn't include it's elapsed event.

I'm having a problem with this segment:
VB Code:
  1. Select Case System.Text.Encoding.Default.GetString(Buffer)
  2.                     Case "Send Data"
  3.                         frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7))
  4.                     Case "Unsubscribe"
  5.                         frmStatus.lbSubscribed.Items.RemoveAt(frmStatus.lbSubscribed.Items.IndexOf(frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7))))
  6.                 End Select
The message will be "Send Data" and the select case can't find a match. When I do a "?System.Text.Encoding.Default.GetString(Buffer)" in the debug window, it looks pefect. That leads me to beleive it has something to do with encoding, but I can't quite figure it out.

I'm hoping someone else has had this issue too. I'm so close...

Thanks.