Results 1 to 10 of 10

Thread: [RESOLVED] [VB.Net 2003 EA]: Socket Encoding

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Resolved [RESOLVED] [VB.Net 2003 EA]: Socket Encoding

    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.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [VB.Net 2003 EA]: Socket Encoding

    just a thought, but does

    Select Case System.Text.Encoding.Default.GetString(Buffer).Trim

    change anything?

  3. #3

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [VB.Net 2003 EA]: Socket Encoding

    The output in the debugger was:

    "Send Data"

    Still didn't match any of the select cases

    Thanks though.

  4. #4

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [VB.Net 2003 EA]: Socket Encoding

    *Bump*

    Still haveing the issue. I'm going to try a few more things, but maybe today's batch of VBFers might know what's wrong.

  5. #5

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [VB.Net 2003 EA]: Socket Encoding

    Ok, this is going to blow your minds....

    As you can clearly see, it evaluates correctly, but it completely skips the matching case statement.

    (Note: With or without Convert.ToString, it evaluates True)

    This is leading me to beleive it has nothing to do with encoding anymore... and now it's an asyncronous problem? Wouldn't syncLock keep other threads from running for this process until it's done?
    Last edited by sevenhalo; Mar 2nd, 2006 at 09:51 AM.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: [VB.Net 2003 EA]: Socket Encoding

    The problem is that you have a lot of extra characters at the end of your string since the buffer is 101 bytes long.

    So you'll have to mask your string doing something like this
    VB Code:
    1. Dim strY As String = System.Text.Encoding.UTF8.GetString(Buffer)
    2.         Select Case True
    3.             Case Microsoft.VisualBasic.Left(strY, 9) = "Send Data"
    4.                 Debug.Print("Found Send Data")
    5.             Case Microsoft.VisualBasic.Left(strY, 12) = "Unsubscribe"
    6.                 Debug.Print("Unsubscribe")
    7.         End Select
    On a seperate note, you could simplify things by using the TcpClient object and woking with streams.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [VB.Net 2003 EA]: Socket Encoding

    you know what, this sounds weird, but try this anyway
    VB Code:
    1. Dim S as String =  System.Text.Encoding.Default.GetString(Buffer)
    2. select case S

    can't hurt since you are pulling your hair out anyway

  8. #8

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [VB.Net 2003 EA]: Socket Encoding

    That worked, thanks a ton
    VB Code:
    1. SyncLock curThread
    2.                     Bytes = CurSocket.Receive(Buffer)
    3.                     Dim strHold As String = System.Text.Encoding.Default.GetString(Buffer).Trim
    4.                     Select Case True
    5.                         Case strHold.Substring(0, strHold.IndexOf(Chr(0))) = "Send Data"
    6.                             frmStatus.lbSubscribed.Items.Add(CurSocket.RemoteEndPoint.Serialize.Item(4) & "." & CurSocket.RemoteEndPoint.Serialize.Item(5) & "." & CurSocket.RemoteEndPoint.Serialize.Item(6) & "." & CurSocket.RemoteEndPoint.Serialize.Item(7))
    7.                         Case strHold.Substring(0, strHold.IndexOf(Chr(0))) = "Unsubscribe"
    8.                             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))))
    9.                     End Select
    10.                 End SyncLock
    It seems like it would do the exact same thing as "Trim," I can't figure out why this worked and it doesn't.

    I thought I was already using TCPClient and streams? Where could I simplify this?

  9. #9

    Thread Starter
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [VB.Net 2003 EA]: Socket Encoding

    @kle - I gave that a shot earlier too. I'm still up in the air why the last code I posted worked, but Trim didn't... That just... I don't know?

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [VB.Net 2003 EA]: Socket Encoding

    well all trim does is eliminate spaces from the front and back of the string (unless you use its overloaded version, and pass a param array of what you want trimmed). If the extra character(s) in the string are null characters and not spaces, it would make sense that trim doesn't do anything.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width