开发者

Basic UDP C# or VB.NET Pipe?

开发者 https://www.devze.com 2023-04-01 07:17 出处:网络
Looking for a basic UDP Pipe or redirector. Should of course be able to see both client 2 server and server 2 client data.

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 run开发者_如何学JAVAProxy()
    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 ;)

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace UdpProxy
{
    class Program
    {
        public static IPEndPoint m_listenEp = null;
        public static EndPoint m_connectedClientEp = null;
        public static IPEndPoint m_sendEp = null;
        public static Socket m_UdpListenSocket = null;
        public static Socket m_UdpSendSocket = null;


        static void Main(string[] args)
        {

            // Creates Listener UDP Server
            m_listenEp = new IPEndPoint(IPAddress.Any, 7900);
            m_UdpListenSocket = new Socket(m_listenEp.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
            m_UdpListenSocket.Bind(m_listenEp);

            //Connect to zone IP EndPoint
            m_sendEp = new System.Net.IPEndPoint(IPAddress.Parse("REMOTE_IP_GOES_HERE"), 7900);
            m_connectedClientEp = new System.Net.IPEndPoint(IPAddress.Any, 7900);

            byte[] data = new byte[1024];

            while (true)
            {
                if (m_UdpListenSocket.Available > 0)
                {

                    int size = m_UdpListenSocket.ReceiveFrom(data, ref m_connectedClientEp); //client to listener

                    if (m_UdpSendSocket == null)
                    {
                        // Connect to UDP Game Server.
                        m_UdpSendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    }

                    m_UdpSendSocket.SendTo(data, size, SocketFlags.None, m_sendEp); //listener to server.

                }

                if (m_UdpSendSocket != null && m_UdpSendSocket.Available > 0)
                {
                    int size = m_UdpSendSocket.Receive(data); //server to client.

                    m_UdpListenSocket.SendTo(data, size, SocketFlags.None, m_connectedClientEp); //listner

                }
            }


            // Wait for any key to terminate application
            Console.ReadKey();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消