Share via

TCP Packet receiver problem with Socket library

John Jin 21 Reputation points
2021-05-04T17:24:47.617+00:00

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?

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Xingyu Zhao-MSFT 5,381 Reputation points
2021-05-07T05:48:04.96+00:00

Hi @John Jin ,
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)  

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. John Jin 21 Reputation points
    2021-05-06T06:37:31.073+00:00

    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).

    Was this answer helpful?

    0 comments No comments

  2. John Jin 21 Reputation points
    2021-05-04T17:52:22.71+00:00

    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

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.