I am trying to trace a request originating from android and send some data to serversocket at each hop. I am using below code to execute client and send data to ServerSocket at each hop (tomcats having servlet projects) and they are able to send and connect.
However when I try to use same below code in android app running on emulator on same machine. It throws me timeout error.
InetSocketAddress hostAddress = new InetSocketAddress(hostAdress,
SocketServerPORT);
SocketChannel client = SocketChannel.open(hostAddress);
after open execution gives me
java.net.ConnectException: failed to connect to /192.168.0.102 (port 8090): connect failed: ETIMEDOUT (Connection timed out)
01-24 13:25:58.140 3382-3424/app W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
01-24 13:25:58.140 3382-3424/app W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:110)
01-24 13:25:58.140 3382-3424/app W/System.err: .....
what could possibly be wrong here?
my app has
<uses-permission android:name="android.permission.INTERNET" />.
I have also used
Socket socket = new Socket(hostaddress,port)
It throws same exception.
Related
I'm trying to implement a client application for Android that connects to a server using Socket, exchanges some data, and then terminates the connection in a graceful way, as described in this article:
https://docs.oracle.com/javase/8/docs/technotes/guides/net/articles/connection_release.html
However, the behavior of the client socket during disconnection does not behave as expected.
We have established a connection between Android client and server.
Client finishes sending all data and calls socket.shutdownOutput() (sends FIN to server).
Server receives the last bytes and calls its socket.shutdownOutput() (sends FIN to client).
Client reads the last bytes from the server and the last call to socket.getInputStream().read(...) throws the following exception:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) with cause: android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
I expect the last call to socket.getInputStream().read(...) should return -1, which is the behavior I get when I run the same code on OpenJDK 11 on Ubuntu.
Is there some setting of Socket which could fix the behavior on Android?
My app is working fine in AVD but when i try to run my app in my mobile it is showing this error. It is not connecting to server. It is not accessing php file. I have given path for php file is "http://10.0.0.72/myapp/search.php". I'm using wamp server and kept my php file inside www folder.
Logcat Error
java.net.ConnectException: failed to connect to /10.0.0.72 (port 80):
connect failed: ETIMEDOUT (Connection timed out) 06-13 13:19:20.229
13823-14092/com.myapp W/System.err: at
libcore.io.IoBridge.connect(IoBridge.java:114)
Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
I assume that you run your wamp server in local PC. Connect both your phone and PC (which has the wamp server) to the same network and replace 10.0.0.72 with the private IP of the PC.
Try the url in the phone browser, It should be accessible.
Make sure you have given internet permission in the manifest file.
I have a problem with android sockets. I am connecting a USB dongle on my PC to an Android phone, a Samsung Galaxy Note II. I am using standard bluetooth sockets creating the socket with createRfcommSocketToServiceRecord.
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
The server is listening with listenUsingRfcommWithServiceRecord.
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,MY_UUID);
The pairing happens fine between the client and the server. I have tried with the PC being the server and and the phone being the client and the other way around. In both cases I get the same exception. The logcat is pasted below.
04-12 06:14:58.229: D/THINBTCLIENT(2613): ON CREATE got remote device6C:F3:73:47:5B:39
04-12 06:14:58.229: D/THINBTCLIENT(2613): ON CREATE Socket created.
04-12 06:15:01.229: E/THINBTCLIENT(2613): socket connect threw an exception.
04-12 06:15:01.229: E/THINBTCLIENT(2613): java.io.IOException: Connection refused
04-12 06:15:01.229: E/THINBTCLIENT(2613): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
04-12 06:15:01.229: E/THINBTCLIENT(2613): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
04-12 06:15:01.229: E/THINBTCLIENT(2613): at com.example.myfirstapp.taskScreen$7.onClick(taskScreen.java:245)
I googled and someone said that unpairing and pairing again helps. I tried that too and still the exception persists. If anyone has any idea, it will be really appreciated.
I used apache http client lib in my program. I set both connection timeout and socket timeout for network calls. My understanding is that, if there is connection timeout, the client didn't reach the backend server; if there is socket timeout, the client reached the backend server but the server was not responding or very slow.
I log the following exception trace stack. How can I tell which timeout (connection or socket) caused the exception? Thanks!
java.net.SocketException: The operation timed out
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(NativeMethod)
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:130)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:247)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:534)
at java.net.Socket.connect(Socket.java:1056)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
From the stack trace, you can tell that it timed out while attempting to establish the connection, not while trying to read from the Socket's InputStream.
I should be able to successfully send and receive file to/from FTP server.
But then, no changes occurred in the code and I started getting this :
Error: java.net.ConnectException: Connection timed out: connect
What I am doing is:
FTPClient ftp = new FTPClient();
ftp.connect( IPADDRESS of FTP server);
connect() is giving this execption. I am not understanding the cause for it.
The error message is telling you that the OS's attempt to connect to the server timed out. This typically means that:
the remote server has dropped off the network, or
something (e.g. a firewall) is "black holing" packets sent to the server on the FTP port.
Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port).
Source: JavaDoc.