Looking for a basic UDP Pipe or redirector. Should of course be able to see both
client 2 server and server 2 client data.
Here is what I tried but it fails because I don’t know when to call which Receive..
Say I call
data = serverUdpClient.Receive(sender)
then I have to reroute this, so I call this
clientUdpClient.Send(data, data.Length)
Now the line after comes which is proper in TCP Pipe.
data = clientUdpClient.Receive(sender)..
But I have to.. call this again..
data = serverUdpClient.Receive(sender)
clientUdpClient.Send(data, data.Length)
before I can use
data = clientUdpClient.Receive(sender)..
Pretty much the code flow is all fucked.. because it’s socket is blocking. When I started working on UDP.. all examples say stay away from non-blocking as it’s too advanced for newbie’s trying to work with networking sockets.. I find that statement wrong.. the other way around!.
Public serverUdpClient As System.Net.Sockets.UdpClient
Public clientUdpClient As System.Net.Sockets.UdpClient
Sub runProxy()
If serverUdpClient IsNot Nothing Then
serverUdpClient.Close()
serverUdpClient = Nothing
End If
If clientUdpClient IsNot Nothing Then
clientUdpClient.Close()
clientUdpClient = Nothing
End If
Try
'Listen for incoming udp connection request.
serverUdpClient = New UdpClient(New IPEndPoint(IPAddress.Any, Int32.Parse(Int(txtListeningPort.Text))))
WriteLog("Server started at: " + txtListeningPort.Text)
Dim data As Byte() = New Byte(1023) {}
Dim sender As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
While True
data = serverUdpClient.Receive(sender)
'Connect to server.
If clientUdpClient Is Nothing Then
clientUdpClient = New UdpClient(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text)))
clientUdpClient.Connect(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text)))
End If
clientUdpClient.Send(data, data.Length)
data = clientUdpClient.Receive(sender)
serverUdpClient.Send(data, data.Length)
End While
Catch ex As Exception
WriteLog("Errors at runProxy @ " + ex.Message)
End Try
End Sub
Also tried this.. doesn’t work properly.
While True
If serverUdpClient.Available > 0 Then
data = serverUdpClient.Receive(sender)
'Connect to server.
If clientUdpClient Is Nothing Then
clientUdpClient = New UdpClient(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text)))
clientUdpClient.Connect(txtIP.Text, Int32.Parse(Int(txtListeningPort.Text)))
End If
clientUdpClient.Send(data, data.Length)
End If
If clientUdpClient.Available > 0 Then
data = clientUdpClient.Receive(sender)
serverUdpClient.Send(data, data.Length)
End If
End While
Fixed all I had to do was convert my sender IPEndPoint to the connected instance..
Here is one without Async.. that I made and it works.. only UDP Proxy on all of google/SO coded in C#.
Fixed it here is the solution if anyone wants to learn how I fixed it.. Please note this is probably the only UDP Proxy on all of google if you stumbled upon this.. that is coded in C#.. easily ported to VB.NET with online .NET converter
Be happy this code works 😉