TCP/IP Socket Programming Static Web IP between Dynamic IP - sockets

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.

Related

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

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.

Connect to server via a local network

When I launch a server in my pc he is listening any ip and a port 105. If a client on another computer wants connect to the server in my computer he is using local ip address like 192.168.1.101.
If a client will use my local ip address (192.168.1.101:105) to send a request, then my server will be able to receive it?
So long as there is a network route between the two computers and no firewall getting in the way: yes.

How Can I ping or create a scoket connection with my friend?

When we ping to any Public IP of a router it give a reply,
we dont get reply, if we ping a local computer with its private IP of a particular network,
So is there a way so I can make direct connection or socket with a particular PC in a network.
I have heard that, I have to make connection with public IP of a particular network and the port number will decide, on which PC in that network , my request will go.
But this method is not working..
If the computer is behind a router that uses Network Address Translation (NAT) - that is, the computer has a private IP that is not visible to the internet - then you cannot ping or establish a connection to the computer from the outside. This is because the router does not know which computer you want since all the computers behind the router share the same public IP address.
The way to establish a connection in this case is to have the computer which is behind the router establish the connection to the outside computer. For example, when you go to a web site, you can do it from a computer behind a router since your computer is initiating the connection. However, the web server you are going to must have a public IP.
When a computer behind the router initiates a connection, the router does select a port number to associate with that connection so packets from the outside for that connection will go to the right computer. However, this only works for traffic on that particular connection; the port number is not a general purpose mapping to that computer.
Two computers behind the same router can establish connections with each other using the private IPs as they don't have to go through the router to do it.
You can decide with your friend who is going to be the client (the one initiation the TCP connection) and who is the server (the one receiving the TCP connection) and which port should be used (let's say X). Let's say that your friend is the server. Then he/she has to configure his/her router to redirect all the traffic to the router to port X to his/her private IP to port X (this is what Warren mentioned as NAT).
If you use a port above 1024, the server (running on your friend's PC) doesn't need root/administrator privileges.
Your program (the client) would then connect to the public IP address of your friend. He/she can check his/her public IP with: What is my IP

Can I do strict socket bind for a client ip address at server side?

I need to accept only connections from particular client ip address at server side. Should not use acl. With help of socket strict bind at server side can i do?
Example:
client ip address: 1.1.1.1
server ip address: 1.1.1.2
At server side:
1. Open a socket
2. Bind socket with 1.1.1.1(client ip address) with port no.
Will i be allow to do the second step at server side? Any special options are there to do?
Please let me know.
Thanks,
Boobesh
You can only bind the server port to an ip address to specify the interface to use.
For example your server has two network interfaces, one connected to the internet and one to a configuration network. The webserver should maybe only listen on the internet interface and a management tool only listen on the configuration network.
For your purpose you can accept the connection, compare the ip address and if it is not in the list of allowed clients close the connection immediately (or after sending an error message).
The other solution would be to use a firewall that is configured to allow only connections from the specified clients to the server port.
I agree with the friend above, u can only manage the ip and port in you server, but not client. u should compare the coming socket with the one u store in your 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.