gpt4 book ai didi

c# - C# 中的 UDP 代理?需要一点帮助

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:48 25 4
gpt4 key购买 nike

有人可以发现我在这里做错了什么吗?似乎只创建了一个套接字实例,而不是两个。

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

namespace UdpProxy
{
class Program
{
public static UdpClient server = null;

static void Main(string[] args)
{
int localPort = 7900;
IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 4001);

IPAddress tempAddress;

IPAddress.TryParse("OUT_GOING_IP/HOST_GOES_HERE", out tempAddress);
remoteSender.Address = tempAddress;
remoteSender.Port = 7900;

// Display some information
Console.WriteLine("Welcome! Starting Upd proxy server.");
Console.WriteLine("Local port: " + localPort);
Console.WriteLine("Remote ip: " + remoteSender.Address.ToString());
Console.WriteLine("Remote port: " + remoteSender.Port);
Console.WriteLine("Press any key to quit.");

// Create UDP client
UdpClient client = new UdpClient(localPort);
UdpState state = new UdpState(client, remoteSender);
state.setRemote(remoteSender);
// Start async receiving
client.BeginReceive(new AsyncCallback(DataReceivedClient), state);



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

private static void DataReceivedClient(IAsyncResult ar)
{
UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
IPEndPoint ipEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e; //local ip and random port.
IPEndPoint remoteIPEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).remote;
byte[] receiveBytes = c.EndReceive(ar, ref ipEndPoint);

// Convert data to ASCII and print in console
string receivedText = BitConverter.ToString(receiveBytes);
Console.WriteLine("Client 2 Server = " + receivedText);


if (server == null)
{
// Create UDP client
server = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
UdpState stateServer = new UdpState(server, remoteIPEndPoint);
server.BeginReceive(new AsyncCallback(DataReceiveServer), stateServer);
server.Connect(remoteIPEndPoint);

}

server.Send(receiveBytes, receiveBytes.Length);

// Restart listening for udp data packages
c.BeginReceive(new AsyncCallback(DataReceivedClient), ar.AsyncState);
}


private static void DataReceiveServer(IAsyncResult ar)
{
UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
IPEndPoint ipEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e; //local ip and random port.
byte[] receiveBytes = c.EndReceive(ar, ref ipEndPoint);

// Convert data to ASCII and print in console
string receivedText = BitConverter.ToString(receiveBytes);
Console.WriteLine("Server 2 Client = " + receivedText);


c.Connect(ipEndPoint);
c.Send(receiveBytes, receiveBytes.Length);

// Restart listening for udp data packages
c.BeginReceive(new AsyncCallback(DataReceiveServer), ar.AsyncState);
}
}
}

辅助类

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

namespace UdpProxy
{
/// <summary>
/// Simple implementation of the UdpState class mentioned on
/// http://msdn.microsoft.com/en-us/library/c8s04db1(v=VS.80).aspx
/// </summary>
internal class UdpState
{
internal UdpState(UdpClient c, IPEndPoint e)
{
this.c = c;
this.e = e;
}

internal void setRemote(IPEndPoint remote)
{
this.remote = remote;
}

internal UdpClient c;
internal IPEndPoint e;
internal IPEndPoint remote;
}
}

最佳答案

如果有人想了解我是如何修复它的,这里是解决方案。请注意,如果您偶然发现这个,这可能是所有谷歌上唯一的 UDP 代理。它是用 C# 编码的。很容易移植到带在线 .NET 转换器的 VB.NET

很高兴这段代码有效;)

当然它效率不高,因为它不使用事件……例如 ReceiveAsync/EndReceive。

不使用 Aysnchronize 事件的唯一缺点是你会在下面看到工作代码......将不得不陷入无限循环......并且它会消耗你的 CPU 周期......可以通过 Thread.Sleep( 10)..(不要设置得太高否则你会有udp延迟)

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();
}
}
}

关于c# - C# 中的 UDP 代理?需要一点帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7279162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com