question

JohnJin-7583 avatar image
0 Votes"
JohnJin-7583 asked XingyuZhao-MSFT commented

TCP Packet receiver problem with Socket library

Hello,

Here is my program

 Imports System.Windows.Forms
 Imports System.Text.RegularExpressions
 Imports System.Net.Sockets
 Imports System.Text
 Imports System.Threading
 Imports System.IO
 Imports System.Net
    
    
    
    
 Public Class Form1
    
     Private TCP As Socket
     Private IPConfig As IPEndPoint
    
     Dim TCpThread As Thread
    
     Dim msg As String
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
         Dim ipAddr As System.Net.IPAddress
         ipAddr = IPAddress.Parse("192.168.1.15")
    
         Try
    
             IPConfig = New IPEndPoint(ipAddr, 4000)
             TCP = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
             TCP.Connect(IPConfig)
    
             If TCP.Connected = True Then
                 TCpThread = New Thread(AddressOf Receive)
                 TCpThread.Start()
             Else
                 Throw New ArgumentException("Failed to connect")
             End If
    
         Catch ex As Exception
             Throw New ArgumentException(ex.Message)
         End Try
     End Sub
    
    
    
     Private Sub Receive()
    
         Dim h(1000) As Byte
         Dim i As Byte = 0
    
         While (1)
             Try
    
                 If TCP.Available > 0 Then
                     TCP.Receive(h, TCP.Available, 0)
                     msg = System.Text.Encoding.ASCII.GetString(h)
                     i = 1
                 ElseIf (i = 1) Then
                     i = 0
                     Application.OpenForms(0).Invoke(New EventHandler(AddressOf prin))
                 End If
    
             Catch ex As Exception
             End Try
         End While
     End Sub
    
     Private Sub prin()
         ListBox1.Items.Add(msg)
         msg = ""
     End Sub
    
 End Class

my transmitter send each packet each 10mS. My code works fine for some packet, but after a while it collects many packet to each other and causes an error in operation as you could see in the picture.
93622-2.jpg

93665-3.jpg

what is my problem?

dotnet-visual-basic
2.jpg (130.0 KiB)
3.jpg (32.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
1 Vote"
XingyuZhao-MSFT answered XingyuZhao-MSFT commented

Hi @JohnJin-7583 ,
Check the following code to see if it works:

         Dim len As Integer = TCP.Receive(h, 0, 73, SocketFlags.None)
         msg = Encoding.ASCII.GetString(h, 0, len)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you XingyuZhao-MSFT for your help. it works nice.
I tried to select your post as the answer but it doesn't show me accept answer for your comments.

0 Votes 0 ·

Hi @JohnJin-7583 ,
I have converted the comment to answer, and you can accept it.

0 Votes 0 ·
JohnJin-7583 avatar image
0 Votes"
JohnJin-7583 answered XingyuZhao-MSFT commented

I changed the Receive() to

     Private Sub Receive()
    
         Dim h(1000) As Byte
         Dim i As Byte = 0
    
         While (1)
             Try
    
                 If TCP.Available > 0 Then
                     If TCP.Available > 74 Then
                         TCP.Receive(h, 74, 0)
                     Else
                         TCP.Receive(h, TCP.Available, 0)
                     End If
    
                     msg = System.Text.Encoding.ASCII.GetString(h)
                     i = 1
                 ElseIf (i = 1) Then
                     i = 0
                     Application.OpenForms(0).Invoke(New EventHandler(AddressOf prin))
                 End If
    
             Catch ex As Exception
             End Try
         End While
     End Sub

but after some seconds it print (2*74) bytes in msg variables. I get confused

93683-4.jpg



4.jpg (120.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JohnJin-7583 ,
I note that you set code accept 74 bytes,

     If TCP.Available > 74 Then
          TCP.Receive(h, 74, 0)

What's the content sent by the server?

Take a look at the following way to receive data:

     Dim byteCounter As Integer = TCP.Receive(h)
     msg = Encoding.ASCII.GetString(h, 0, byteCounter)
1 Vote 1 ·
JohnJin-7583 avatar image
0 Votes"
JohnJin-7583 answered XingyuZhao-MSFT converted comment to answer

Dear XingyuZhao-MSFT,

Thank you very much for you comment,

The transmitter send each 10ms a 73 bytes packet to the computer. I was wondering why my code works for some packets and after that it doesn't split the packet into 74 bytes based on my if condition ( If TCP.Available > 74 Then).

your mention code works fine

          Dim byteCounter As Integer = TCP.Receive(h)
          msg = Encoding.ASCII.GetString(h, 0, byteCounter)

but in some seconds it connect two 74 packet to each other (it think it is because of the hardware delay).


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.