Can't connect to Android server using hostname - java

i'm trying to make a Java application that connects to an Android application.
Both my pc and my phone are connected to the same network.
This is the Java client wich runs on my pc:
client = new Socket("muffin", port);
System.out.println("Connected");
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
System.out.println("Streams ready");
And this is the Android application wich works as the server:
server = new ServerSocket(port);
socket = server.accept();
Log.i("Server", "Connected");
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
input = new ObjectInputStream(socket.getInputStream());
In the manifest i added the permissions:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
As you can see i'm trying to connect from my pc to the android server by using its hostname (i changed my android hostname to "muffin"), but it doesn't connect and it throws an exception:
java.net.UnknownHostException: muffin
If instead of the hostname i use the ip address, it works without problems.
It looks like it cannot find a device on LAN called "muffin", but you can see from this screenshot of my modem page that the name is right:
That said, i tryed to use the android application as the client and the java program as the server, but it looks like android has some problems because it didn't connect to my pc even by using the ip address instead of the hostname.
Do you have any idea on how to fix this problem?
Thanks in advance and sorry for my english, it's not my mother tongue.

I solved the problem, i've had to allow the traffic on the specific port through the windows firewall.
Now i can connect to the android application using its hostname.

You can't do:
new Socket("muffin", port);
There is a difference between Hostname, and Host, so:
java.net.UnknownHostException: muffin
means that the host "muffin" doesn't exist, and that's true: the only existing host is 192.168.1.105, who has a hostname who is "muffin".
So you should have done:
new Socket("192.168.1.105", port);
It is not possible to get a host from it's hostname : so if it is the only way for you to do it, you will have to do a huge scan of all the local network, and then see which host that is connected has the good hostname. As this method is much much harder, i realy recommand you to find another way to do it :)

Related

Java - TCP connection

I am working now on simple program to send file through TCP using Java. I have a problem that I am not able to connect between computers ( I am testing application using router and local IP adresses).
I start connection by:
sendSocket = new Socket(sendIp, port);
and I am trying to recieve connection on next PC by:
servsock = new ServerSocket(port);
recieveSocket = servsock.accept();
where
port is 12222,
sendIp is 169.254.5.47 ( second computer that recieves)
and myIP is 192.168.0.52 ( computer that sends)
What I am doing wrong?
I always use the same port, and I see on TCPView that java app uses that port.
Maybe I assign wrong IP, or my firewall blocks somehow.
Best regards and thanks,
Chris
If this is a Windows network, the IP 169.254.5.47 means the host did not obtain a valid IP address from the DHCP. In a standard local network both addresses should be on the same class C range (192.168.0.*).
You have to first solve this physical issue with your network and test it using ping (each host should be able to ping the other and see replies).

Connection to LocalHost/10.0.2.2 from Android Emulator timed out

