Results 1 to 2 of 2

Thread: TCP Packet receiver problem with Socket library

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    1

    TCP Packet receiver problem with Socket library

    Hello,

    Here is my program
    Code:
    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.
    Name:  93622-2.jpg
Views: 179
Size:  29.9 KB
    Name:  93665-3.jpg
Views: 177
Size:  14.8 KB

    changed the Receive() to
    Code:
             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
    Name:  93683-4.jpg
Views: 145
Size:  27.1 KB

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: TCP Packet receiver problem with Socket library

    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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