Socket connection = new Socket("192.168.1.200", 6000); // 6000 is the standard port for the device to which i am connecting
InputStream is = connection.getInputStream();
Socket connection as implemented above does not work on LGP500(Android) but works fine on HTC Desire(Android). Could someone tell me what could be wrong...And the weird part is that it used to work fine in LG P500 before, stopped working all of a sudden...tried doing a factory reset..but it was no use still...
I get a SocketException: Network unreachable....
Would be really happy if someone could help me out on this..
That IP address is for a local subnet (probably WiFi) so make 100% sure the phone is using WiFi and not 3G for networking.
Related
I know similar questions have been asked but this one is slightly different.
We have an app that does some HTTP connectivity with our server.
While running the app on an LG Nexus 5X or Sumsung Galaxy 5, using any of our 3 WiFi routers, the connection times out ("SocketTimeoutException: timeout" due to "SocketException: Socket closed").
The connection doesn't timeout if we use cellular network, tethering between devices or the simulator on a laptop connected to the same (problematic) WiFi routers.
Just to point out, the routers are connected to different ISPs.
Did anyone ever experience anything like it or have an idea?
Thanks
SOLVED: Trying a GET instead of a POST (with a JSON body of ~4K bytes) seemed to work fine. So, after a session with tcpdump, server side, it turned out that the request does reach the server but it's "corrupted". The first ~300 bytes and the last ~1000 bytes do reach the server but the middle ~2500 bytes are missing (could be due to some service provider infrastructure/ shaper or whatever).
In any case, lowering the buffer size of the OKHttpClient instance (providing it with a new SocketFactory) to 512, did the trick.
Thanks to all of those who tried to assist.
I have get UnknownhostException in parsing JSON data from server,
My URL working on :
http://jsonlint.com
on real device browser(data plan only)
Sometimes its working on PC browser and sometimes not.
Actually I have get the wifi issue, Sometimes its working on wifi connection, when its not, I have restarted my wifi and its worked, and after some time the same issue came,
I want to get the permanent solution for this wifi connection issue, Restarting the Wifi is never a solution on the application user side,
What is the exact problem and solution also...
Usually the UnknownHostException fires when you cannot resolve the DNS record of the URL you've provided. There's a reasonable timeout for that operation, but if you have a weak WiFi connection or you don't have enough signal on your device, the communication can be interrupted in the middle between sending and receiving the response, so your device doesn't receive the response, thus it thinks it's a DNS timeout.
There are 2 things you can try:
Increase the timeout of the response. This will not help, though, if your communication gets interrupted you already sent the query.
Use the IP address instead:
shut-up#i-kill-you:~$ ping jsonlint.com
PING jsonlint.com (54.243.171.164) 56(84) bytes of data.
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
System.out.println("Link Speed is======"+linkSpeed);
It will give u wifi speed so just mention one speed if that speed come then only do next work
i'm developing a small example about socket connection between android and ios (via wifi), after trying, the connection hasn't been established. Here is what I have done so far, I created a server on ios (used Bonjour to publish the service). I also created a client on android. However, after starting the server on ios, I also got the log:
ServerSocketConnection[3487:c07] Bonjour Service Published: domain(local.) type(_serversocket._tcp.) name(Macmini) port(54065)
Which means the server starting ok.
To the client part(android), I created the client through Socket class, some few lines of code:
Socket s = new Socket("local.", 54065);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println("Hello Android!");
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String st = input.readLine();
Putting it into AsyncTask to execute. However, I got the UnknowHostException:
08-06 12:45:44.460: W/System.err(873): java.net.UnknownHostException: Unable to resolve host "local.": No address associated with hostname
I'm a newbie to this kind of problem so any ideas what the problem is? I know it's related to the "host" thing but need the way to fix it.
*Note: I run 2 apps on 2 simulators (ios and android) as the same wifi network and same MAC, maybe this is the problem? any help would be appreciated and sorry for my English, it's not my native one.
Use the actual IP address of the iPhone instead of 'local.'.
On Android you can find a phone's IP via Settings -> WiFi -> Advanced. Not sure if iPhone offers the same option.
ps. Also be sure to have the internet permission in your manifest;
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I want to connect my cell-phone to PC by using socket connection.
But it's only successful when I use IP Address. I try to use Computer Name but it throws UnKnowHostException.
mySocket = new Socket("192.168.1.100", 10000); //it's ok!
mySocket = new Socket("My_PC_NAME", 10000); //it throw UnKnowHostException !
My cell phone is using WIFI and its IP is : 192.168.1.99
Please help me,
Thanks !
This is quite normal as your android device hasn't access to the DNS that assigns names to IP addresses. Since it isn't part of your PC network, you can't use computer names for the socket connection.
I'm trying to listen on a port using ServerSocket on an Android device. I want to be able to connect to this port over WiFi using a computer on the same network.
I get no exception when binding it to a port, however when I check netstat it says:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 (null):4040 (null):* LISTEN
I've tried countless ways of binding it to localhost, 0.0.0.0, the WiFi LAN IP address of the device with SocketInetAddress and InetAddress.getByName. Nothing seems to work.
When I try to connect to the port from a computer in the same WiFi (I've tried both netcat and Java's Socket.connect()), all I can see in Wireshark is an ARP request:
Who has [phone's LAN address]? Tell [computer LAN address].
This request repeat itself until timed out.
I've tried the reverse way, by setting the ServerSocket on the computer and connecting to that port from the phone, that works very well.
My testing phone is an Samsung Spica i5700 with a custom ROM.
Any ideas?
Edit:
The code is simple as this:
ServerSocket server = new ServerSocket();
server.setReuseAddr(true);
server.setTimeout(0);
server.bind(new InetSocketAddress(4040));
Socket client = null;
while((client = server.accept()) == null);
// Connected
enter code here
enter code here
Instead of using server.bind, try initializing the server socket like this:
server = new ServerSocket(4040);
Also, server.accept() will actually block until a connection is made, so you don't need that while loop (see: http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept() )
I struggled with this too and was only able to connect to my Android server by using:
ServerSocket myServerSocket = new ServerSocket();
String hostname = getLocalIpAddress();
myServerSocket.bind(new InetSocketAddress(hostname, myPort));
Where hostname was the local IP, which I got using the getLocalIpAddress() function from this page:
https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/MainActivity.java
I was able to get this working by using
ServerSocket server = new ServerSocket( myTcpPort, 0, addr );
where addr = InetAddress of your phone. Otherwise, it only seems to bind to localhost (127.0.0.1). Also, I'm using port 8080.