Can someone spot what I did wrong here? Seems to only create one instance of the sockets not two.
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);
}
}
}
supporting helper class
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;
}
}
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 😉
Sure it’s not efficient because it doesn’t use events.. like ReceiveAsync/EndReceive.
Only downfall to not using Aysnchronize events.. is that you see below the working code.. will have to be stuck in a infinite loop.. and it will burn your CPU cycles.. easily fixed with a Thread.Sleep(10).. (don’t set to high or you will have udp lag)