1
0
Fork 0
mirror of https://github.com/rfc2822/GfxTablet synced 2025-10-04 10:09:16 +02:00
This commit is contained in:
wech71 2018-11-24 13:47:02 +01:00
parent c8d0ad49cf
commit 0890103556
2 changed files with 29 additions and 26 deletions

View file

@ -150,7 +150,7 @@ namespace GfxTabletWinDotnet
{ {
Listener.Instance.TabletEvent += Instance_TabletEvent; Listener.Instance.TabletEvent += Instance_TabletEvent;
Listener.Instance.Start(); Listener.Instance.Start();
this.txtIpAddress.Text = Listener.Instance.ListenAddress.ToString(); this.txtIpAddress.Text = Listener.Instance.listenAddress.ToString();
} }
} }
} }

View file

@ -44,41 +44,44 @@ namespace GfxTabletWinDotnet
{ {
public static readonly Listener Instance = new Listener(); public static readonly Listener Instance = new Listener();
public IPAddress ListenAddress { get; private set; }
public string ListenHostname { get; private set; }
public IPEndPoint ListeningIpEndPoint;
private Listener() private Listener()
{ {
this.ListenHostname = Dns.GetHostName();
IPHostEntry hostInfo = Dns.GetHostEntry(ListenHostname);
// Get the DNS IP addresses associated with the host.
IPAddress[] IPaddresses = hostInfo.AddressList;
ListeningIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
var nic = NetworkInterface.GetAllNetworkInterfaces().Where(
o => o.OperationalStatus == OperationalStatus.Up
&& o.GetIPProperties().GatewayAddresses.Count > 0).FirstOrDefault();
foreach (var ip in nic.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork) //only use ipv4
ListeningIpEndPoint = new IPEndPoint(ip.Address, 0);
} }
ListenAddress = ListeningIpEndPoint.Address; //public Socket socket;
} public IPAddress listenAddress;
public void Start() public void Start()
{ {
IPHostEntry hostInfo = Dns.GetHostEntry(Dns.GetHostName());
// Get the DNS IP addresses associated with the host.
IPAddress[] IPaddresses = hostInfo.AddressList;
//var hostEndPoint = new IPEndPoint(IPAddress.Any, Protocol.GFXTABLET_PORT);
//socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//socket.Bind(hostEndPoint);
//socket.Listen(0);
////socket.Listen(2);
//socket.BeginAccept(new AsyncCallback(NewConnection), socket);
UdpClient server = new UdpClient(Protocol.GFXTABLET_PORT); UdpClient server = new UdpClient(Protocol.GFXTABLET_PORT);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
var nic = NetworkInterface.GetAllNetworkInterfaces().Where(o => o.OperationalStatus == OperationalStatus.Up && o.GetIPProperties().GatewayAddresses.Count>0).FirstOrDefault();
foreach(var ip in nic.GetIPProperties().UnicastAddresses)
{
if(ip.Address.AddressFamily == AddressFamily.InterNetwork) //ignore ipv4
RemoteIpEndPoint = new IPEndPoint(ip.Address, 0);
}
listenAddress = RemoteIpEndPoint.Address;
var thread = new System.Threading.Thread(delegate () var thread = new System.Threading.Thread(delegate ()
{ {
while (true) while (true)
{ {
Byte[] receiveBytes = server.Receive(ref ListeningIpEndPoint); Byte[] receiveBytes = server.Receive(ref RemoteIpEndPoint);
Protocol.event_packet eventData = ByteArrayToStructure<Protocol.event_packet>(receiveBytes); Protocol.event_packet eventData = ByteArrayToStructure<Protocol.event_packet>(receiveBytes);
ntohs(ref eventData.x); ntohs(ref eventData.x);