Click here to Skip to main content
13,287,315 members (50,414 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I have a slight problem with my code where my entire program halts while sending/receiving TCP/IP messages. I also noticed that it affects another application that is connected to the same IP address but on a different port. Am I handling something wrong here? I'm assuming the fact that right after I send the message to the socket I'm asking for the result. Do I use a receive event or something like that?

Any suggestions would be greatly appreciated.

Thanks in advance!

Here is my Code:
'   MatchPort b/g RSSI Query over Telnet protocol
'   Developed by Marco van der Merwe
'   23/08/2012
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class MatchPortRSSI
    Private TelnetSocket As Socket
    Private MatchPortSSID As String
    Private WithEvents tmrRSSI As New Timer
    Public Event RSSI(ByVal dBmRx As Integer)
    Private Sub TelnetRead()
        Dim nRxBytes As Integer = 0
        Dim RxBytes(100) As [Byte]
        If TelnetSocket.Connected Then
            nRxBytes = TelnetSocket.Receive(RxBytes, RxBytes.Length, 0)
            Dim TelnetString As String = String.Empty
            Dim i As Integer
            If nRxBytes < 100 Then
                For i = 0 To nRxBytes ' Filter out problematic characters
                    Select Case RxBytes(i)
                        Case 0
                        Case 1
                        Case 3
                        Case 10
                            TelnetString = TelnetString & vbNewLine
                        Case 13
                            TelnetString = TelnetString & vbNewLine
                        Case 251
                        Case 255
                        Case Else
                            TelnetString = TelnetString & Chr(RxBytes(i))
                    End Select
            End If
            Dim intdBmIndex As Integer = TelnetString.IndexOf("dBm")
            If intdBmIndex > 3 Then
                Dim strRSSIval As String = TelnetString.Substring(intdBmIndex - 4, 3)
                RaiseEvent RSSI(CInt(strRSSIval))
            End If
        End If
    End Sub
    Public Sub StartRSSI(ByVal strIPAddress As String, ByVal strSSID As String)
        Dim MatchPortIPAddress As IPAddress = IPAddress.Parse(strIPAddress.Trim)
        Dim TelnetServer As IPEndPoint = New IPEndPoint(MatchPortIPAddress, 9999)
        TelnetSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        MatchPortSSID = strSSID
        Catch oEX As SocketException
            ' Error Handler
            ' Need to place code to deal with errors.
            Exit Sub
        End Try
        If TelnetSocket.Connected Then
            Dim strTx As String = "M" ' Places telnet into Monitor Mode
            TelnetSocket.Send(Encoding.ASCII.GetBytes(strTx), strTx.Length, SocketFlags.None)
            tmrRSSI.Enabled = True
        End If
    End Sub
    Public Sub StopRSSI()
        tmrRSSI.Enabled = False ' Stop timed RSSI Query
        If TelnetSocket.Connected Then ' If connected close Telnet Monitor Mode
            Dim strTx As String = "QU" & vbLf
            TelnetSocket.Send(Encoding.ASCII.GetBytes(strTx), strTx.Length, SocketFlags.None)
            TelnetSocket.Disconnect(False) ' Close TCP Port
        End If
    End Sub
    Public Property Interval() As Integer
            Interval = tmrRSSI.Interval
        End Get
        Set(ByVal Interval As Integer)
            tmrRSSI.Interval = Interval
        End Set
    End Property
    Private Sub tmrRSSI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRSSI.Tick
        If TelnetSocket.Connected Then
            Dim strTx As String = "SA " & MatchPortSSID & vbLf
            TelnetSocket.Send(Encoding.ASCII.GetBytes(strTx), strTx.Length, SocketFlags.None)
        End If
    End Sub
End Class
Posted 2-Oct-12 6:11am
Updated 2-Oct-12 6:14am
Use the debugger; also catch all exceptions on top stack frame of the stack of each thread and see what happened; you can also log the exception information. If the problem is still not clear, log some more; you can use System.Diagnostics.EventLog. If the problem is not solved, add the information on what you discovered to your question using "Improve question".
bigbro_1985 3-Oct-12 12:44pm
Hi SA,

Thanks for the links the minimalistic Telnet client really helped me a lot. I still have to implement it for my application.

Thanks again!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question — this is the main part of what you need to do. Sorry, I don't want to spend time on investigation before you do you part of work, but I can only share what's wrong in this code, what I can see from the first glance.

The biggest problem I can see is your try-catch block, with the catch for SocketException. You are doing something which should be done very rarely, as an exclusion, and only if you know exactly what's going on in some particular catch block: you block the propagation of the exception. Don't do it; this is a misuse of exceptions. Exceptions should not be caught in every place then can appear; the main rule is: let go. The mechanism is designed to isolate exception handling from "regular" code. I told you what to do in my comment to the question above. In your case, just remove try-catch completely. What you have now simply makes the exception silent, so that's why you don't know what exactly happened.

Now, "filter out problematic characters" is at least weird. Please review it.

More generally, your problem is using "magical constants", worse, immediate constants. Why, on Earth, 3, 251 or 255, "M", "QU", "SA"?! Look at your "100". It it repeated twice. If you decide to change it, you will easily forget another place and screw up things heavily. Better, as a minimum, declare all constants explicitly, with the keyword const, this way you won't repeat them and will be able to parametrize the code with them when you find it necessary.

Also, these CodeProject articles could be useful for you, please see:
Quick tool : A minimalistic Telnet library[^],
Windows Mobile Telnet Client[^].

Good luck,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100