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)...
Client Code (Sending End)...VB Code:
Private tcpListener As Net.Sockets.TcpListener Private intPort As Int32 = 32767 Private WithEvents tmrListen As New System.Timers.Timer(100) Private frmStatus As New frmMain Protected Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tcpListener = New Net.Sockets.TcpListener(Net.Dns.GetHostByName(System.Environment.MachineName).AddressList(0), intPort) frmStatus.lblListening.Text = "Listening on: " & intPort frmStatus.Show() tcpListener.Start() tmrListen.Start() End Sub Private Sub tmrListen_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmrListen.Elapsed Dim CurThreadStart As System.Threading.ThreadStart Dim CurThread As System.Threading.Thread Dim ThreadCount As Integer Dim i As Integer If Not tcpListener.Pending() Then Exit Sub End If tmrListen.Enabled = False CurThreadStart = New System.Threading.ThreadStart(AddressOf ProcessRequest) CurThread = New System.Threading.Thread(CurThreadStart) CurThread.Start() tmrListen.Enabled = True End Sub Private Sub ProcessRequest() Dim CurSocket As System.Net.Sockets.Socket Dim Buffer(100) As Byte Dim Bytes As Integer CurSocket = tcpListener.AcceptSocket Try If CurSocket.Available > 0 Then Bytes = CurSocket.Receive(Buffer) Select Case System.Text.Encoding.Default.GetString(Buffer) Case "Send Data" frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7)) Case "Unsubscribe" 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)))) End Select End If Catch ex As Exception Stop Finally If CurSocket.Connected Then CurSocket.Close() End If End Try 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.VB Code:
Private WithEvents tmrDelay As System.Timers.Timer 'Send Objects Private remEP As Net.IPEndPoint Private socClient As Net.Sockets.TcpClient Private NewThread As System.Threading.Thread Private Temp As String Private Buffer() As Byte Private Sub SendSubscript() socClient = New Net.Sockets.TcpClient socClient.Connect(remEP) Temp = "Send Data" Buffer = System.Text.Encoding.ASCII.GetBytes(Temp.ToCharArray) socClient.GetStream.Write(Buffer, 0, Buffer.Length) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Initialize Dim strIP As String = "192.168.187.187" 'DEFAULT Dim intPort As Int32 = 32767 'DEFAULT Dim intDelay As Int32 = 2000 'DEFAULT Dim Temp As String Dim Buffer() As Byte Try NewThread = System.Threading.Thread.CurrentThread tmrDelay = New System.Timers.Timer(intDelay) remEP = New Net.IPEndPoint(Net.IPAddress.Parse(strIP), intPort) Dim thrdStart As New System.Threading.Thread(AddressOf SendSubscript) thrdStart.Start() tmrDelay.Start() Catch ex As Exception Finally End Try End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing socClient = New Net.Sockets.TcpClient socClient.Connect(remEP) Temp = "Unsubscribe" Buffer = System.Text.Encoding.ASCII.GetBytes(Temp.ToCharArray) socClient.GetStream().Write(Buffer, 0, Buffer.Length) socClient.Close() tmrDelay.Stop() tmrDelay.Dispose() tmrDelay = Nothing socClient = Nothing remEP = Nothing GC.Collect() End Sub
I'm having a problem with this segment:
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.VB Code:
Select Case System.Text.Encoding.Default.GetString(Buffer) Case "Send Data" frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7)) Case "Unsubscribe" 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)))) End Select
I'm hoping someone else has had this issue too. I'm so close...![]()
Thanks.




Reply With Quote