Although this question has been asked multiple times in StackOverflow and I went through many of them, still I couldn't resolve my issue or I am not able to find out the root cause of the issue. Hence, posting a new question.
Below are the list of links I went through --
How to connect to my http://localhost web server from Android Emulator in Eclipse
Accessing localhost:port from Android emulator
How can I access my localhost from my Android device?
how to connect localhost in android emulator?
Here goes my code --
protected Void doInBackground(Void... params)
{
try
{
HttpURLConnection connection = null;
URL url = new URL("http://10.0.2.2:8080/android_connect/user_registration.php");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setConnectTimeout(720000000);
connection.setReadTimeout(72000000);
connection.connect();
OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
output.write(emp_details.toString());
output.flush();
output.close();
HttpResult = connection.getResponseCode();
connection.disconnect();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
I am trying to connect Android with MySQL database using PHP through WAMP server. The PHP file (user_registration.php) is being saved in the below path --
C:\wamp\www\android_connect
Now after executing the code, I am getting an error like "java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 8080) after 720000000ms: isConnected failed: ETIMEDOUT (Connection timed out)".
I went through the particular link in order to resolve this issue --
Android connection to localhost
But could not understand how it has been resolved! :)
Can you please help me in this case? Please note I am using Android Studio for my build.
Thanks in advance!
Making a connection from your Android to your Computer is working with 10.0.2.2 only on an Google Android Virtual Device. Android Virtual Devices are listening for 10.0.2.2 and forwarding all the requests to your computer.
Genymotion Android Virtual Devices are listening on 10.0.2.3 and forwarding those requests to your computer.
10.0.2.2 is not working with your real Android device. If you want to use it with your real device you have to set the IP of your computer, as it has been suggested by a previous answer.
I have figured out the reason why it was not working.
There were two issues --
The IP Address was not correct. So I changed the IP Address from 10.0.2.2 to the IPv4 address - which can be obtained on windows by typeing ipconfig in the command prompt and see link for linux.
Also the port number 8080 was not correct. I have set my own port number in httpd.conf file, like
##Listen 12.34.56.78:8383Listen 0.0.0.0:8383Listen [::0]:8383##, under Apache and I used the same.
After changing both and re-starting the WAMP server, it worked like a charm.
Why to go for localhost or any ip address to run on emulator or real device simply go for ngrok to convert the localhost as a global address with very convenient and simplest way.
Accepted answer to this question is really unclear so maybe this will help someone.
Just got the same problem and resolved it by executing "adb reverse tcp:8080 tcp:8080", although after I revoked forwarding, the host machine remained visible and all ports were working, so it is unclear how it works.
Make sure that
you have no proxy in wifi settings
if you have, then 10.0.2.2 is added to bypass without mask
that you didn't make a typo, sometimes people put 0 instead of 2 like this 10.0.0.2:8080
In my situation, I had this code running on my android device:
private static final String BACKEND_URL = "http://10.0.2.2:4242/"; // 10.0.2.2 is the Android emulator's alias to localhost
But my server code was running on my PC. So since my PC and my android device are "not the same device", that's why "http://10.0.2.2:4242/" didn't work. Solution was to change "http://10.0.2.2:4242/" to "https://<IP_OF_SERVER>:4242/"

Java Socket Bind Error

I have been having trouble about socket usage in java. First of all, let me explain what I want to do with sockets in java. I want to connect my laptop over the Internet via it. My laptop has a server and a client must connect over the internet. Because I have a router to handle my local network and I do not want to lead a port on the router to my laptop, I need to follow the path "internet->router->localNetwork->mylaptop". The problem is I have found a way to use both the internet ip address and the local ip address; However, it throws an exception : "Exception in thread "main" java.net.BindException: Address already in use"
The code I try is :
InetAddress addr = InetAddress.getByName("XXX.XXX.XXX.XXX");
InetAddress local = InetAddress.getByName("YYY.YYY.YYY.YYY");
Socket socket = new Socket(addr, 1111, local, 1111); // The line I have got exception
With leading router port to my laptop, I can run this code for the similar purposes:
Socket socket = new Socket("XXX.XXX.XXX.XXX", 1111);
*Xs stand for internet ip address
*Ys stand for local ip address
*Codes are belongs to client side of code
As far as I understand, you have a router with Internet (WAN) IP XXX.XXX.XXX.XXX with NAT, and you have a laptop with Local (LAN) IP YYY.YYY.YYY.YYY connected to the router, and you assume that
Socket socket = new Socket(InetAddress.getByName("XXX.XXX.XXX.XXX"), 1111, InetAddress.getByName("YYY.YYY.YYY.YYY"), 1111);
Will connect to the laptop. That is not correct.
Documentation of the constructor of Socket class you are using tells:
Creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.
That is not what you want.
You can't connect to a device behind NAT like this. You have to "lead a port on the router".
You don't need to specify the local address:port of the Socket, and you are doing so incorrectly. Remove the last two parameters.

UnknowHostException android client socket

I am getting the exception java.net.UnknowHostException:http://arbitrary-hero.dyndns.org/. I am attempting to connect to the address with a android client application I have made.
I have two computers one is running ubuntu 10.10 and the other is running windows 7. When I go to www.ipchicken.com on the windows 7 computer to check my ip I get 71.72.220.109 when I do a ifconfig from the command line on my linux machine I get 71.67.105.9. The 71.72.220.109 goes to my server application on the windows 7 computer the 71.67.105.9 and the address arbitrary-hero.dyndns.org goes to the apache server on my ubuntu 10.10 machine. The computers are in the same house using the same network and I dont understand why they have those different addresses. Also I am trying to get them to both use the URL.
String webserver = "71.67.105.9"; //does not work
String everythingelseinthehouse = "71.72.220.109"; //works
String weburl = "http://arbitrary-hero.dyndns.org/"; // does not work
Socket sock = new Socket (weburl , 13267);
//Socket sock = new Socket (address_everythingelse , 13267);
//Socket sock = new Socket (address_room , 13267);
This is where I declare my socket, sorry about the extra code but I have tried all possible combinations to make this work.
If you would like more code from me to help me solve this problem please ask I would be very happy to resolve this issue.
URL is not a host name, use InetAddress.getByName("something.dyndns.org") instead.
Other stuff about chickens is totally not clear in the question :)
Edit 0:
... when I do a ifconfig from the command line on my linux machine I get 71.67.105.9 ...
This tells me that your Linux box is either statically configured with this IP, or your router is setup to treat wired connections differently.
You need to add this premission to AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Also, you need to use InetAddress for using the domain name instead of the IP address:
Socket sock = new Socket(InetAddress.getByName(weburl) , 13267);

