-
Oct 18th, 2020, 08:25 AM
#1
Thread Starter
New Member
udp send and receive
hi all, i have a program that needs bi directional comunication with a device i bought,
i can send udp commands to it, thats working like a charm, but when i try to receive data from it i can only see the data that im sending, not the response,
my code:
Code:
Public udpClient As New UdpClient(11000)
Public udpClient2 As New UdpClient(52380)
Private Sub Hoofd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
udpClient.Connect("255.255.255.255", 52380)
udp_scan.Start()
udpClient2.Client.ReceiveTimeout = 1000
End Sub
Private Sub udp_scan_Tick(sender As Object, e As EventArgs) Handles udp_scan.Tick
info_label.Text = "ZOEKEN..."
Dim sendbytes = New Byte() {&H2, &H45, &H4E, &H51, &H3A, &H6E, &H65, &H74, &H77, &H6F, &H72, &H6B, &HFF, &H3}
udpClient.Send(sendbytes, sendbytes.Length)
doread()
End Sub
Function doread()
Try
Dim iepRemoteEndPoint As IPEndPoint = New _
IPEndPoint(IPAddress.Any, 52380)
Dim strMessage As String = String.Empty
Do
Dim bytRecieved As Byte() =
udpClient2.Receive(iepRemoteEndPoint)
strMessage = Encoding.ASCII.GetString(bytRecieved)
Console.WriteLine("This is the message you received: " _
+ strMessage)
Loop While (strMessage <> "exit")
udpClient2.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
Return 0
End Function
in wireshark i can see both the data i send and the response, in my program i only receive what im sending not the response.
i added the full project in attachments
Who can help me ?
Last edited by Shaggy Hiker; Oct 18th, 2020 at 09:09 AM.
Reason: Added CODE tags.
-
Oct 18th, 2020, 08:40 AM
#2
Re: udp send and receive
Please don't post unformatted code snippets. They are too hard to read.
vb.net Code:
Public udpClient As New UdpClient(11000)
Public udpClient2 As New UdpClient(52380)
Private Sub Hoofd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
udpClient.Connect("255.255.255.255", 52380)
udp_scan.Start()
udpClient2.Client.ReceiveTimeout = 1000
End Sub
Private Sub udp_scan_Tick(sender As Object, e As EventArgs) Handles udp_scan.Tick
info_label.Text = "ZOEKEN..."
Dim sendbytes = New Byte() {&H2, &H45, &H4E, &H51, &H3A, &H6E, &H65, &H74, &H77, &H6F, &H72, &H6B, &HFF, &H3}
udpClient.Send(sendbytes, sendbytes.Length)
doread()
End Sub
Function doread()
Try
Dim iepRemoteEndPoint As IPEndPoint = New _
IPEndPoint(IPAddress.Any, 52380)
Dim strMessage As String = String.Empty
Do
Dim bytRecieved As Byte() =
udpClient2.Receive(iepRemoteEndPoint)
strMessage = Encoding.ASCII.GetString(bytRecieved)
Console.WriteLine("This is the message you received: " _
+ strMessage)
Loop While (strMessage <> "exit")
udpClient2.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
Return 0
End Function
Also, the size of the attachment suggests that it likely includes binary files in the archive, which is against forum rules. You MUST delete all bin and obj folders from your projects before zipping them for attachment.
-
Oct 18th, 2020, 09:14 AM
#3
Re: udp send and receive
I removed the attachment, as it did contain compiled code. Please delete the compiled code and re-attach.
The issue may simply be that you are taking too long to read. UDP is VERY fast. I would suggest that you start listening before you send anything. Listen on a background thread, and when data is received, raise an event to the UI thread. I have a thread in the CodeBank that deals with broadcasting messages. You aren't doing that, but the receiver is on a different thread, so if you want an example of listening like that, it might be of some use.
My usual boring signature: Nothing
-
Oct 18th, 2020, 12:52 PM
#4
Re: udp send and receive
I agree with Shaggy. There is no way that code would be able to receive the data sent because in terms of the speed at which these things operate, that data was sent long before it was ready to receive it. When dealing with network code in general, you want to be listening long before anything is actually sent.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|