-1

I'm developing new software and I need to be communicate those apps amongst themselves like TeamViewer and Skype. For this challenge, I'm read too many article and write code but I cannot do that. I must use TCP, it cannot be UDP. Data safety is very important for my app.

http://www.codeproject.com/Articles/807861/Open-NAT-A-NAT-Traversal-library-for-NET-and-Mono

I found N(etwork)A(address)T(ranslation) can solve my problem and I use it. I write same code to what they say, but still doesn't work...

LISTENER CLIENT CODES

NatDiscoverer discoverer = new NatDiscoverer();
CancellationTokenSource cts = new CancellationTokenSource(5000);
NatDevice device = await discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts);

// display the NAT's IP address
Console.WriteLine("The external IP Address is: {0} ", await device.GetExternalIPAsync());

// create a new mapping in the router [external_ip:1702 -> host_machine:1602]
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 1602, 1702, "For testing"));

IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 1602);
socket = new Socket(endPoint.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
socket.Bind(endPoint);
socket.Listen(4);

REQUEST SENDER CLIENT CODES

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("100.96.106.192"), 1602);
                sock.Connect(ipep);

They are 2 client and I want to conenct them like that. "100.96.106.192" is computer Public IP (not a router(external) IP)..

For this challenge, I can use other languages or other libraries too except WCF because my app need to work platform independently.

For example, I start to Listener Client and into 'cmd', I write 'netstat -a' and I saw, it's listen to 0.0.0.0:1602. But Why?

Please help me and guide me. What's wrong my code? What am I doing wrong? How can I solve this problem?

Thanks for all answers and helps. Have a good day, good works...

1 Answers1

0

It's listening on 0.0.0.0 because you've passed IpAddress.Any to your listener, which listens on "all IPv4 interfaces". This is supposed to show up as 0.0.0.0 in netstat.

As per Microsoft:

Provides an IP address that indicates that the server must listen for client activity on all network interfaces. This field is read-only.

The Socket.Bind method uses the Any field to indicate that a Socket instance must listen for client activity on all network interfaces. The Any field is equivalent to 0.0.0.0 in dotted-quad notation.

https://msdn.microsoft.com/en-us/library/system.net.ipaddress.any%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Community
  • 1
  • 1
pay
  • 366
  • 3
  • 18