Java Server client and hos name computer - java

I made a simple java client server program its work very well on my computer (localhost) but when I run the server on my computer and the client on friend computer I don't know how to get my full hostname that the client need when I go to system information get the hostname from their and run the client he cant find this hostname, what I supposed to do thank you for your help

Sounds like what you want is port forwarding.
Login to your router by going to 192.168.1.1 on your web browser (if that doesn't work try 10.0.0.1)
Type in your username and password for the router, they're usually on a sticker on the side of the router.
Get your computer's local IP address by looking for your computer's name in the list of connected hosts on the router, or by following the instructions for your OS on https://kb.iu.edu/d/aapa
Go to the port forwarding section on your router's config page and add a new rule that forwards traffic from the port you specified in your program (like 5000 or something like that) to your local IP address (something like 192.168.1.12).
Get your public IP address from https://whatismyip.com
Make your client program try to connect to your public IP on the port you chose
Start your server on your computer that you port forwarded
You should then be able to tell your friend to start his client to connect to your server.

Related

Java run ServerSocket on PC

A common problem for a common project: I'm trying to run a ServerSocket Java program on my local PC and connect to it from an Android client. As others have experienced, things go smoothly when the client connects to the local address (eg. 192.168.xxx.xxx). If I try to connect through the internet, nothing happens:
Now, here are the things I tried:
Created inbound/outbound rules in Windows Firewall to allow traffic for the Server Port (3000)
Used the public IP in the client socket (IP obtained here)
Set up port forwarding on my router: inbound connection on port 3000 are redirected to the local IP of the server (eg. 192.1686.xxx.xxx again)
Check that the IP address of the server is listening to the port 3000 using "netstat -anp tcp".
Disabled any firewall on my router, just in case.
Still, there is no connection. Using this tool (https://www.canyouseeme.org/) I triedd checking that the port was visible (with the server app running of course), but nothing, all I get is a time out.
I'm out of options, I was hoping someone could show me what I'm missing.
Thanks.
(1) Does your PC where your server runs on have a public IP?
If you haven't applied for one and are running your code in a home or working environment, your computer only has a private IP. And considering the fact, your server and client connect well in the same LAN, the problem may be the IP.
(2)If you do have a public Ip, does your server listen on all IP address or just the LAN? Check the server bind IP is '0.0.0.0', and in ServerSocket constructor you can just set it to be null.

Socket TIMEOUT when phone is on 3G, and works when on Wifi- Why?

I'm programming a client-server app, my client being an android phone, my server being my laptop.
So my issue is that this one line of code:
Socket connectionToServerSocket = new Socket(hostName, portNumber);
works perfectly fine when my cellular phone connects to the Internet with my home wifi connection, and simply times out when connected through 3G (cellular provider) eg it blocks for a while then throws a timeout exception.
The funky thing is,that I can see (using OS Monitor) that some apps are connected through very common ports, for example port #80, but switching portNumber (as well as the port that the server is listening to) to 80 doesn't help (eg it still times out), and I've tried many different ports-same result.
DNS works fine (eg it translates the logical String which I gave hostName to the correct IP) but it doesn't send the server anything...
I'm lost,what could be the reason? How can I check and resolve it?
I've run into this issue as well doing a similar application.
Your laptop and phone can connect to each other while on the same network because they share a IP address lookup through your router.
When the device is connected to the WIFI, it's request get passed through the router to check for IP addresses, it will find your laptops IP and save a request to a DNS because it can find the laptops IP already. The laptop works the same way, it finds the Ip address of the client through the router as well.
However, when your phone is on 3G, it has no way of knowing exactly where your laptop's IP address is. That's why it times out: it goes from your router to your nearest DNS (where it tries to resolve the correct IP Address), if it cannot find a domain or IP that matches it will fail.
Some steps to fix this . . .
Depending on your router you can set up port forwarding for your laptop's IP. This means incoming requests to your router can be piped to your laptop's server implementation.
Then go to any site like this http://touch.whatsmyip.org/ on your laptop to get your laptops IP. Save this to add to your clients Socket set up.
For debugging until your laptop server is visible for DNS lookup, go into your client code and add this.
Socket debugSocket = new Socket("the.laptop.ip", 80);
Some warnings:
Depending on your Router, your ip may change during restarts
With port-forwarding any browser with your ip, ex) 178.12.434.01 can log onto your laptops personal server
Future Changes:
Once a dedicated server is up and running, registered with a domain you can change the above ip parameter to "your.domain.com", and behind the scenes the actual IP address to your server will be found via DNS lookup.
the reason for that is that the server in your laptop is closed to external network by default, what you need to do is something called port forwarding
*take note: port forwarding put your server in a cyber security risk, make sure you make the right adjustments to keep your server safe.
case 1: It is working when your laptop and your phone is connected to wifi right ?
Try this once
case 2 :
connect your phone to 3G.
enable hotspot on phone.
connect your laptop to your phone's hotspot.
check the IP of your laptop if it is changed replace that in Socket object. Socket connectionToServerSocket = new Socket(newIP, portNumber);
Run your project.
Just try this once you will get what i am trying to say.
You are getting the timeout exception because your server that is
having the service is not reachable from the external network.
Hope this will help :)

