Note that there are some explanatory texts on larger screens.

plurals
  1. POUDP Proxy in C#? Needs a little assistance
    primarykey
    data
    text
    <p>Can someone spot what I did wrong here? Seems to only create one instance of the sockets not two.</p> <pre><code>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); } } } </code></pre> <p>supporting helper class</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; namespace UdpProxy { /// &lt;summary&gt; /// Simple implementation of the UdpState class mentioned on /// http://msdn.microsoft.com/en-us/library/c8s04db1(v=VS.80).aspx /// &lt;/summary&gt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload