Ethernet TcpClient class problme

  • Thread starter Thread starter Parisa_1
  • Start date Start date
P

Parisa_1

Guest
Hello,

I wrote a simple receiver software.( Receive about 80Bytes every 5ms) The transmitter send a 80 bytes to receiver asynchronous it means (it send 80bytes in 5ms( for example 38bytes at 1ms, 20bytes at 2ms, 22bytes at 2ms). So I need to sum up all incoming data and check its length and start/stop characters (A, B). Here is my class code:


Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO




Public Class Parisa


Public Structure DataSeperated
Dim Zero As ULong
Dim AccuratePosition As ULong
Dim Data1 As String
Dim Data2 As Single
End Structure


Public ReceivedDataSeperated As New DataSeperated

Private IP As String
Private Port As Integer
Private TCP As TcpClient
Private State As String
Private Message As String
Private MSG As String
Private Det As Boolean
Dim RecData As String

Dim TCpThread As Thread

Public Sub Connect()
Try
TCP = New TcpClient(Me.IP, Me.Port)
If TCP.Connected = True Then
State = "Connected"
TCpThread = New Thread(AddressOf Receive)
TCpThread.Start()
Else
Throw New ArgumentException("Failed to connect")
Me.State = "Close"
Me.Det = False
End If

Catch ex As Exception
State = "IDLE"
Throw New ArgumentException(ex.Message)
End Try

End Sub


Private Sub Receive()
Dim inp As NetworkStream
Dim income As BinaryReader
inp = TCP.GetStream
income = New BinaryReader(inp)

While (State = "Connected")
Try
MSG = income.ReadChar
Application.OpenForms(0).Invoke(New EventHandler(AddressOf SumUP))
Catch ex As Exception
Try
If TCpThread.IsAlive = True Then TCpThread.Abort()
TCP.Close()
MSG = ex.Message
Catch ex1 As Exception
Application.OpenForms(0).Invoke(New EventHandler(AddressOf SumUP))
End Try
State = "IDLE"
End Try
End While
End Sub

Private Sub SumUP()
Message &= msg
Check(Message)
End Sub

Private Sub Check(ByVal SearchData As String)
Dim pattern As String = ""
Dim regex1 As Regex
Dim matches As MatchCollection

pattern = "A[0-9]{70}B"
regex1 = New Regex(pattern)
matches = regex1.Matches(SearchData)
If matches.Count > 0 Then
Message = matches(0).ToString()
Message = Replace(Message, "A", "")
Message = Replace(Message, "B", "")
RecData = Message
Message = ""
DataCalculation()
End If


End Sub
Private Sub DataCalculation()
Dim p As Byte = 0
Dim msg(100) As String
Dim s As String = RecData
Dim msp As String = ""
Dim sum As Single = 0


If s <> "" Then

msg(10) = Mid(s, 53, 2)
msg(11) = Mid(s, 55, 2)
msg(12) = Mid(s, 57, 2)

Select Case Trim(Val(msg(11)))
Case 0 : msp = " Error 1"
Case 1 : msp = " Error 2"
Case 2 : msp = " Error 3"
End Select


ReceivedDataSeperated.Data1 = msp
ReceivedDataSeperated.Data2 = msp
ReceivedDataSeperated.Zero = msg(10)
ReceivedDataSeperated.AccuratePosition = msg(12)




End If


End Sub

Public Sub Send(ByVal Data As String)
Try
If State <> "Connected" Then Exit Sub
Dim out As New IO.StreamWriter(TCP.GetStream)
out.Write(Data)
out.Flush()
Catch ex As Exception
If TCpThread IsNot Nothing Then
If TCpThread.IsAlive = True Then TCpThread.Abort()
End If
TCP.Close()
' Throw New ArgumentException("Connection not found or TCP object did not initialize.")
State = "IDLE"
End Try
End Sub


Private Sub SendDefault()
Try
Send("Hello System1 I am Pari")

Catch ex As Exception

End Try

End Sub


Public Sub New()
Me.IP = ""
Me.Port = 0
End Sub

Public Property RemoteIP() As String
Get
Return Me.IP
End Get
Set(ByVal value As String)
Dim reg As New Regex("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")
If reg.IsMatch(value) = False Then
Throw New ArgumentException("Please enter a valid IP for SetConnection method")
Exit Property
End If
Me.IP = value
End Set
End Property

Public Property RemotePort() As Integer
Get
Return Me.Port
End Get
Set(ByVal value As Integer)
If value <= 0 Or value > 65535 Then
Throw New ArgumentException("Please enter a valid port number for SetConnection method")
Exit Property
End If
Me.Port = value
End Set
End Property



End Class




I put a timer (1ms) on my form to show the value of ReceivedDataSeperated variable data (such as Data1, Data2 ,...).For low speed (for example 500ms each 80bytes) it works fine. But for 10ms (80bytes) it can't follow the incoming packages. I think the receiver function is too slow. I checked the incoming bytes with SecureCRT software and it works fine.


what is my mistake?

Continue reading...
 
Back
Top