I am trying to accomplish UDP hole punching. I am basing my theory on this article and this WIKI page, but I am facing some issues with the C# coding of it. Here is my problem:
Using the code that was posted here I am now able to connect to a remote machine and listen on the same port for incoming connections (Bind 2 UDP clients to the same port).
For some reason the two bindings to the same port block each other from receiving any data.
I have a UDP server that responds to my connection so if I connect to it first before binding any other client to the port I get its responses back.
If I bind another client to the port no data will be received on either clients.
Following are 2 code pieces that show my problem. The first connects to a remote server to create the rule on the NAT device and then a listener is started on a different thread to capture the incoming packets. The code then sends packets to the local IP so that the listener will get it. The second only sends packets to the local IP to make sure this works. I know this is not the actual hole punching as I am sending the packets to myself without living the NAT device at all. I am facing a problem at this point, and I don't imagine this will be any different if I use a computer out side the NAT device to connect.
[EDIT] 2/4/2012
I tried using another computer on my network and WireShark (packet sniffer) to test the listener. I see the packets incoming from the other computer but are not received by the listener UDP client (udpServer) or the sender UDP client (client).
[EDIT] 2/5/2010
I have now added a function call to close the first UDP client after the initial sending and receiving of packets only living the second UDP client to listen on the port. This works and I can receive packets from inside the network on that port. I will now try to send and receive packets from outside the network. I will post my findings as soon as I find something.
Using this code I get data on the listening client:
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
ThreadPool.QueueUserWorkItem(delegate
{
UdpClient udpServer = new UdpClient();
udpServer.ExclusiveAddressUse = false;
udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer.Client.Bind(localpt);
IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0);
Console.WriteLine("Listening on " + localpt + ".");
byte[] buffer = udpServer.Receive(ref inEndPoint); //this line will block forever
Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + ".");
});
Thread.Sleep(1000);
UdpClient udpServer2 = new UdpClient(6000);
// the following lines work and the data is received
udpServer2.Connect(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
udpServer2.Send(new byte[] { 0x41 }, 1);
Console.Read();
}
If I use the following code, after the connection and data transfer between my client and server, the listening UDP client will not receive anything:
static void Main(string[] args)
{
IPEndPoint localpt = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
//if the following lines up until serverConnect(); are removed all packets are received correctly
client = new UdpClient();
client.ExclusiveAddressUse = false;
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.Client.Bind(localpt);
remoteServerConnect(); //connection to remote server is done here
//response is received correctly and printed to the console
ThreadPool.QueueUserWorkItem(delegate
{
UdpClient udpServer = new UdpClient();
udpServer.ExclusiveAddressUse = false;
udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer.Client.Bind(localpt);
IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0);
Console.WriteLine("Listening on " + localpt + ".");
byte[] buffer = udpServer.Receive(ref inEndPoint); //this line will block forever
Console.WriteLine("Receive from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + ".");
});
Thread.Sleep(1000);
UdpClient udpServer2 = new UdpClient(6000);
// I expected the following line to work and to receive this as well
udpServer2.Connect(Dns.Resolve(Dns.GetHostName()).AddressList[0], 4545);
udpServer2.Send(new byte[] { 0x41 }, 1);
Console.Read();
}
If i understand correctly, you are trying to communicate peer-to-peer between 2 clients each behind a different NAT, using a mediation server for hole punching?
Few years ago i did the exact same thing in c#, i haven't found the code yet, but ill give you some pointers if you like:
First, I wouldn't use the Connect() function on the udpclient, since UDP is a connectionless protocol, all this function really does is hide the functionality of a UDP socket.
You should perfrom the following steps:
Open a UDP socket on a server with it's ports not blocked by a firewall, at a specific port (eg Bind this socket to a chosen port for example 23000)
Create a UDP socket on the first client, and send something to the server at 23000. Do not bind this socket. When a udp is used to send a packet, windows will automatically assign a free port to the socket
Do the same from the other client
The server has now received 2 packets from 2 clients at 2 different adresses with 2 different ports. Test if the server can send packets back on the same address and port. (If this doesn't work you did something wrong or your NAT isn't working. You know its working if you can play games without opening ports :D)
The server should now send the address and port of the other clients to each connected client.
A client should now be able to send packets using UDP to the adresses received from the server.
You should note that the port used on the nat is probably not the same port as on your client pc!! The server should distribute this external port to clients. You must use the external adresses and the external ports to send to!
Also note that your NAT might not support this kind of port forwarding. Some NAT's forward all incoming traffic on a assigned port to you client, which is what you want. But some nats do filtering on the incoming packets adresses so it might block the other clients packets. This is unlikely though when using a standard personal user router.
Edit: After a lot more testing this doesn't seem to work at all for me unless I enable UPnP. So a lot of the things I wrote here you may find useful but many people don't have UPnP enabled (because it is a security risk) so it will not work for them.
Here is some code using PubNub as a relay server :). I don't recommend using this code without testing because it is not perfect (I'm not sure if it is even secure or the right way to do things? idk I'm not a networking expert) but it should give you an idea of what to do. It at least has worked for me so far in a hobby project. The things it is missing are:
Testing if the client is on your LAN. I just send to both which works for your LAN and a device on another network but that is very inefficient.
Testing when the client stops listening if, for example, they closed the program. Because this is UDP it is stateless so it doesn't matter if we are sending messages into the void but we probably shouldn't do that if noone is getting them
I use Open.NAT to do port forwarding programatically but this might not work on some devices. Specifically, it uses UPnP which is a little insecure and requires UDP port 1900 to be port forwarded manually. Once they do this it is supported on most routers but many have not done this yet.
So first of all, you need a way to get your external and local IPs. Here is code for getting your local IP:
// From http://stackoverflow.com/questions/6803073/get-local-ip-address
public string GetLocalIp()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("Failed to get local IP");
}
And here is some code for getting your external IP via trying a few websites that are designed to return your external IP
public string GetExternalIp()
{
for (int i = 0; i < 2; i++)
{
string res = GetExternalIpWithTimeout(400);
if (res != "")
{
return res;
}
}
throw new Exception("Failed to get external IP");
}
private static string GetExternalIpWithTimeout(int timeoutMillis)
{
string[] sites = new string[] {
"http://ipinfo.io/ip",
"http://icanhazip.com/",
"http://ipof.in/txt",
"http://ifconfig.me/ip",
"http://ipecho.net/plain"
};
foreach (string site in sites)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(site);
request.Timeout = timeoutMillis;
using (var webResponse = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = webResponse.GetResponseStream())
{
using (StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
return responseReader.ReadToEnd().Trim();
}
}
}
}
catch
{
continue;
}
}
return "";
}
Now we need to find an open port and forward it to an external port. As mentioned above I used Open.NAT. First, you put together a list of ports that you think would be reasonable for your application to use after looking at registered UDP ports. Here are a few for example:
public static int[] ports = new int[]
{
5283,
5284,
5285,
5286,
5287,
5288,
5289,
5290,
5291,
5292,
5293,
5294,
5295,
5296,
5297
};
Now we can loop through them and hopefully find one that is not in use to use port forwarding on:
public UdpClient GetUDPClientFromPorts(out Socket portHolder, out string localIp, out int localPort, out string externalIp, out int externalPort)
{
localIp = GetLocalIp();
externalIp = GetExternalIp();
var discoverer = new Open.Nat.NatDiscoverer();
var device = discoverer.DiscoverDeviceAsync().Result;
IPAddress localAddr = IPAddress.Parse(localIp);
int workingPort = -1;
for (int i = 0; i < ports.Length; i++)
{
try
{
// You can alternatively test tcp with nc -vz externalip 5293 in linux and
// udp with nc -vz -u externalip 5293 in linux
Socket tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
tempServer.Bind(new IPEndPoint(localAddr, ports[i]));
tempServer.Close();
workingPort = ports[i];
break;
}
catch
{
// Binding failed, port is in use, try next one
}
}
if (workingPort == -1)
{
throw new Exception("Failed to connect to a port");
}
int localPort = workingPort;
// You could try a different external port if the below code doesn't work
externalPort = workingPort;
// Mapping ports
device.CreatePortMapAsync(new Open.Nat.Mapping(Open.Nat.Protocol.Udp, localPort, externalPort));
// Bind a socket to our port to "claim" it or cry if someone else is now using it
try
{
portHolder = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
portHolder.Bind(new IPEndPoint(localAddr, localPort));
}
catch
{
throw new Exception("Failed, someone is now using local port: " + localPort);
}
// Make a UDP Client that will use that port
UdpClient udpClient = new UdpClient(localPort);
return udpClient;
}
Now for the PubNub relay server code (P2PPeer will be defined later below). There is a lot here so I'm not really gonna explain it but hopefully the code is clear enough to help you understand what is going on
public delegate void NewPeerCallback(P2PPeer newPeer);
public event NewPeerCallback OnNewPeerConnection;
public Pubnub pubnub;
public string pubnubChannelName;
public string localIp;
public string externalIp;
public int localPort;
public int externalPort;
public UdpClient udpClient;
HashSet<string> uniqueIdsPubNubSeen;
object peerLock = new object();
Dictionary<string, P2PPeer> connectedPeers;
string myPeerDataString;
public void InitPubnub(string pubnubPublishKey, string pubnubSubscribeKey, string pubnubChannelName)
{
uniqueIdsPubNubSeen = new HashSet<string>();
connectedPeers = new Dictionary<string, P2PPeer>;
pubnub = new Pubnub(pubnubPublishKey, pubnubSubscribeKey);
myPeerDataString = localIp + " " + externalIp + " " + localPort + " " + externalPort + " " + pubnub.SessionUUID;
this.pubnubChannelName = pubnubChannelName;
pubnub.Subscribe<string>(
pubnubChannelName,
OnPubNubMessage,
OnPubNubConnect,
OnPubNubError);
return pubnub;
}
//// Subscribe callbacks
void OnPubNubConnect(string res)
{
pubnub.Publish<string>(pubnubChannelName, connectionDataString, OnPubNubTheyGotMessage, OnPubNubMessageFailed);
}
void OnPubNubError(PubnubClientError clientError)
{
throw new Exception("PubNub error on subscribe: " + clientError.Message);
}
void OnPubNubMessage(string message)
{
// The message will be the string ["localIp externalIp localPort externalPort","messageId","channelName"]
string[] splitMessage = message.Trim().Substring(1, message.Length - 2).Split(new char[] { ',' });
string peerDataString = splitMessage[0].Trim().Substring(1, splitMessage[0].Trim().Length - 2);
// If you want these, I don't need them
//string peerMessageId = splitMessage[1].Trim().Substring(1, splitMessage[1].Trim().Length - 2);
//string channelName = splitMessage[2].Trim().Substring(1, splitMessage[2].Trim().Length - 2);
string[] pieces = peerDataString.Split(new char[] { ' ', '\t' });
string peerLocalIp = pieces[0].Trim();
string peerExternalIp = pieces[1].Trim();
string peerLocalPort = int.Parse(pieces[2].Trim());
string peerExternalPort = int.Parse(pieces[3].Trim());
string peerPubnubUniqueId = pieces[4].Trim();
pubNubUniqueId = pieces[4].Trim();
// If you are on the same device then you have to do this for it to work idk why
if (peerLocalIp == localIp && peerExternalIp == externalIp)
{
peerLocalIp = "127.0.0.1";
}
// From me, ignore
if (peerPubnubUniqueId == pubnub.SessionUUID)
{
return;
}
// We haven't set up our connection yet, what are we doing
if (udpClient == null)
{
return;
}
// From someone else
IPEndPoint peerEndPoint = new IPEndPoint(IPAddress.Parse(peerExternalIp), peerExternalPort);
IPEndPoint peerEndPointLocal = new IPEndPoint(IPAddress.Parse(peerLocalIp), peerLocalPort);
// First time we have heard from them
if (!uniqueIdsPubNubSeen.Contains(peerPubnubUniqueId))
{
uniqueIdsPubNubSeen.Add(peerPubnubUniqueId);
// Dummy messages to do UDP hole punching, these may or may not go through and that is fine
udpClient.Send(new byte[10], 10, peerEndPoint);
udpClient.Send(new byte[10], 10, peerEndPointLocal); // This is if they are on a LAN, we will try both
pubnub.Publish<string>(pubnubChannelName, myPeerDataString, OnPubNubTheyGotMessage, OnPubNubMessageFailed);
}
// Second time we have heard from them, after then we don't care because we are connected
else if (!connectedPeers.ContainsKey(peerPubnubUniqueId))
{
//bool isOnLan = IsOnLan(IPAddress.Parse(peerExternalIp)); TODO, this would be nice to test for
bool isOnLan = false; // For now we will just do things for both
P2PPeer peer = new P2PPeer(peerLocalIp, peerExternalIp, peerLocalPort, peerExternalPort, this, isOnLan);
lock (peerLock)
{
connectedPeers.Add(peerPubnubUniqueId, peer);
}
// More dummy messages because why not
udpClient.Send(new byte[10], 10, peerEndPoint);
udpClient.Send(new byte[10], 10, peerEndPointLocal);
pubnub.Publish<string>(pubnubChannelName, connectionDataString, OnPubNubTheyGotMessage, OnPubNubMessageFailed);
if (OnNewPeerConnection != null)
{
OnNewPeerConnection(peer);
}
}
}
//// Publish callbacks
void OnPubNubTheyGotMessage(object result)
{
}
void OnPubNubMessageFailed(PubnubClientError clientError)
{
throw new Exception("PubNub error on publish: " + clientError.Message);
}
And here is a P2PPeer
public class P2PPeer
{
public string localIp;
public string externalIp;
public int localPort;
public int externalPort;
public bool isOnLan;
P2PClient client;
public delegate void ReceivedBytesFromPeerCallback(byte[] bytes);
public event ReceivedBytesFromPeerCallback OnReceivedBytesFromPeer;
public P2PPeer(string localIp, string externalIp, int localPort, int externalPort, P2PClient client, bool isOnLan)
{
this.localIp = localIp;
this.externalIp = externalIp;
this.localPort = localPort;
this.externalPort = externalPort;
this.client = client;
this.isOnLan = isOnLan;
if (isOnLan)
{
IPEndPoint endPointLocal = new IPEndPoint(IPAddress.Parse(localIp), localPort);
Thread localListener = new Thread(() => ReceiveMessage(endPointLocal));
localListener.IsBackground = true;
localListener.Start();
}
else
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(externalIp), externalPort);
Thread externalListener = new Thread(() => ReceiveMessage(endPoint));
externalListener.IsBackground = true;
externalListener.Start();
}
}
public void SendBytes(byte[] data)
{
if (client.udpClient == null)
{
throw new Exception("P2PClient doesn't have a udpSocket open anymore");
}
//if (isOnLan) // This would work but I'm not sure how to test if they are on LAN so I'll just use both for now
{
client.udpClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse(localIp), localPort));
}
//else
{
client.udpClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse(externalIp), externalPort));
}
}
// Encoded in UTF8
public void SendString(string str)
{
SendBytes(System.Text.Encoding.UTF8.GetBytes(str));
}
void ReceiveMessage(IPEndPoint endPoint)
{
while (client.udpClient != null)
{
byte[] message = client.udpClient.Receive(ref endPoint);
if (OnReceivedBytesFromPeer != null)
{
OnReceivedBytesFromPeer(message);
}
//string receiveString = Encoding.UTF8.GetString(message);
//Console.Log("got: " + receiveString);
}
}
}
Finally, here are all my usings:
using PubNubMessaging.Core; // Get from PubNub GitHub for C#, I used the Unity3D library
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
I'm open to comments and questions, feel free to give feedback if something here is bad practice or doesn't work. A few bugs were introduced in translation from my code that I'll fix here eventually but this should at least give you the idea of what to do.
Have you tried using the Async functions, here is a example of how you might get it to work it may need a bit of work to make it 100% functional:
public void HolePunch(String ServerIp, Int32 Port)
{
IPEndPoint LocalPt = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], Port);
UdpClient Client = new UdpClient();
Client.ExclusiveAddressUse = false;
Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Client.Client.Bind(LocalPt);
IPEndPoint RemotePt = new IPEndPoint(IPAddress.Parse(ServerIp), Port);
// This Part Sends your local endpoint to the server so if the two peers are on the same nat they can bypass it, you can omit this if you wish to just use the remote endpoint.
byte[] IPBuffer = System.Text.Encoding.UTF8.GetBytes(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString());
byte[] LengthBuffer = BitConverter.GetBytes(IPBuffer.Length);
byte[] PortBuffer = BitConverter.GetBytes(Port);
byte[] Buffer = new byte[IPBuffer.Length + LengthBuffer.Length + PortBuffer.Length];
LengthBuffer.CopyTo(Buffer,0);
IPBuffer.CopyTo(Buffer, LengthBuffer.Length);
PortBuffer.CopyTo(Buffer, IPBuffer.Length + LengthBuffer.Length);
Client.BeginSend(Buffer, Buffer.Length, RemotePt, new AsyncCallback(SendCallback), Client);
// Wait to receve something
BeginReceive(Client, Port);
// you may want to use a auto or manual ResetEvent here and have the server send back a confirmation, the server should have now stored your local (you sent it) and remote endpoint.
// you now need to work out who you need to connect to then ask the server for there remote and local end point then need to try to connect to the local first then the remote.
// if the server knows who you need to connect to you could just have it send you the endpoints as the confirmation.
// you may also need to keep this open with a keepalive packet untill it is time to connect to the peer or peers.
// once you have the endpoints of the peer you can close this connection unless you need to keep asking the server for other endpoints
Client.Close();
}
public void ConnectToPeer(String PeerIp, Int32 Port)
{
IPEndPoint LocalPt = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], Port);
UdpClient Client = new UdpClient();
Client.ExclusiveAddressUse = false;
Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Client.Client.Bind(LocalPt);
IPEndPoint RemotePt = new IPEndPoint(IPAddress.Parse(PeerIp), Port);
Client.Connect(RemotePt);
//you may want to keep the peer client connections in a list.
BeginReceive(Client, Port);
}
public void SendCallback(IAsyncResult ar)
{
UdpClient Client = (UdpClient)ar.AsyncState;
Client.EndSend(ar);
}
public void BeginReceive(UdpClient Client, Int32 Port)
{
IPEndPoint ListenPt = new IPEndPoint(IPAddress.Any, Port);
Object[] State = new Object[] { Client, ListenPt };
Client.BeginReceive(new AsyncCallback(ReceiveCallback), State);
}
public void ReceiveCallback(IAsyncResult ar)
{
UdpClient Client = (UdpClient)((Object[])ar.AsyncState)[0];
IPEndPoint ListenPt = (IPEndPoint)((Object[])ar.AsyncState)[0];
Byte[] receiveBytes = Client.EndReceive(ar, ref ListenPt);
}
I hope this helps.
Update:
Whichever of the UdpClients binds first is the one that will be sent incoming packets by Windows. In your example try moving the code block that sets up the listening thread to the top.
Are you sure the problem is not just that the receive thread is only written to handle a single receive? Try replacing the receive thread with as below.
ThreadPool.QueueUserWorkItem(delegate
{
UdpClient udpServer = new UdpClient();
udpServer.ExclusiveAddressUse = false;
udpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpServer.Client.Bind(localpt);
IPEndPoint inEndPoint = new IPEndPoint(IPAddress.Any, 0);
Console.WriteLine("Listening on " + localpt + ".");
while (inEndPoint != null)
{
byte[] buffer = udpServer.Receive(ref inEndPoint);
Console.WriteLine("Bytes received from " + inEndPoint + " " + Encoding.ASCII.GetString(buffer) + ".");
}
});
Sorry for uploading such a huge piece of code, but i guess this is very clearly explains how things work, and may be really useful. If you will have issues with this code, please let me know.
Note:
this is just a draft
(important) you MUST inform server with your local endpoint. if you will not do it you will not be able to communicate between two peers behind one NAT (for example, on one local machine) even if the server is out of NAT
you must close "puncher" client (at least, i did not manage to receive any packets until i did it). later you will be able to communicate with server using other UdpClient's
of cource it will not work with symmetric NAT
if you find that something in this code is "terrible practice", please tell me, i'm not network expert :)
Server.cs
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using HolePunching.Common;
namespace HolePunching.Server
{
class Server
{
private static bool _isRunning;
private static UdpClient _udpClient;
private static readonly Dictionary<byte, PeerContext> Contexts = new Dictionary<byte, PeerContext>();
private static readonly Dictionary<byte, byte> Mappings = new Dictionary<byte, byte>
{
{1, 2},
{2, 1},
};
static void Main()
{
_udpClient = new UdpClient( Consts.UdpPort );
ListenUdp();
Console.ReadLine();
_isRunning = false;
}
private static async void ListenUdp()
{
_isRunning = true;
while ( _isRunning )
{
try
{
var receivedResults = await _udpClient.ReceiveAsync();
if ( !_isRunning )
{
break;
}
ProcessUdpMessage( receivedResults.Buffer, receivedResults.RemoteEndPoint );
}
catch ( Exception ex )
{
Console.WriteLine( $"Error: {ex.Message}" );
}
}
}
private static void ProcessUdpMessage( byte[] buffer, IPEndPoint remoteEndPoint )
{
if ( !UdpProtocol.UdpInfoMessage.TryParse( buffer, out UdpProtocol.UdpInfoMessage message ) )
{
Console.WriteLine( $" >>> Got shitty UDP [ {remoteEndPoint.Address} : {remoteEndPoint.Port} ]" );
_udpClient.Send( new byte[] { 1 }, 1, remoteEndPoint );
return;
}
Console.WriteLine( $" >>> Got UDP from {message.Id}. [ {remoteEndPoint.Address} : {remoteEndPoint.Port} ]" );
if ( !Contexts.TryGetValue( message.Id, out PeerContext context ) )
{
context = new PeerContext
{
PeerId = message.Id,
PublicUdpEndPoint = remoteEndPoint,
LocalUdpEndPoint = new IPEndPoint( message.LocalIp, message.LocalPort ),
};
Contexts.Add( context.PeerId, context );
}
byte partnerId = Mappings[context.PeerId];
if ( !Contexts.TryGetValue( partnerId, out context ) )
{
_udpClient.Send( new byte[] { 1 }, 1, remoteEndPoint );
return;
}
var response = UdpProtocol.PeerAddressMessage.GetMessage(
partnerId,
context.PublicUdpEndPoint.Address,
context.PublicUdpEndPoint.Port,
context.LocalUdpEndPoint.Address,
context.LocalUdpEndPoint.Port );
_udpClient.Send( response.Data, response.Data.Length, remoteEndPoint );
Console.WriteLine( $" <<< Responsed to {message.Id}" );
}
}
public class PeerContext
{
public byte PeerId { get; set; }
public IPEndPoint PublicUdpEndPoint { get; set; }
public IPEndPoint LocalUdpEndPoint { get; set; }
}
}
Client.cs
using System;
namespace HolePunching.Client
{
class Client
{
public const string ServerIp = "your.server.public.address";
static void Main()
{
byte id = ReadIdFromConsole();
// you need some smarter :)
int localPort = id == 1 ? 61043 : 59912;
var x = new Demo( ServerIp, id, localPort );
x.Start();
}
private static byte ReadIdFromConsole()
{
Console.Write( "Peer id (1 or 2): " );
var id = byte.Parse( Console.ReadLine() );
Console.Title = $"Peer {id}";
return id;
}
}
}
Demo.cs
using HolePunching.Common;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace HolePunching.Client
{
public class Demo
{
private static bool _isRunning;
private static UdpClient _udpPuncher;
private static UdpClient _udpClient;
private static UdpClient _extraUdpClient;
private static bool _extraUdpClientConnected;
private static byte _id;
private static IPEndPoint _localEndPoint;
private static IPEndPoint _serverUdpEndPoint;
private static IPEndPoint _partnerPublicUdpEndPoint;
private static IPEndPoint _partnerLocalUdpEndPoint;
private static string GetLocalIp()
{
var host = Dns.GetHostEntry( Dns.GetHostName() );
foreach ( var ip in host.AddressList )
{
if ( ip.AddressFamily == AddressFamily.InterNetwork )
{
return ip.ToString();
}
}
throw new Exception( "Failed to get local IP" );
}
public Demo( string serverIp, byte id, int localPort )
{
_serverUdpEndPoint = new IPEndPoint( IPAddress.Parse( serverIp ), Consts.UdpPort );
_id = id;
// we have to bind all our UdpClients to this endpoint
_localEndPoint = new IPEndPoint( IPAddress.Parse( GetLocalIp() ), localPort );
}
public void Start( )
{
_udpPuncher = new UdpClient(); // this guy is just for punching
_udpClient = new UdpClient(); // this will keep hole alive, and also can send data
_extraUdpClient = new UdpClient(); // i think, this guy is the best option for sending data (explained below)
InitUdpClients( new[] { _udpPuncher, _udpClient, _extraUdpClient }, _localEndPoint );
Task.Run( (Action) SendUdpMessages );
Task.Run( (Action) ListenUdp );
Console.ReadLine();
_isRunning = false;
}
private void InitUdpClients(IEnumerable<UdpClient> clients, EndPoint localEndPoint)
{
// if you don't want to use explicit localPort, you should create here one more UdpClient (X) and send something to server (it will automatically bind X to free port). then bind all clients to this port and close X
foreach ( var udpClient in clients )
{
udpClient.ExclusiveAddressUse = false;
udpClient.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true );
udpClient.Client.Bind( localEndPoint );
}
}
private void SendUdpMessages()
{
_isRunning = true;
var messageToServer = UdpProtocol.UdpInfoMessage.GetMessage( _id, _localEndPoint.Address, _localEndPoint.Port );
var messageToPeer = UdpProtocol.P2PKeepAliveMessage.GetMessage();
while ( _isRunning )
{
// while we dont have partner's address, we will send messages to server
if ( _partnerPublicUdpEndPoint == null && _partnerLocalUdpEndPoint == null )
{
_udpPuncher.Send( messageToServer.Data, messageToServer.Data.Length, _serverUdpEndPoint );
Console.WriteLine( $" >>> Sent UDP to server [ {_serverUdpEndPoint.Address} : {_serverUdpEndPoint.Port} ]" );
}
else
{
// you can skip it. just demonstration, that you still can send messages to server
_udpClient.Send( messageToServer.Data, messageToServer.Data.Length, _serverUdpEndPoint );
Console.WriteLine( $" >>> Sent UDP to server [ {_serverUdpEndPoint.Address} : {_serverUdpEndPoint.Port} ]" );
// THIS is how we punching hole! very first this message should be dropped by partner's NAT, but it's ok.
// i suppose that this is good idea to send this "keep-alive" messages to peer even if you are connected already,
// because AFAIK "hole" for UDP lives ~2 minutes on NAT. so "we will let it die? NEVER!" (c)
_udpClient.Send( messageToPeer.Data, messageToPeer.Data.Length, _partnerPublicUdpEndPoint );
_udpClient.Send( messageToPeer.Data, messageToPeer.Data.Length, _partnerLocalUdpEndPoint );
Console.WriteLine( $" >>> Sent UDP to peer.public [ {_partnerPublicUdpEndPoint.Address} : {_partnerPublicUdpEndPoint.Port} ]" );
Console.WriteLine( $" >>> Sent UDP to peer.local [ {_partnerLocalUdpEndPoint.Address} : {_partnerLocalUdpEndPoint.Port} ]" );
// "connected" UdpClient sends data much faster,
// so if you have something that your partner cant wait for (voice, for example), send it this way
if ( _extraUdpClientConnected )
{
_extraUdpClient.Send( messageToPeer.Data, messageToPeer.Data.Length );
Console.WriteLine( $" >>> Sent UDP to peer.received EP" );
}
}
Thread.Sleep( 3000 );
}
}
private async void ListenUdp()
{
_isRunning = true;
while ( _isRunning )
{
try
{
// also important thing!
// when you did not punched hole yet, you must listen incoming packets using "puncher" (later we will close it).
// where you already have p2p connection (and "puncher" closed), use "non-puncher"
UdpClient udpClient = _partnerPublicUdpEndPoint == null ? _udpPuncher : _udpClient;
var receivedResults = await udpClient.ReceiveAsync();
if ( !_isRunning )
{
break;
}
ProcessUdpMessage( receivedResults.Buffer, receivedResults.RemoteEndPoint );
}
catch ( SocketException ex )
{
// do something here...
}
catch ( Exception ex )
{
Console.WriteLine( $"Error: {ex.Message}" );
}
}
}
private static void ProcessUdpMessage( byte[] buffer, IPEndPoint remoteEndPoint )
{
// if server sent partner's endpoinps, we will store it and (IMPORTANT) close "puncher"
if ( UdpProtocol.PeerAddressMessage.TryParse( buffer, out UdpProtocol.PeerAddressMessage peerAddressMessage ) )
{
Console.WriteLine( " <<< Got response from server" );
_partnerPublicUdpEndPoint = new IPEndPoint( peerAddressMessage.PublicIp, peerAddressMessage.PublicPort );
_partnerLocalUdpEndPoint = new IPEndPoint( peerAddressMessage.LocalIp, peerAddressMessage.LocalPort );
_udpPuncher.Close();
}
// since we got this message we know partner's endpoint for sure,
// and we can "connect" UdpClient to it, so it will work faster
else if ( UdpProtocol.P2PKeepAliveMessage.TryParse( buffer ) )
{
Console.WriteLine( $" IT WORKS!!! WOW!!! [ {remoteEndPoint.Address} : {remoteEndPoint.Port} ]" );
_extraUdpClientConnected = true;
_extraUdpClient.Connect( remoteEndPoint );
}
else
{
Console.WriteLine( "???" );
}
}
}
}
Protocol.cs
I'm not sure how good this approach, maybe something like protobuf can do it better
using System;
using System.Linq;
using System.Net;
using System.Text;
namespace HolePunching.Common
{
public static class UdpProtocol
{
public static readonly int GuidLength = 16;
public static readonly int PeerIdLength = 1;
public static readonly int IpLength = 4;
public static readonly int IntLength = 4;
public static readonly byte[] Prefix = { 12, 23, 34, 45 };
private static byte[] JoinBytes( params byte[][] bytes )
{
var result = new byte[bytes.Sum( x => x.Length )];
int pos = 0;
for ( int i = 0; i < bytes.Length; i++ )
{
for ( int j = 0; j < bytes[i].Length; j++, pos++ )
{
result[pos] = bytes[i][j];
}
}
return result;
}
#region Helper extensions
private static bool StartsWith( this byte[] #this, byte[] value, int offset = 0 )
{
if ( #this == null || value == null || #this.Length < offset + value.Length )
{
return false;
}
for ( int i = 0; i < value.Length; i++ )
{
if ( #this[i + offset] < value[i] )
{
return false;
}
}
return true;
}
private static byte[] ToUnicodeBytes( this string #this )
{
return Encoding.Unicode.GetBytes( #this );
}
private static byte[] Take( this byte[] #this, int offset, int length )
{
return #this.Skip( offset ).Take( length ).ToArray();
}
public static bool IsSuitableUdpMessage( this byte[] #this )
{
return #this.StartsWith( Prefix );
}
public static int GetInt( this byte[] #this )
{
if ( #this.Length != 4 )
throw new ArgumentException( "Byte array must be exactly 4 bytes to be convertible to uint." );
return ( ( ( #this[0] << 8 ) + #this[1] << 8 ) + #this[2] << 8 ) + #this[3];
}
public static byte[] ToByteArray( this int value )
{
return new[]
{
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value
};
}
#endregion
#region Messages
public abstract class UdpMessage
{
public byte[] Data { get; }
protected UdpMessage( byte[] data )
{
Data = data;
}
}
public class UdpInfoMessage : UdpMessage
{
private static readonly byte[] MessagePrefix = { 41, 57 };
private static readonly int MessageLength = Prefix.Length + MessagePrefix.Length + PeerIdLength + IpLength + IntLength;
public byte Id { get; }
public IPAddress LocalIp { get; }
public int LocalPort { get; }
private UdpInfoMessage( byte[] data, byte id, IPAddress localIp, int localPort )
: base( data )
{
Id = id;
LocalIp = localIp;
LocalPort = localPort;
}
public static UdpInfoMessage GetMessage( byte id, IPAddress localIp, int localPort )
{
var data = JoinBytes( Prefix, MessagePrefix, new[] { id }, localIp.GetAddressBytes(), localPort.ToByteArray() );
return new UdpInfoMessage( data, id, localIp, localPort );
}
public static bool TryParse( byte[] data, out UdpInfoMessage message )
{
message = null;
if ( !data.StartsWith( Prefix ) )
return false;
if ( !data.StartsWith( MessagePrefix, Prefix.Length ) )
return false;
if ( data.Length != MessageLength )
return false;
int index = Prefix.Length + MessagePrefix.Length;
byte id = data[index];
index += PeerIdLength;
byte[] localIpBytes = data.Take( index, IpLength );
var localIp = new IPAddress( localIpBytes );
index += IpLength;
byte[] localPortBytes = data.Take( index, IntLength );
int localPort = localPortBytes.GetInt();
message = new UdpInfoMessage( data, id, localIp, localPort );
return true;
}
}
public class PeerAddressMessage : UdpMessage
{
private static readonly byte[] MessagePrefix = { 36, 49 };
private static readonly int MessageLength = Prefix.Length + MessagePrefix.Length + PeerIdLength + ( IpLength + IntLength ) * 2;
public byte Id { get; }
public IPAddress PublicIp { get; }
public int PublicPort { get; }
public IPAddress LocalIp { get; }
public int LocalPort { get; }
private PeerAddressMessage( byte[] data, byte id, IPAddress publicIp, int publicPort, IPAddress localIp, int localPort )
: base( data )
{
Id = id;
PublicIp = publicIp;
PublicPort = publicPort;
LocalIp = localIp;
LocalPort = localPort;
}
public static PeerAddressMessage GetMessage( byte id, IPAddress publicIp, int publicPort, IPAddress localIp, int localPort )
{
var data = JoinBytes( Prefix, MessagePrefix, new[] { id },
publicIp.GetAddressBytes(), publicPort.ToByteArray(),
localIp.GetAddressBytes(), localPort.ToByteArray() );
return new PeerAddressMessage( data, id, publicIp, publicPort, localIp, localPort );
}
public static bool TryParse( byte[] data, out PeerAddressMessage message )
{
message = null;
if ( !data.StartsWith( Prefix ) )
return false;
if ( !data.StartsWith( MessagePrefix, Prefix.Length ) )
return false;
if ( data.Length != MessageLength )
return false;
int index = Prefix.Length + MessagePrefix.Length;
byte id = data[index];
index += PeerIdLength;
byte[] publicIpBytes = data.Take( index, IpLength );
var publicIp = new IPAddress( publicIpBytes );
index += IpLength;
byte[] publicPortBytes = data.Take( index, IntLength );
int publicPort = publicPortBytes.GetInt();
index += IntLength;
byte[] localIpBytes = data.Take( index, IpLength );
var localIp = new IPAddress( localIpBytes );
index += IpLength;
byte[] localPortBytes = data.Take( index, IntLength );
int localPort = localPortBytes.GetInt();
message = new PeerAddressMessage( data, id, publicIp, publicPort, localIp, localPort );
return true;
}
}
public class P2PKeepAliveMessage : UdpMessage
{
private static readonly byte[] MessagePrefix = { 11, 19 };
private static P2PKeepAliveMessage _message;
private P2PKeepAliveMessage( byte[] data )
: base( data )
{
}
public static bool TryParse( byte[] data )
{
if ( !data.StartsWith( Prefix ) )
return false;
if ( !data.StartsWith( MessagePrefix, Prefix.Length ) )
return false;
return true;
}
public static P2PKeepAliveMessage GetMessage()
{
if ( _message == null )
{
var data = JoinBytes( Prefix, MessagePrefix );
_message = new P2PKeepAliveMessage( data );
}
return _message;
}
}
#endregion
}
}
Related
Does anyone have an example of using a NetMQPoller in conjunction with sending messages from an asynchronous task? i.e.
// 1. Receive a message from one of many clients
// 2. Handle client requests on multiple async Task's (may be file bound)
// 3. Respond to clients when long running Task finishes
We are currently using a very barebones Dealer/Router setup for multiple clients to talk to a single server. On the server we pass each client request on to a pool of Tasks/Threads that handle the request, generate a response, and then attempt to respond to the original client. Because I'm new to this I didn't realize calling the client on a different Task/Thread would bork things but it makes sense now.
What I'm looking for is the right way to handle this. I could definitely have a BlockingCollection of messages to send back and just service that from the original thread, but I recently discovered the NetMQPoller and it sort of sounds like it has some of this built in. I am not 100% sure if it can help me send messages on the original thread or not but there are a lot of parts to it that sound promising. Can anyone point me at an example of sending messages back to the Dealer/client from the Router/server on a different thread than the original message was received on?
UPDATE
I think I may have sort-of solved my own question by combining
Router-Dealer example with the
WithPoller unit test from NetMQUnitTests.cs
class Program
{
static void Main( string[] args )
{
// NOTES
// 1. Use ThreadLocal<DealerSocket> where each thread has
// its own client DealerSocket to talk to server
// 2. Each thread can send using it own socket
// 3. Each thread socket is added to poller
const int delay = 3000; // millis
var clientSocketPerThread = new ThreadLocal<DealerSocket>();
using( var server = new RouterSocket("#tcp://127.0.0.1:5556") )
using( var queue = new NetMQQueue<MyResponse>() )
using( var poller = new NetMQPoller { queue } )
{
// Start some threads, each with its own DealerSocket
// to talk to the server socket. Creates lots of sockets,
// but no nasty race conditions no shared state, each
// thread has its own socket, happy days.
for( int i = 0; i < 3; i++ )
{
Task.Factory.StartNew(state =>
{
DealerSocket client = null;
if( !clientSocketPerThread.IsValueCreated )
{
client = new DealerSocket();
client.Options.Identity =
Encoding.Unicode.GetBytes(state.ToString());
client.Connect("tcp://127.0.0.1:5556");
client.ReceiveReady += Client_ReceiveReady;
clientSocketPerThread.Value = client;
poller.Add(client);
}
else
{
client = clientSocketPerThread.Value;
}
while( true )
{
var messageToServer = new NetMQMessage();
messageToServer.AppendEmptyFrame();
messageToServer.Append(state.ToString());
Console.WriteLine("======================================");
Console.WriteLine(" OUTGOING MESSAGE TO SERVER ");
Console.WriteLine("======================================");
PrintFrames("Client Sending", messageToServer);
client.SendMultipartMessage(messageToServer);
Thread.Sleep(delay);
}
}, string.Format("client {0}", i), TaskCreationOptions.LongRunning);
}
queue.ReceiveReady += ( sender, e ) =>
{
var queueItem = e.Queue.Dequeue();
var messageToClient = new NetMQMessage();
messageToClient.Append(queueItem.ClientId);
messageToClient.AppendEmptyFrame();
messageToClient.Append(queueItem.MessageToClient);
server.SendMultipartMessage(messageToClient);
};
// start the poller
poller.RunAsync();
// server loop
while( true )
{
var clientMessage = server.ReceiveMultipartMessage();
Console.WriteLine("======================================");
Console.WriteLine(" INCOMING CLIENT MESSAGE FROM CLIENT ");
Console.WriteLine("======================================");
PrintFrames("Server receiving", clientMessage);
if( clientMessage.FrameCount == 3 )
{
var clientAddress = clientMessage[0];
var clientOriginalMessage = clientMessage[2].ConvertToString();
string response = string.Format("{0} back from server {1}",
clientOriginalMessage, DateTime.Now.ToLongTimeString());
Task.Factory.StartNew(async() =>
{
await Task.Delay(200);
var clientResponse = new MyResponse()
{
ClientId = clientAddress,
MessageToClient = response
};
queue.Enqueue(clientResponse);
}, TaskCreationOptions.LongRunning);
}
}
}
}
static void PrintFrames( string operationType, NetMQMessage message )
{
for( int i = 0; i < message.FrameCount; i++ )
{
Console.WriteLine("{0} Socket : Frame[{1}] = {2}", operationType, i,
message[i].ConvertToString());
}
}
static void Client_ReceiveReady( object sender, NetMQSocketEventArgs e )
{
bool hasmore = false;
e.Socket.ReceiveFrameString(out hasmore);
if( hasmore )
{
string result = e.Socket.ReceiveFrameString(out hasmore);
Console.WriteLine("REPLY {0}", result);
}
}
class MyResponse
{
public NetMQFrame ClientId;
public string MessageToClient;
}
}
There are two programs that I made that didn't work. There are the server and the client. The server accepts many client by giving a user an ID (starting from 0). The server sends out the command to the specific client based up the server's id. (Example: 200 client are connected to 1 server. The server's selected id is '5', so the server will send the command to all of the client, and the client will ask the server what ID he wants to execute his command on, and if it's '5', that client will execute and send data to the server). The client has many commands, but to create the smallest code with the same error, I only use 1 command (dir). Basically, the server sends the command to the client and if it matches with the client current id and the server current id, it will process the command. By default, the server's current ID is 10. Here are the list of the commands to help the people who wants to answer:
Server Command:
list (Shows all of the users ID connected and the server's current ID) --> Happens on server
dir (request the client to send its dir listing) --> Sent by the client, read by the Server
set (set the server's current id to any number) (example: 'set 4')
Client:
using System;
using System.Speech.Synthesis;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace clientControl
{
class Program
{
public static string directory = #"C:\";
public static int id = -10;
public static Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
Connect();
getSession();
ReadResponse(sck);
}
static byte[] readResponseFunc()
{
long fileSize = 0;
string fileSizeInString = null;
byte[] fileSizeInByteArray = new byte[1024];
int fileSizeLength = sck.Receive(fileSizeInByteArray);
for (int i = 0; i < fileSizeLength; i++)
{
fileSizeInString = fileSizeInString + (char)fileSizeInByteArray[i];
}
try
{
fileSize = Convert.ToInt64(fileSizeInString);
}
catch { Console.WriteLine(fileSizeInString); }
sck.Send(Encoding.ASCII.GetBytes("a"));
byte[] responseUnknown = new byte[1];
sck.Receive(responseUnknown);
if (Encoding.ASCII.GetString(responseUnknown) == "b")
{
byte[] dataInByteArray = new byte[fileSize];
int dataLength = sck.Receive(dataInByteArray);
return dataInByteArray;
}
return Encoding.ASCII.GetBytes("ERROR RESPONSE FUNC");
}
static void getSession()
{
byte[] message_1 = Encoding.ASCII.GetBytes("make_id");
sck.Send(Encoding.ASCII.GetBytes(message_1.Length.ToString()));
byte[] responseUnknown = new byte[1];
if (Encoding.ASCII.GetString(responseUnknown) == "a")
{
sck.Send(Encoding.ASCII.GetBytes("b"));
sck.Send(message_1);
}
byte[] receivedID = readResponseFunc();
id = Convert.ToInt32(Encoding.ASCII.GetString(receivedID));
}
static bool SocketConnected(Socket s)
{
bool part1 = s.Poll(1000, SelectMode.SelectRead);
bool part2 = (s.Available == 0);
if (part1 && part2)
return false;
else
return true;
}
static void ReadResponse(Socket sck)
{
while (true)
{
if (SocketConnected(sck) == true)
{
try
{
string response = Encoding.ASCII.GetString(readResponseFunc());
byte[] message_1 = Encoding.ASCII.GetBytes("get_id");
sck.Send(Encoding.ASCII.GetBytes(message_1.Length.ToString()));
byte[] responseUnknown = new byte[1];
if (Encoding.ASCII.GetString(responseUnknown) == "a")
{
sck.Send(Encoding.ASCII.GetBytes("b"));
sck.Send(message_1);
}
byte[] response_2InByteArray = readResponseFunc();
string response_2 = Encoding.ASCII.GetString(response_2InByteArray);
if (Convert.ToInt32(response_2) == id)
{
if (response == "dir")
{
string resultOfDirring = "Current Directory: " + directory + "\n\n";
string[] folderListingArray = Directory.GetDirectories(directory);
foreach (string dir in folderListingArray)
{
string formed = "DIRECTORY: " + Path.GetFileName(dir);
resultOfDirring = resultOfDirring + formed + Environment.NewLine;
}
string[] fileListingArray = Directory.GetFiles(directory);
foreach (var file in fileListingArray)
{
FileInfo fileInfo = new FileInfo(file);
string formed = "FILE: " + Path.GetFileName(file) + " - FILE SIZE: " + fileInfo.Length + " BYTES";
resultOfDirring = resultOfDirring + formed + Environment.NewLine;
}
byte[] message_11 = Encoding.ASCII.GetBytes(resultOfDirring);
sck.Send(Encoding.ASCII.GetBytes(message_11.Length.ToString()));
byte[] responseUnknown_11 = new byte[1];
if (Encoding.ASCII.GetString(responseUnknown_11) == "a")
{
sck.Send(Encoding.ASCII.GetBytes("b"));
sck.Send(message_11);
}
}
}
else { }
}
catch { if (SocketConnected(sck) == false) { Console.WriteLine("Client Disconnected: " + sck.RemoteEndPoint); break; }; }
}
else if (SocketConnected(sck) == false) { Console.WriteLine("Client Disconnected: " + sck.RemoteEndPoint); break; }
}
}
static void Connect()
{
while (true)
{
try
{
sck.Connect(IPAddress.Parse("127.0.0.1"), 80);
break;
}
catch { }
}
}
}
}
Server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
namespace serverControl
{
class Program
{
public static int ftpNum = 1;
public static List<int> listOfClient = new List<int>();
public static TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
public static string message = null;
public static int id = 0;
public static int selected_id = 10;
static void Main(string[] args)
{
server.Start();
Thread startHandlingClientThread = new Thread(startHandlingClient);
startHandlingClientThread.Start();
while (true)
{
Console.Write(":> ");
string rawmessage = Console.ReadLine();
if (rawmessage == "list")
{
Console.WriteLine("SELECTED ID: " + selected_id);
Console.WriteLine("List of Clients ID:");
for (int i = 0; i < listOfClient.Count; i++)
{
Console.WriteLine(listOfClient[i]);
}
message = rawmessage+"PREVENT_REPETITION_IN_COMMAND";
}
else if (rawmessage.Contains("set ")) { int wantedChangeId = Convert.ToInt32(rawmessage.Replace("set ", "")); selected_id = wantedChangeId; message = rawmessage+ "PREVENT_REPETITION_IN_COMMAND"; }
else
{
message = rawmessage;
}
}
}
static byte[] readResponseFunc(Socket sck)
{
long fileSize = 0;
string fileSizeInString = null;
byte[] fileSizeInByteArray = new byte[1024];
int fileSizeLength = sck.Receive(fileSizeInByteArray);
for (int i = 0; i < fileSizeLength; i++)
{
fileSizeInString = fileSizeInString + (char)fileSizeInByteArray[i];
}
fileSize = Convert.ToInt64(fileSizeInString);
sck.Send(Encoding.ASCII.GetBytes("a"));
byte[] responseUnknown = new byte[1];
sck.Receive(responseUnknown);
if (Encoding.ASCII.GetString(responseUnknown) == "b")
{
byte[] dataInByteArray = new byte[fileSize];
int dataLength = sck.Receive(dataInByteArray);
return dataInByteArray;
}
return Encoding.ASCII.GetBytes("ERROR RESPONSE FUNC");
}
static void startHandlingClient()
{
while (true)
{
handleClient(server);
}
}
static void handleClient(TcpListener clientToAccept)
{
Socket sck = clientToAccept.AcceptSocket();
Thread myNewThread = new Thread(() => ReadResponse(sck));
myNewThread.Start();
}
static bool SocketConnected(Socket s)
{
bool part1 = s.Poll(1000, SelectMode.SelectRead);
bool part2 = (s.Available == 0);
if (part1 && part2)
return false;
else
return true;
}
static void ReadResponse(Socket sck)
{
Thread myNewThread = new Thread(() => SendtoClient(sck));
myNewThread.Start();
Thread.Sleep(2000);
while (true)
{
if (SocketConnected(sck) == true)
{
try
{
byte[] dataInByteArray = readResponseFunc(sck);
string response = Encoding.ASCII.GetString(dataInByteArray);
Console.WriteLine("res: " + response);
if (response != "make_id" && response != "get_id") { Console.WriteLine(response); }
if (response == "make_id")
{
Console.WriteLine("Someone wants an ID");
byte[] message_1 = Encoding.ASCII.GetBytes(id.ToString());
listOfClient.Add(id);
// START
sck.Send(Encoding.ASCII.GetBytes(message_1.Length.ToString()));
byte[] responseUnknown = new byte[1];
if (Encoding.ASCII.GetString(responseUnknown) == "a")
{
sck.Send(Encoding.ASCII.GetBytes("b"));
sck.Send(message_1);
}
id++;
}
if (response == "get_id")
{
byte[] message_1 = Encoding.ASCII.GetBytes(selected_id.ToString());
sck.Send(Encoding.ASCII.GetBytes(message_1.Length.ToString()));
byte[] responseUnknown = new byte[1];
if (Encoding.ASCII.GetString(responseUnknown) == "a")
{
sck.Send(Encoding.ASCII.GetBytes("b"));
sck.Send(message_1);
}
}
}
catch { if (SocketConnected(sck) == false) { Console.WriteLine("Client Disconnected: " + sck.RemoteEndPoint); break; }; }
}
else if (SocketConnected(sck) == false) { Console.WriteLine("Client Disconnected: " + sck.RemoteEndPoint); break; }
}
}
static void SendtoClient(Socket sck)
{
string tempmessage = null;
while (true)
{
if (SocketConnected(sck) == true)
{
if (tempmessage != message)
{
if (!message.Contains("PREVENT_REPETITION_IN_COMMAND"))
{
byte[] message_1 = Encoding.ASCII.GetBytes(message);
sck.Send(Encoding.ASCII.GetBytes(message_1.Length.ToString()));
byte[] responseUnknown = new byte[1];
if (Encoding.ASCII.GetString(responseUnknown) == "a")
{
sck.Send(Encoding.ASCII.GetBytes("b"));
sck.Send(message_1);
}
}
tempmessage = message;
}
}
else if (SocketConnected(sck) == false)
{ Console.WriteLine("Client Disconnected: " + sck.RemoteEndPoint); break; }
}
}
}
}
Problem:
The problem is within the GetSession or the ReadResponseFunc function. The client thinks that his ID received by the server is 'a' (it's suppose to be an integer). I don't want a solution that suggest me to use other libs or
the TcpClient class
Bounty:
I'll put up a bounty with no expiry time to those who solve the problem.
The logic in your code is very confusing. My question to you: Why are you sending 'a' and 'b' back and forth between the server and client? Is it some sort of confirmation that the message has been received?
Anyways, throughout the quick tests I've done just now, it seems that the problem is Line 59 of your server:
sck.Send(Encoding.ASCII.GetBytes("a"));
Here's what I figured out during testing:
Server executes.
Client executes.
Client sends server the length of "make_id" (Line 51 of client)
Client waits for a response to return.
Server reads the value and sends back 'a' (Line 59 of server)
You may want to spend some time to straighten out your protocol so it's less confusing and more organized. That would help people like me and you spot bugs much more easily.
The user 'Bobby' has already found your problem. Credits go to him.
I further suggest that you use less threads, as thread synchronisation needs some effort when doing it right: all data that is accessed from different threads must be secured by locks, so that no outdated values remain in the CPU caches. Use .net Monitor from the "threading synchronisation primitives" for that job.
About the threads themselves:
You should have only one thread in the server. This thread takes all clients from a list (secured by Monitor), in which they were added when connection attempts were received. On each client it checks for incoming messages, evaluates them and replies with own messages if needed.
The client also has just one thread, that will loop (dont forget a sleep in the loop or you will have 100% usage of the used CPU core), send messages when desired and wait for replies when messages were sent.
About the messages:
I already gave some proposals in a comment to Bobby's answer. I suggest you create a CMessage class that has a Serialize() and Deserialze() to create a byte array to send from the CMessage members (serializing the content) or vice versa filling the members from the received bytes. You then may use this class in both programs and you'll have common solution.
I've created a unix socket in .NET Core on Linux (Ubuntu 16.04):
var unixSocket = "/var/run/mysqld/mysqld.sock";
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
Now how do I connect the socket?
The .NET Core API lists various Socket.Connect options, but all except for the first one deal with IP Addresses:
public void Connect(EndPoint remoteEP)
public void Connect(IPAddress address, int port)
public void Connect(IPAddress[] addresses, int port)
public void Connect(string host, int port)
The System.Net API defines a DNSEndpoint and an IPEndpoint, but I can't seem to find a UnixEndpoint or similar class to pass to Socket.Connect(EndPoint remoteEP)
Update: .NET Standard 2.1 / .NET Core 2.1 contains a UnixDomainSocketEndPoint Class
Original answer, applies to versions of .NET Standard / .NET Core prior to 2.1:
There does not seem to be a built-in UnixEndPoint class for .NET Core or a library that implements one at the time of this writing. The UnixEndPoint class from the Mono.Posix project can be easily adapted to work with .NET Core, however:
// copied from https://github.com/mono/mono/blob/master/mcs/class/Mono.Posix/Mono.Unix/UnixEndPoint.cs
//
// Mono.Unix.UnixEndPoint: EndPoint derived class for AF_UNIX family sockets.
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo#ximian.com)
//
// (C) 2003 Ximian, Inc (http://www.ximian.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Net.Sockets;
using System.Text;
namespace System.Net
{
public class UnixEndPoint : EndPoint
{
string filename;
public UnixEndPoint (string filename)
{
if (filename == null)
throw new ArgumentNullException ("filename");
if (filename == "")
throw new ArgumentException ("Cannot be empty.", "filename");
this.filename = filename;
}
public string Filename {
get {
return(filename);
}
set {
filename=value;
}
}
public override AddressFamily AddressFamily {
get { return AddressFamily.Unix; }
}
public override EndPoint Create (SocketAddress socketAddress)
{
/*
* Should also check this
*
int addr = (int) AddressFamily.Unix;
if (socketAddress [0] != (addr & 0xFF))
throw new ArgumentException ("socketAddress is not a unix socket address.");
if (socketAddress [1] != ((addr & 0xFF00) >> 8))
throw new ArgumentException ("socketAddress is not a unix socket address.");
*/
if (socketAddress.Size == 2) {
// Empty filename.
// Probably from RemoteEndPoint which on linux does not return the file name.
UnixEndPoint uep = new UnixEndPoint ("a");
uep.filename = "";
return uep;
}
int size = socketAddress.Size - 2;
byte [] bytes = new byte [size];
for (int i = 0; i < bytes.Length; i++) {
bytes [i] = socketAddress [i + 2];
// There may be junk after the null terminator, so ignore it all.
if (bytes [i] == 0) {
size = i;
break;
}
}
string name = Encoding.UTF8.GetString (bytes, 0, size);
return new UnixEndPoint (name);
}
public override SocketAddress Serialize ()
{
byte [] bytes = Encoding.UTF8.GetBytes (filename);
SocketAddress sa = new SocketAddress (AddressFamily, 2 + bytes.Length + 1);
// sa [0] -> family low byte, sa [1] -> family high byte
for (int i = 0; i < bytes.Length; i++)
sa [2 + i] = bytes [i];
//NULL suffix for non-abstract path
sa[2 + bytes.Length] = 0;
return sa;
}
public override string ToString() {
return(filename);
}
public override int GetHashCode ()
{
return filename.GetHashCode ();
}
public override bool Equals (object o)
{
UnixEndPoint other = o as UnixEndPoint;
if (other == null)
return false;
return (other.filename == filename);
}
}
}
With this class in your project, the socket can be connected like so:
var unixSocket = "/var/run/mysqld/mysqld.sock";
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
var unixEp = new UnixEndPoint(unixSocket);
socket.Connect(unixEp);
using System.Net.Sockets;
var unixSocketName = "/var/run/mysqld/mysqld.sock";
var unixSocket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
unixSocket.Connect(new UnixDomainSocketEndPoint(unixSocketName));
Because I couldn't find a good example for unix sockets I designed my own.
Its very rudementary and has not many errorcatching, but it works.
Here it is:
using System;
using System.Net.Sockets;
using System.Threading;
namespace mps.unix.socket
{
public class UnixSocket
{
private Socket recvSocket;
private string unixSocketpath; // needed for cleanup
private bool running = true;
private byte[] data = new byte[0];
public UnixSocket(string unixSocket)
{
this.unixSocketpath = unixSocket;
this.recvSocket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
System.IO.File.Delete(unixSocketpath);
var ep = new UnixDomainSocketEndPoint(unixSocketpath);
recvSocket.Bind(ep);
recvSocket.Listen();
Thread w = new Thread(worker);
w.Start();
}
private void worker()
{
while (this.running)
{
try
{
var clientSocket = this.recvSocket.Accept(); // waits for 'client' to 'connect'
Thread t = new Thread(delegate () { clientWorker(clientSocket); });
t.Start();
}
catch (Exception) { }
}
}
private void clientWorker(Socket s)
{
while (running)
{
byte[] d = new byte[s.ReceiveBufferSize];
int length = s.Receive(d);
lock (this.data)
{
byte[] newData = new byte[this.data.Length + length];
Buffer.BlockCopy(this.data, 0, newData, 0, this.data.Length);
Buffer.BlockCopy(d, 0, newData, this.data.Length, length);
this.data = newData;
}
}
s.Close();
}
public bool isRunning()
{
return this.running;
}
public void Stop()
{
this.running = false;
Thread.Sleep(10);
this.recvSocket.Close();
System.IO.File.Delete(this.unixSocketpath);
}
public bool hasData()
{
return this.data.Length > 0;
}
public int dataLength()
{
lock (this.data)
return this.data.Length;
}
public byte[] getData()
{
lock (this.data)
{
var d = this.data;
this.data = new byte[0];
return d;
}
}
public byte[] getData(int length)
{
lock (this.data)
{
if (this.data.Length < length)
return this.getData();
//var d = this.data.Take(length).ToArray();
//var rest = new byte[0];
byte[] taken = new byte[length];
byte[] rest = new byte[this.data.Length - length];
Buffer.BlockCopy(this.data, 0, taken, 0, length);
Buffer.BlockCopy(this.data, length, rest, 0, rest.Length);
this.data = rest;
return taken;
}
}
}
}
Here is a litte program where you can see how to use the class:
using System;
using System.Threading;
using mps.unix.socket;
namespace TEST_mps.unix.socket
{
class Program { static void Main(string[] args) { var P = new TEST(); } }
class TEST
{
public TEST()
{
var v = new UnixSocket("/tmp/test.mps.unix.sock");
Thread t = new Thread(delegate () { runner(v); });
t.Start();
}
private void runner(UnixSocket s)
{
while (s.isRunning())
{
Thread.Sleep(5);
if (s.hasData())
{
string responseData = System.Text.Encoding.ASCII.GetString(s.getData());
Console.WriteLine("Message received: {0}", responseData);
if (responseData == "END")
s.Stop();
}
}
}
}
}
I compiled this with .Net 6.0 for linux-x64.
For testing run the software and then send something to the socket:
On server:
root#server:# ./test.mps.unix.socket
Message received: this is a message for the socket!
Message received: here is another one..
Message received: END
root#server:#
From another shell:
root#server:# echo -n 'this is a message for the socket!' | socat UNIX-CONNECT:/tmp/test.mps.unix.sock STDIO
root#server:# echo -n 'here is another one..' | socat UNIX-CONNECT:/tmp/test.mps.unix.sock STDIO
root#server:# echo -n 'END' | socat UNIX-CONNECT:/tmp/test.mps.unix.sock STDIO
If you found bugs or can make improvements tell me and I will see if we can add it to the example :)
I just finished my C# proxy containing 2 sockets (one client-socket receiving data from a certain client, one server-socket receiving data from a certain server).
- Method I)
The fully working receive/send procedure from a previous early-alpha version uses the following memory eating, quick and dirty method I won't implement again:
while (true)
{
if (ClientSocket.Available > 0)
{
// Received data from the game client
byte[] buf = new byte[ClientSocket.Available];
ClientSocket.Receive(buf);
Packet p = new Packet(buf);
Logger.Log(p.ID, LogType.PACKET);
// Forward re-encrypted data back to the official server
ServerSocket.Send(p.Encrypt());
}
if (ServerSocket.Available > 0)
{
// Received Data from the official server
byte[] buf = new byte[ServerSocket.Available];
ServerSocket.Receive(buf);
Packet p = new Packet(buf);
Logger.Log(p.ID, LogType.PACKET);
// Forward re-encrypted data back to the game client
ClientSocket.Send(p.Encrypt());
}
}
- Method II)
The not working receive/send procedure from the actual version uses the following memory friendly method split into 2 threads:
class ClientReceiveThread
{
public Thread T { get; set; }
public ClientReceiveThread(Socket ClientSocket, Socket ServerSocket)
{
T = new Thread(() =>
{
try
{
while (ClientSocket.Available > 0)
{
// Received data from the game client
byte[] buf = new byte[ClientSocket.Available];
ClientSocket.Receive(buf);
Packet p = new Packet(buf);
Logger.Log(p.ID, LogType.PACKET);
// Forward re-encrypted data back to the official server
ServerSocket.Send(p.Encrypt());
}
}
catch (Exception e)
{
ExceptionHandler.Handle(e);
}
});
T.Start();
}
}
class ServerReceiveThread
{
public Thread T { get; set; }
public ServerReceiveThread(Socket ClientSocket, Socket ServerSocket)
{
T = new Thread(() =>
{
try
{
while (ServerSocket.Available > 0)
{
// Received Data from the official server
byte[] buf = new byte[ServerSocket.Available];
ServerSocket.Receive(buf);
Packet p = new Packet(buf);
Logger.Log(p.ID, LogType.PACKET);
// Forward re-encrypted data back to the game client
ClientSocket.Send(p.Encrypt());
}
}
catch (Exception e)
{
ExceptionHandler.Handle(e);
}
});
T.Start();
}
}
Method I) does insofar work that both Client- and Serversockets receive data, while method II) only receives data from the ClientReceiveThread.
Why does ServerReceiveThread in method II) not receive data? It's basically the same code as in the while(true) loop, only ported to a seperate thread.
Any suggestions or answers are highly appreciated.
Thanks in advance!
Fixed it by avoiding the "Available" property:
class ClientReceiveThread
{
public Thread T { get; set; }
public ClientReceiveThread(Socket ClientSocket, Socket ServerSocket)
{
T = new Thread(() =>
{
try
{
byte[] buf = new byte[1024];
while (ClientSocket.Receive(buf) > 0)
{
// Received data from the game client
Packet p = new Packet(buf);
Logger.Log(p.ID, LogType.PACKET);
// Forward re-encrypted data back to the official server
ServerSocket.Send(p.Encrypt());
}
}
catch (Exception e)
{
ExceptionHandler.Handle(e);
}
});
T.Start();
}
}
class ServerReceiveThread
{
public Thread T { get; set; }
public ServerReceiveThread(Socket ClientSocket, Socket ServerSocket)
{
T = new Thread(() =>
{
try
{
byte[] buf = new byte[1024];
while (ServerSocket.Receive(buf) > 0)
{
// Received Data from the official server
Packet p = new Packet(buf);
Logger.Log(p.ID, LogType.PACKET);
// Forward re-encrypted data back to the game client
ClientSocket.Send(p.Encrypt());
}
}
catch (Exception e)
{
ExceptionHandler.Handle(e);
}
});
T.Start();
}
}
first question here!
I have written and borrowed code to form a IP Address and MAC address finder console application. It send Asynchrous Ping request, and for every IP Address it finds it does and ARP request to find the MAC address.
How can I configure this to work with a different submask than /24 (255.255.255.0) to find IP Address's?
This is NOT for a botnet. It is for my friend who is a network technician.
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace FindIpAndMacPro
{
internal class Program
{
private static CountdownEvent _countdown;
private static int _upCount;
private static readonly object LockObj = new object();
private static void Main()
{
_countdown = new CountdownEvent(1);
var sw = new Stopwatch();
sw.Start();
Console.Write("Skriv in IP-Adress");
string ipBase = Console.ReadLine();
for (int i = 0; i < 255; i++)
{
string ip = ipBase + "." + i;
//Console.WriteLine(ip);
var p = new Ping();
p.PingCompleted += PPingCompleted;
_countdown.AddCount();
p.SendAsync(ip, 100, ip);
}
_countdown.Signal();
_countdown.Wait();
sw.Stop();
new TimeSpan(sw.ElapsedTicks);
Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, _upCount);
Console.WriteLine("External IP (whatismyip.com): {0}", GetExternalIp());
Console.ReadLine();
}
private static void PPingCompleted (object sender, PingCompletedEventArgs e)
{
var ip = (string) e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
{
string name;
string macAddress = "";
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ip);
name = hostEntry.HostName;
macAddress = GetMac(ip);
}
catch (SocketException)
{
name = "?";
}
Console.WriteLine("{0} | {1} ({2}) is up: ({3} ms)", ip, macAddress, name, e.Reply.RoundtripTime);
}
lock (LockObj)
{
_upCount++;
}
}
else if (e.Reply == null)
{
Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
}
_countdown.Signal();
}
[DllImport ("iphlpapi.dll")]
public static extern int SendARP (int destIp, int srcIp, [Out] byte[] pMacAddr, ref int phyAddrLen);
private static string GetMac (String ip)
{
IPAddress addr = IPAddress.Parse(ip);
var mac = new byte[6];
int len = mac.Length;
SendARP(ConvertIpToInt32(addr), 0, mac, ref len);
return BitConverter.ToString(mac, 0, len);
}
private static Int32 ConvertIpToInt32 (IPAddress apAddress)
{
byte[] bytes = apAddress.GetAddressBytes();
return BitConverter.ToInt32(bytes, 0);
}
private static string GetExternalIp()
{
const string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
var wc = new WebClient();
var utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
Console.WriteLine(we.ToString());
}
return requestHtml;
}
}
}
first of all you had to calculate the network from subnetmask.if your subnetmask is 255.255.248.0 then you can easly calculate by subtracting the subnetmask from 255. example: 192.168.32.0/21 (255.255.248.0)
255.255.255.255
-255.255.248.0
---------------
0 . 0 . 7 . 255
the network range is from 192.168.32.0 to 192.168.39.255 (32 + 7)
every 0 is your ipBase here 192.168. the rest is done with for loops. so for all possible networks you need up to 4 for loops. one for every octet of an ip address.
maybe you can use an existing class for calculating subnets but i don't know such a class