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);
Console.WriteLine("The external IP Address is: {0} ", await device.GetExternalIPAsync());
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?
EDIT:
I changed to LISTENER CLIENT Code with
IPEndPoint endPoint = new IPEndPoint(device.GetExternalIPAsync(), 1602);
But this time, it's thrown an exception on BIND() method:
The requested address is not valid
When I remove the BIND() method, it's thrown and exception on Listen(4) method:
An invalid argument was supplied
Thanks for all answers and helps. Have a good day, good works...
What I have tried:
I tried TCPListener, TCPClient, other network libraries etc. But I did not get the result what I want.