TCP Chat not working in different networks - c++

I coded a TCP/IP Chat Server/Client in C/C++ that works perfectly in my local network and with several clients connected to it. It does not work when the Server is in a different network to the client. I coded time ago the same tool in Python and the same happened.
Is there any way of solving this? Without port forwarding, that is the most common solution.
I could only find this: Android server concept confusion. How to connect TCP chat app on different networks but I could not understand it.
NOTE: I have just find a sample program (server) that works when it is on a different network; http://www.codeproject.com/Articles/1891/Beginning-Winsock-Programming-Simple-TCP-server

You have three choices:
1) NAT penetration. Both devices simultaneously attempt to exchange data with each other, tricking each person's router into thinking that it's replying. For this to work, you each need each other's public IP address and if you need any features from TCP, you need to implement them yourself.
2) Public rendezvous server. Both devices automatically connect to a server on an unNATed network. The server knows the public IP address of every client that connects to it, so it can route your data to the client with the correct public IP.
3) Some combination of 1 and 2. Here, a public server is used to facilitate NAT penetration and eliminate the need for manual coordination. Your friend registers with the public server and the public server tells him your public IP address and facilitates NAT penetration.

In local network addresses of devices doesn't change offently. Yo can reach the computer by only defining the local address. On the other hand, if you want to communicate with a device in different network yoou should know its public IP and should define a routing to the port.
If you have a server with static IP or known IP you can reach it. You can store the IP addresses of clients dynamically in this server. Write a program that inform the IP of host computer to the server. In that way, you can store the IP addresses even they change oftenly.

Related

How to get the IP that others can use to connect to my socket build in c++

