Public ip socket networking - java

I have a server object and client object that can connect and communicate with each other.
The client connects using the servers private IP address.
But I would like to be able to use public IP addresses to be able to communicate over the internet.
I get the public ip address using a url www.whatsmyip.com as a url.
I paste this public ip address where needed, but the client is unable to seemingly find the server using the public IP address

It doesn't have anything to do with code, as long as the server is binding to INADDR_ANY. It has to do with correct port forwarding at the server-side firewall.

Related

How can I connect to my server in my own machine from a client in another pc?

Im making a client/server app just for learning purposes, and i want my Client to connect the server (which is running in my pc) from another pc.
Right now im trying to use the server's pc public ip (which I got with whatsmyip.org) in the Client socket: Socket sock = new Socket(HOST, PORT); where host is the public ip of the server's pc.
When I was testing it with localhost and the client running in the same pc, the connection worked.
I already oppened the port im using in the Server pc and added firewall rules, including router's firewall. So any of my firewalls should be blocking the connection to the port im using.
I read in another question this (it was an example): "Your Server PC is in New York with an IP 192.168.1.121, that is behind a router with internal IP 192.168.1.1 and public IP 40.20.26.63. You will need to make sure that you go into the router and forward port 8084 (TCP) to internal address 192.168.1.121 (the internal Server PC)." How can I do that? I know the public IP and the internal IP of the server, but I dont know what IP I need to type in the Client Socket.
Thanks in advance.

DatagramSocket with a public ip address

hello everyone i'm making an application that uses DatagramSocket to transmit data between Pc and android Phone it works when i use a local ip Address but when i use ddns from no-ip or my public ip address it didn't i work i get this error message
DatagramSocket serverSocket = new DatagramSocket(port,InetAddress.getByName("XXXXX.ddns.net"));
i got this error message :
java.net.BindException: Cannot assign requested address: Cannot bind
plz help me how can i establish a connection between two machines using no-ip ddns instead of my local ip address
You can only bind an addresses that are configured on network interfaces on the device you use. I don't know your home network setup but most likely your public IP address is configured on the router/modem you use to connect to the internet so this simply will not work.
Your router may have port forwarding feature where it sends packets sent to a specific port to a specific IP address in your home network. You would have to configure a static IP address on the device and set up port forwarding to that address.

connecting to a remote java application

I have to build a server/client chat room in java as a school project, and I want to know if I can connect to that server from the world network (not local network) using the IP address and ports (I wanted to host it but I realized too late that I should have built a web app not a desktop one). My app is using Transmission Content Protocol(TCP) sockets. I have tried to connect to the app by using the public IP and the port that I have opened in firewall. I can post the code if needed. Thanks in advance
If your networks firewall settings let you do it, of course you can connect.
Now, your computer has an internal ip address, which is like 192.168.xxx.xxx and your router has an external ip address which is unique.
Set up your router to forward connections. Steps to do it may change for each router but the point is to forward all connections coming to this router with a port number you have declared, to a internal ip address and a port number.
In your client side, your connection statement will have your external ip adress and the port number you have written in your router settings. In your server side, it will listen your internal ip address and the port number your router forwards to.
To be clear;
Client ---"xx.xx.xx.xx:9999"---> Router ---"192.168.xx.xx:8888"---> Server
As you see above, the router forwards all the connections coming to 9999. port, to 192.168.xx.xx address and 8888. port.

Getting the hostname of an SSL client behind a NAT gateway or proxy using java

I have a 2 way SSL client and server written in java. Problem is my server needs to be able to find out the hostname of the client and that client is located behind a NAT gateway or proxy. I have tried:
clientSocket.getRemoteSocketAddress(),
clientSocket.getInetAddress().getCanonicalHostName(),
clientSocket.getInetAddress().getHostAddress(),
whereby clientSocket is of type SSLSocket and none of them works, they just give me a public IP address. Is there a way for me to obtain the actual hostname of the client that is connecting to the server?
Not from the Socket. If the remote host is behind a NAT firewall then by definition its hostname and IP address are invisible to you. All you can see is the public IP and hostname.
However, you could execute a hostname command at the remote side and pipe the output back over the SSh connection. The data you get back will be only as good as the remote host's configuration. There's no guarantee that what you get back matches its internal DNS hostname or anything else.

How do I get a public IP address of a mobile phone?

I want to connect my phone to a server using the internet using the server's IP address. Mobile phone's IP address are normally private as far as I came to know.. To make my app work, I need a public Ip address of my phone. How do I achieve that??
Thanks
You'll have to wait for IPv6 ;) Now you get private address and the your service provider does some address translation (NAT): The router has one single public IP and uses ports to map incoming messages to IP addresses of the private network.
Your mobile's IP is invisible to the internet and the networks public IP is useless, because it is the router's address. Maybe with IPv6 we have public IPs for all mobiles, but for now you'll have to find a different solution for your problem - public IP doesn't work with mobiles as with all other clients in private networks (behind routers).
Setup your own hosting server, make your app "goto" that server, fetch a simple txt page, in that simple txt you dynamically on the server set the address that was used to get the txt.
Example:
Server: yourexample.com
url for /ip.php
ip.php just respondes with mimetype text and the address of the requesting client.
J2ME:
Request yourexample/ip.php in the repsonse is the address of your app.
astonishingly - t-mobile 3g service seems to assign a public IP. I'm still looking into the details, but I've seen an internet-available website running on a tethered laptop
You should get your i/p address. Open your browser and enter http://whatismyipaddress.com/ or wait till you have You'll have to wait for IPv6

Categories

Resources