有人能发现我在这里做错了吗?似乎只创建一个插槽实例而不是两个。
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;
}
}
答案 0 :(得分:1)
修正了这里的解决方案,如果有人想了解我是如何修复它的..请注意这可能是所有谷歌上唯一的UDP代理,如果你偶然发现这个...用C#编码..很容易移植到VB.NET与在线.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();
}
}
}