How to set up a client/server connection using port forwarding - sockets

I created a multi-threaded client/server application that can send messages to each other at real time. Everything works perfectly, but I want to be able to send messages over the Internet. From what I understand, I need to do port forwarding to be able to make my server reachable for the clients. I then set up my port forwarding options by providing a port (9991) and then my Macbook Air's IP Address (192.168.0.1).
I then tried to connect to my server using my public server IP (let's say 197.132.20.222) and it didn't work. I then tried to see if the port forwarding worked by using this website: https://www.yougetsignal.com/tools/open-ports/ and I realized that the connection was closed. I also tried the command nc -vz 197.132.20.222 9991 while running my application and the connection is refused.
I'm using a JavaFX application, and for my server side I use a ServerSocket with port 9991. For the client side, I use a Socket and set the IP Address to my public router IP Address, and I tried to connect with another PC using mobile data to use a different network.
My firewall settings are turn off, so I really don't know what is blocking my application to connect to that port. Could it be my ISP is blocking connections? I just don't understand why my ports are blocked even with no firewalls enabled.

Related

Client and Server connection with different port numbers?

I have set up a client and a server using sockets in python where my client sends data to the server, servers performs an operation, then returns some data to the client. Originally both the client and server were to have the same port number (9999). My issue currently is that I have to change the port of the server to 19999, and when I try to run, it does not work. The client is able to send data if its port is also changed to 19999, but it does not work if client is 9999 and server is 19999, which is what I need. New to networking systems so would appreciate any useful links to information or advice.
pic of client (left) and server (right)
Normally, clients will use an ephemeral port for its local port, and connect to the server port. Your client code is attempting to connect to port 9999, which is not the server port, which explains why it is not working. You need to connect to port 19999, since that is the port the server is listening on.
Using an unbound socket causes the client to choose an ephemeral port for its local address when making a connection. If you want the client to bind to a specific port, use bind before you call connect.
# bind locally to 9999
local_addr = (host, 9999)
s.bind(local_addr)
# connect to 19999
remote_addr = (host, 19999)
s.connect(remote_addr)

Explain SSH tunneling process and limitations (for a remote Xdebug session)

The Preamble
I start up my local SSH terminal at work behind a firewall, and connect to a remote server all the time without any problem.
The way Xdebug works, correct me if I'm wrong, is that it sends an "unsolicited" request to my network's port 9000. I actually initiated that action by sending the remote server an HTTP request through my browser with a POST/GET/COOKIE variable instructing xdebug to start up. But my network doesn't know that. All it knows is that it is getting a request on port 9000 from the internet. It doesn't know which computer in its private network to forward it to (without setting up port forwarding on the router), and can only ignore the request.
So if you can't do port forwarding, another option (and a much better one from what I can tell), is SSH tunneling. My computer sends the SSH request, the server responds. My router knows which computer in its network to route these responses to. Piggybacking on that SSH connection allows those "unsolicited" port 9000 requests from the remote server to get to me.
I think I understand that much.
I finally got tunneling to work, thanks to stackoverflow, but how it works is still fuzzy to me.
On the remote server, I tell Xdebug to connect to localhost (not to my ip via xdebug.remote_host=173.123.45.56, and not to xdebug.remote_connect_back=1 which also would end up at my IP) on port 9000. Connecting to localhost seems a bit weird, since I picture that as the server sending messages to its own IP address, as if it is sending messages into itself (but I think that connecting to localhost is probably fundamentally different than connecting to any other IP... I don't think the message gets routed out and back in to localhost).
On my computer at work, I open up an SSH connection on port 22, specifying a tunnel to/on port 9000, and remote port 9000. I've seen some explanations of the various settings here but still don't understand them. Some even seem to involve three machines. What seems to be happening though, is I'm connected as usual via port 22, but I've told the remote machine that I want to receive its port 9000 communications. I've specified "localhost" in my tunnel, and I suppose that might need to match the localhost in my xdebug.remote_host value. I wonder if I specified my IP address in both places (i.e. xdebug.remote_host=173.123.45.56 on the remote server, and same IP in my SSH terminal), would that work too?
So Xdebug on the remote server sends me a request to initiate a debug session. It comes through my port 22, but my SSH tunnel somehow makes it seem that it is coming in on port 9000. So my IDE that is listening on port 9000 receives the request and sends a response (also on 9000), which my SSH tunnel intercepts somehow and sends back to the remote server on port 22, where it is similarly spoofed into looking like port 9000 to xdebug.
The Crux
So what I'm really not clear on is, what exactly is the localhost in my SSH tunnel configuration referring to? Does it relate directly to the xdebug.remote_host=localhost value? Can I change them both to my IP address?
Are all of the remote server's outgoing communications on port 9000 being forwarded to me, or just some of them? E.g., if someone in Chattanooga initiates a debug session in their browser, will I receive Xdebug's response?
Are all of my outgoing communications on port 9000 being forwarded to that server? I.e. can I debug two applications on two different servers at the same time, with some of my port 9000 communications going one way and some the other, or would I need one port per local application? (I can use Google Chrome and Firefox browsers at the same time, both on port 80, for example.)
The tunnel consists of an SSHD listening to port 9000 (as well as 22) at your end and an SSHD listening to port 22 at the other end. When you connect your XDebug to your local 9000, the SSHDs intercommunicate and the remote SSHD connects to port 9000 at the remote. Thereafter your local port 9000 behaves identically to the remote port 9000: all data written to either end appears at the other end.

TCP/IP Socket Programming Static Web IP between Dynamic IP

I am confused about TCP/IP Socket Programming. I know the internet protocols but in theory...
I am explaining my problem (what i need )
I have a server working on X.X.X.X IP Adress. And it always listens to Y PORT.
When i want to connect that server on MY PC, i have no problem because i wrote the ip adress (X.X.X.X) and the port (Y) and my PC connects..
When i connect to server server keeps my ip adress and my local ip adress.. After that connection is end.
Here is my problem starting...
As i sad my server knows my local PC informations. How can i connect to my local PC on server my web server and sent TCP or UDP packets ? I did port forwarding on my modem but i dont want it. When i did port forwarding there is no problem but i dont want it ...
Thanks for replies and sorry for my engislih if i have mistakes .
If a router/NAT sits between your server and an outside client, you MUST use port forwarding on the router. The outside client will NOT be connecting to your server's private LAN IP/Port directly, it will be connecting to the router's public WAN IP/Port instead. So the router needs to know to forward inbound packets to that IP/Port to the server's private LAN IP/Port.
A client running on your local PC is able to connect to the server because they are both on the same LAN side of the router, so the connection is direct and does not go through the router's WAN. That is not the case for clients that are on the WAN side of the router.

Connecting to TCP server running in a machine connected to private home LAN

I like to connect to a TCP server that run in a machine that is connected to the LAN in my home network. This LAN can be connected to internet via either following methods.
1. Through a router which has a wireless or wired WAN connection
2. Through a router which uses a mobile broadband connection, for example a router that accept a USB modem to connect to internet.
I know in the first case, we can use port forwarding.
In the second case, I believe telcos use PPP protocol and port forwarding does not work.
In addition, even in the first case, if ISP does not give a static IP, then we need to use DNS mapping service to map a fixed URL to the allocated dynamic IP and we need to configure router to go and register the dynamic ip at the start up.
My question is, are there any other better methods that can be used in this situation? I am wondering how chat applications connect to each other? I hope they maintain a TCP connection between two devices, without a central relay server?

Connection between arduino(wifi shield) and PC locally, with router

I want to connect my PC-with-arduino (server client) with TCP/IP socket. In my PC i have written java code to make this happen. My questions are : I have to make port forwarding to my router? If yes, i will pass the port socket(client-server) into router? I have the zte zxv10 h108l router and i can't find any instructions about port forwarding for this model!
You say locally, which means you do not need any port forwarding. All your devices are on the same subnet, and you can directly open connections with each other.
Port forwarding would only be necessary if something outside you network wants to open a connection to something inside. Basically, if something on the WAN side of the router wants to open a connection to something on the LAN side. For example, you at a coffee shop on your laptop connecting to Arduino in your house.
Note that the reverse does not hold: something on the LAN side can open a connection to something on the WAN side with no port forwarding -- that is the NAT function of the router and is always active. For example, that is how your computer connects to stackoverflow with no special configuration on the router.