My computer is using Wi-Fi provided by a tplink router.
I am using a website to get my IP, https://www.whatismyip.com.
My code is fine, because I can connect to the socket in my own computer by using 127.0.0.1.
Here is my code from https://www.geeksforgeeks.org/socket-programming-cc/amp/
But I don't know why my friend can't connect to my socket using the IP I get from the website.
I wonder, do I need to set up something in my router, or am I using the wrong code?
What you get from whatismyip.com is your public IP address. Your computer is probably behind a NAT/PAT (Network Address Translation / Port Address Translation), meaning your machine has a private IP address that gets translated by your router/firewall into the public one displayed by whatismyip.com
You have two solutions:
Get a public IP address from your Internet provider, and disable NAT on your router (probably you'll need help for this).
Configure port forwarding on your router so that it forwards traffic on your public IP address to a given port on your machine.

Second management interface/NIC/IP for ESXi 6.5

We have 3 ESXi servers that each have their public IP for manageability, however for the backups we need the servers to have an internal on a different NIC.
However, when we've added a new VMKernel network, the original (public IP) network won't connect anymore, resulting in the server being only reachable via the newly added LAN network.
Is there a solution we can use so the servers are reachable on both NICs/IPs ?
The 3 servers have these configuration for network:
Interface 1: Dell iDRAC
Interface 2: VMWare public management network (public)
Interface 3: VMWare private management network (10.0.0.1/24)
Interface 4-5: Double redundant uplink
Interface 6-7: LAN network trunked
You may use the same switch (with 2 uplinks and explicit LBFO settings for different port groups) or two different switches each using its own uplink - one for external and another for internal management network.
I think you can keep external management network setup as it is now (same vSwitch, same management port group, the same vmk0 adapter in default TCP/IP stack). This vmk0 adapter may have IP configuration like this:
IP: 192.168.5.5/24
GW: 192.168.5.1 - it may be defined for default TCP/IP stack or on vmk0 itself
For internal management network, just create another vSwitch, new management port group and new vmk1 adapter. Imagine you want to use internal management network like this:
IP: 10.5.5.5/24
GW: 10.5.5.1
Because we cannot have 2 gateways in default TCP/IP stack, you can define gateway directly on vmk1 (this is supported in ESXi 6.5):
esxcli network ip interface ipv4 set -g 10.5.5.1 -i vmk1 -t static -I 10.5.5.5 -N 255.255.255.0
Once you do this, I think both internal and external management networks should work for you. There may be some edge cases with routing where this scheme may not work, but I think for your use-case it should be fine.
In general there is not a problem with having two or more management interfaces. You should to give us some more information about network configuration. Did you change default gateway in host configuration? Remember that you may have only one default gateway and if you have changed it to correct for LAN then packets get by public interface not know how to return.
If this is the problem you should set default gateway properly for public interface. But you also need to connect from LAN. If machines in LAN are in the same network segment - it should just work. If machines are in other LAN - add entry to routing table, like described here: https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2001426

C++ P2P Listen on a port without port forwarding

I'm trying to write a C++ chat program that is based on Peer To Peer technique, with no need to a server. Say peers connect to each other using their IP addresses as identifier.
Can I listen to incoming connections without configuring port forwarding on the router?
It is possible if peers are not behind a NAT. If they are you have to make port mapping(forward ports). You can easily write a function to check if a peers machine is behind NAT or not. And if it is, you can reconsider using server as a transfer place of the messages between peers.
Edit:
You can also think about using public VPN as a proxy(with port forward included). However, it is hard to find free one. Even if you are willing to pay for it, you have no assurance that no one will listen to it and you will be dependent of the uptime of the VPN servers.

Pass along client ip address

So I have a system using ZeroMQ as the message controller and because it doesn't allow you to find information about a clients address that is connecting or sending messages I need to pass in self identifying information containing port and ip address of the node. However I'm not sure the best way of doing this.
I'm stuck because if they connect using an internal ip address for the network I would obviously need to use that ip address but if its going over WAN then I need the public ip address. What is the best way of communicating the ip address of the machine without knowing whether or not you are connecting to a local machine or a machine over WAN

private vs public ports

I am having a tough time with private and public udp ports. I am doing a client-server VoIP program and have some questions.
1) the private port is the one you use in bind() right?
2) the public port is assigned by the firewall right? as it is the port visible outside my local network.
3) When I am debugging between two machines on my local network, I am specifying both to send/recv to the private port, and communication works. If I would be communicating with a client outside my network I would use the public port, right?
4) Is there any way for two hosts on the local network to communicate on the public ports? since that's what its going to be in release mode, it would be good to make sure it works.
5) will the router forward packets sent to the public port to the application listneing on the private port? so the sender (if outside the local network) specifies the public port and not the private port.
Hope that was clear, just ask otherwise!
Thanks in advance!
Johan
1) the private port is the one you use
in bind() right?
Right.
2) the public port is assigned by the
firewall right? as it is the port
visible outside my local network.
Well, yes, but it's not exactly the firewall. It's the NAT. Of course, NAT could be (and most often is) implemented in the firewall, but there are also firewalls that don't use any NAT. Another thing to note is that you may have multiple levels of NAT (like one at home and one at ISP), in this case it probably makes sense to refer to the port assigned by the outermost NAT as the public one.
3) When I am debugging between two
machines on my local network, I am
specifying both to send/recv to the
private port, and communication works.
If I would be communicating with a
client outside my network I would use
the public port, right?
That depends on the network setup. Since you mention "client-server" in your question, I assume that the client "connects" (sends the first packet) to the server. If the server isn't behind any NAT, then its public IP/port pair would be the same as the local one. But if the server is behind a NAT, then you can't just connect to it because it has no public port assigned yet. Just opening a port doesn't cause the NAT to assign a public port, you need to actually send something from that port.
So if your server is behind NAT, then it must act as a client, and the client must act as a server, provided that the client isn't behind NAT too. If both sides are behind NAT, then you'll need a third-party non-NATed server to perform hole punching. Note that when using hole punching, usually both private and public endpoints are used just in case that both sides happen to be in the same LAN by pure chance.
4) Is there any way for two hosts on
the local network to communicate on
the public ports? since that's what
its going to be in release mode, it
would be good to make sure it works.
That depends on the NAT setup. It could just ignore everything that comes from inside the LAN and has the NAT's public address as destination. For example, I can't even ping my own public address from my home PC.
5) will the router forward packets
sent to the public port to the
application listneing on the private
port? so the sender (if outside the
local network) specifies the public
port and not the private port.
See my answer to 3). Of course it will forward packets as soon as the public port is assigned, because that's what it assigns it for in the first place. But it will probably check that the incoming packet is coming from the same address and port that the packet that caused the port to be opened was sent to, so it's a valid response to a packet sent earlier, not just some random hacker trying to break in.