Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
C#
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
C#
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
C#
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.
Posted
Comments
Foothill 28-Jun-16 17:28pm    
I'm no expert with networking but I can add this. 0.0.0.0:1602 is port your app is listening on. For two way communication between computers, one usually acts as a server and the other a client. If you want two user PCs to talk, I would suggest setting up a server to act the intermediary between them.
[no name] 28-Jun-16 18:42pm    
If you use IPAddress.Any, it means 0.0.0.0. But if you want to listen to specific IP address, then use IPAddress.Parse("100.96.106.192") for your Listener. If I read it again, your computer has a public IP and why do you still need port forwarding ?
Umut Comlekcioglu 28-Jun-16 19:31pm    
When I change it with IPAddress.Parse("100.96.106.192"); this time it thrown an exception on Bind() method. If I remove at line of code, it's thrown an exception on Listen(4) method.. I written there that situation too..

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

I'm using that library and I don't need to be port forwarding. After NAT processes, it create and Public IP for my computer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900