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