Unable to connect on socket across different networks

I am having trouble connecting my online application to others across another network. I am able to give them the hostAddress to connect when we are on the same network but when we are doing it across the internet the generated host address doesn't allow a connection, nor does using the ip address gotten from online sites such as whatismyip.com
My biggest issue isn't debugging this code, because it works over intra-network but The server doesn't see attempts when we try to move to different networks. Also, the test port I am using is 2222.
InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();
System.out.println("Hostname: " + hostname);
System.out.println("IP: " + addr.getHostAddress());
I display the host to the server when it is starting
if (isClient) {
System.out.println("Client Starting..");
clientSocket = new Socket(host, port_number);
} else {
System.out.println("Server Starting..");
echoServer = new ServerSocket(port_number);
clientSocket = echoServer.accept();
System.out.println("Warning, Incoming Game..");
}
If it works on your local lan but not across the internet then one or both peers are probably on a NAT'ed connection, meaning that the public IP address you see on the internet is not the same as the IP address of the machine you are trying to talk to. You would probably need to set up some kind of port forwarding to allow your app to connect.
The issue is probably firewall configuration.
Assuming you're testing this at home (it would usually be more complex from a university or company).
Usually you'll need to configure your router to let port 2222 open (you can also open port 5555 and tell your router to redirect to the host you want on your lan (there might be many), and port 2222).
To sum up:
other user ----> internet ----> [your modem] internet_IP -> [your router] lan_IP -> your computer lan_IP2
internet_IP is given by your ISP; find it here: http://www.whatismyip.com/
lan_IP: you defined in your router configuration. Typically: 192.168.0.1
lan_IP2: usually given to your PC by the router (DHCP). Find it by typing ipconfig (Windows) or ifconfig (Linux).
You need to tell your router to open port 2222, and route it to lan_IP2 on port 2222.
Configuring the router is usually done by connecting on its http interface: http://192.168.0.1
Some additional information might be helpful. Can you ping the machine from where you are? Are you attempting to go through a firewall? You say they work over localhost or local network - those are a bit different. Do you mean a network using local space (i.e. 10...* or 192.168.. or the like)? You say you are using a test ip of 2222 - that is not an ip address. Is that the domain of the address? Or is that the port number?

Categories