Make private computer's IP accessible by server using Java

To say in other words: My computer has an IP address 1.1.1.1 and the server has an IP address 2.2.2.2. My computer can access the server's IP 2.2.2.2 and work with services running on its ports (Apache, SOCKS proxy etc.). But the server can't do the same with my computer - my ISP doesn't allow it - it means when I run an Apache service on my computer on port 80, the server isn't able to call 1.1.1.1:80 (the call won't pass ISP's firewalls and reach my computer).
So, is there a way (but not contacting the ISP) to allow the server to call my IP address with successful response?
I will need some Java code to do that.
I mean creating some sort of "tunnel" which my computer opens to the server and makes the computer's IP accessible through the tunnel.

Connect via TCP and public IP without forwarding

I'm trying to write a simple chat program using TCP in java.
To connect to a server I need to know its IP address. I'm connected to a router in my network, that connects me to the Internet.
When I type local IP (assigned by router) it works pretty well between my two computers.
But when I typed public IP (I got it from google: "what is your ip"...) - it didn't work.
So I opened router's setup and make forwarding rule from my public IP and specific port to the local IP - and it worked.
--------- Question:
But how to do this without forwarding? I want to write a chat program. I can't tell my users: "just do the port forwarding" ;)
One opens my program and logs into central server, then writes there public IP. Some other user opens this server, download the file and gets first user's IP (or any other needed info).
But if first user didn't do the port forwarding, it won't work. How to make it work?
For this you will need to understand how something called NAT (network address translation) works. In simple terms the NAT is responsible for sending packets to the right computer on the internal network from the external.
Say for example you have computer A as a server on the internal network, and have computer B on the external. If you then try to connect to computer A from computer B, it will not work because NAT (your router) doesent know what computer to send that packet of data to, on the internal network.
Its diffrent when you want to connect to an external server. Lets say computer A (client) is on the external network, and computer B (server) is on the internal network with the router port forwarded to its IP-address. Then you will be able to connect to the server because NAT knows where to send the data packets.
So to keep all your users from port forwarding their routers:
Port forward your server on your internal network
Connect the clients on another network
For clients on the internal network; use the local IP of the server
Hope this helped!
-Kad

Java Socket Programming

Java Socket Program did not work for WAN
I have written a TCP IP socket program which works fine in my LAN.
One of my friend is at bangalore He ran the server and I ran the
client with the host name of my friend's IP. In this case my
socket program did not work.
You said that your program is attempting to connect to host 192.168.1.107 port 46216.
192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).
on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.
Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.
The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.
WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.
You could use an implementation of STUNT or other NAT Traversal protocol.
The ip of computer on thih u deployed your server program is not in your reach.
192.x.x.x ip means (class C) it is for local subnet.
You need to have change your ip address of your net-adapter so that your router could route it through internet.

Categories