C#中的UDP代理?需要一点帮助吗
有人能看出我做错了什么吗?似乎只创建一个套接字实例,而不是两个C#中的UDP代理?需要一点帮助吗,c#,proxy,udp,pipe,C#,Proxy,Udp,Pipe,有人能看出我做错了什么吗?似乎只创建一个套接字实例,而不是两个 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; stati
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;
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Net.Sockets;
Net系统;
名称空间UdpProxy
{
///
///上提到的UdpState类的简单实现
/// http://msdn.microsoft.com/en-us/library/c8s04db1(v=VS.80).aspx
///
内部类UdpState
{
内部UdpState(UDP客户端c、IPEndPoint e)
{
这个.c=c;
这个。e=e;
}
内部无效设置远程(IPEndPoint远程)
{
this.remote=远程;
}
内部UDP客户端c;
内点e;
内部IPEndPoint远程;
}
}
修复如果有人想了解我是如何修复它的,这里有一个解决方案。。请注意,如果你无意中发现这一点,这可能是谷歌上唯一的UDP代理。。这是用C#编码的。。使用在线.NET转换器轻松移植到VB.NET
很高兴此代码可以正常工作;)
当然它没有效率,因为它不使用事件。。与ReceiveAsync/EndReceive类似
唯一的缺点是不使用AYSN记录事件。。就是你在下面看到的工作代码。。将不得不陷入无限循环中。。它会消耗你的CPU周期。。很容易用线固定。睡眠(10)。。(不要设置为高,否则将出现udp延迟)
睡眠通常是个坏主意。它只是说:“在10毫秒过去之前不要再次运行此代码”。但绝对不能保证它会在10毫秒后正常运行。另外,在大多数系统上,ThreadSleep精度是16ms的倍数:基本上,任何大于0的数字都意味着至少16ms。我如何使它更好?你能告诉我如何使用事件吗?这篇msdn文章很有用:除了使用BeginRead之外,可能还有一种等待数据的简单方法。。。例如,我认为select()适用于UDP。如果使用Thread.Sleep(),我建议在接收到数据时设置一个标志,并且只有在前一个循环中没有接收到数据时才调用Sleep。这样,如果您在任何给定时间有大量数据流,它将不会休眠,直到它捕获到内部Windows/winsock缓冲区中等待的所有数据。
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();
}
}
}