I'm creating an android app. It requires a TCP socket connection to send data to the another device.
Everything works fine for the first time transfer. However when I connect it the second time, it says connection refused by the host or bind error.
I did all possible things to solve this bind error and connection refused. I shutdown both reading and writing end once the transfer is complete, set socket option to re use address, lingering option.
These things work fine when I exit the application completely and open it again but it does not work when I try to open the connection without exiting.
Kindly, help me figure out whether the problem is something related with sockets or with the application, and any ideas how this could be solved?
Look at the output of netstat -a after you close the socket on the server side and before you retry anything. If your ipaddress/port pair is still in any state other than, I think TIME_WAIT, you will not be able to reconnect.
The server side (the one that calls accept) is most likely the culprit, but I'd check both now that you see something wrong.
Related
After launching the application, it connection to the server. I am using Socket. TCP connection type. As long as the application is active on the screen, everything is fine. But if the application is moved to the background and the screen turns off, and after a while the connection with the server is lost, then it is impossible to create a connection again. The socket throws an exception:
"Unable to resolve host "m15.cloudmqtt.com": No address associated with hostname".
Where "m15.cloudmqtt.com" is the server name.
But if you take the phone out of the background and open the application, then the connection occurs immediately.
The creation of a socket is implemented using AsyncTask, which is launched from the Service.
Is it possible to solve this problem? I have been looking for various solutions for several days.
I'm trying to learn how to connect my app to a certain FTP server. For this purpose , Im using Apache Commons net which is quite good. So far I managed to connect my app to the FTP server while Im connected to the WiFi. While Im connected to the Wi-Fi, Its connecting and logging in smoothly, yet, when I try to connect my FTP server with my celluar network It gives me connection timeout error. First I thought this because of my cellular network speed. Therefore I increased the timeout for giving my cellular network much more time to connect but It didn't work. I mean It's basicly not working. Here is the code Im using. Its in a class which is extended by AsyncTask.
#Override
protected String doInBackground(String[] params) {
String temp = "Files : \n";
FTPClient client = new FTPClient();
client.setConnectTimeout(360 * 1000);
client.setDefaultTimeout(360 * 1000);
try {
Log.d("FtpDebug", "Connecting ...");
client.connect(myftpservername);
client.enterLocalPassiveMode();
Log.d("FtpDebug","Connected!");
}catch (Exception e){
Log.d("FtpError", e.toString());
}
I said its not working because it gives me this error
D/FtpError: java.net.SocketTimeoutException: failed to connect to xxx.xx.xxxx.xx.xx/xxx.xxx.xx.x (port 21) after 360000ms: isConnected failed: ETIMEDOUT (Connection timed out)
In this error it says It failed to connect after trying 360.000ms=6 minutes. But actually , Im getting this error in 1 minute or less than 1 minute.So Its not even waiting for 6 minutes.
It is clear that Im doing something wrong. I would be really happy if someone point that out. Thanks.
FTP is a protocol which uses a TCP connection to transfer the FTP commands (control connection), but for each data transfer it uses a new TCP connection (data connection). Information about the endpoint of the data connection (i.e. the port used) is exchanged inside the control connection.
This setup makes FTP a nightmare whenever firewalls or network address translations (NAT) are involved, which typically is the case when connecting from private networks but also from cellular networks. Sometimes it is possible to work around these problems by switching between passive and active mode, sometimes transparent helper applications are employed inside the network to deal with these problems and sometimes even these helper application will not help or will even make the situation worse.
You might try to play around with FTP active and passive mode, IPv4 vs. IPv6 and maybe you will be able to make the application work in this specific cellular network. But it might be still broken inside another network. I instead recommend that you spare you all this trouble and use a different protocol which has not the design issues, like HTTP/HTTPS or SFTP (but not FTPS which has the same problems).
After doing some research and tests I find out that this problem occurs because of FTP server settings. I was using my university's FTP server for this but most probably because of security issues, they decreased the timeout time significantly which was my problem from the beginning. I am now using my friend's FTP server for my testing.
Solution : There is no direct solution for that If you have no permission to change your FTP server's settings. You might want to try your another FTP servers for avoiding or narrowing your problem. But I would like to give a quick advice to newbies like me, If you are trying to do something like a message app or so, don't start doing this with FTP but use MySQL instead. FTP servers, as Steffen pointed out already, is really unreliable and might cause you many troubles when think you are ready after finishing your code.
The error happens sometimes, the error state may last for a while, and after that it will recover automatically.
the exception as below:
SSL handshakes failed,it seems that the server reset the connection,then I captured the packets at the sever end:
192.168.5.126 is server,192.168.111.216 is android client.we can see that client reset the connection first and then send "client hello" to server repeatedly, server reset the connection.
In the successfull case, packets as below:
So, why the client choose to reset the connection, what happens at that time?
Does the server not support TLS1? not exactly, sometimes OK.
Dose the network affected? always use wifi and can capture packets on the server end which indicates the network is ok.
Some bugs may exist in the implementation of HTTPS? we use the org.apache.http package to connect HTTPS server.
Then I google it, but found no complete solution.
Has anybody encountered the problem like this, or anybody can help? thanks a lot!
I really need help here...
I have a simple Android application that connects to my sever to fetch data over HTTPS.
Everything is working fine up until i connected to the same server from another device (iOS or Android). I am starting to get timeouts or connection refused or other errors depending on the library that I use (sometimes SSL handshake)
I tried 2 android app on the same router - fails
I tried Volley, Retrofit, plain http library - fails
if one device is connected via cellular network and one on wifi, it is working fine. However, i have seen cases that it fails even using 2 devices connected to cellular network and not wifi.
It is easy to reproduce. one app is working fine. as soon as i do an operation on the other device. the first app will not be able to connect.
iOS app using the same api/server is working fine. no failures
I ran wireshark on the android app during failure and received the following:
70 47.073286 10.0.0.1 10.0.0.138 ICMP 120 Destination unreachable (Port unreachable)
Seems like port issues. I am not sure anymore if this is a server issue or a client issue.. iOS app works fine. no issues. Only Android.
I tried:
System.setProperty("http.keepAlive", "false");
I tried setting an http header "connection :close" nothing works...
Any idea would be appreciated...
Same problem here . When my ios device connect to allstar heroes or fun run application the android device cant connect anymore . I cant find s fix for that . My router is tp link dir 615 . Maybe the problem is frpm router .
At the end of the day, it was server side issue. the IT guy gave me the following info: "incorrect flag on the tcp kernel settings" "Reuse connection" that is all I have for you. hope it can help someone else
I had the same problem. The issue seemed to be when the app tried to access ports in TIME_CLOSE here is a great explanation how this happen, changing the tcp kernel to Reuse connection, might solve the problem since the server will try to re-use those connection in TIME_CLOSE again. but it must be a solution from the client side to avoid the connection to get stuck. in my case I was trying to create connection from multiples activities and i guess they, somehow, competed for opening and closing the connections, i solved the issue using a single activity to make the connection to the server.
hope this is useful to someone.
Had exact the same issue and spent a lot of time investigating, nothing helped including "Reuse connection" flag, tried also to disable tcp_timestamp, tcp_tw_reuse, tcp_tw_recycle and enable vm safe mode for the app as suggested here:
https://github.com/square/okhttp/issues/903
https://github.com/square/okhttp/issues/1037
https://github.com/square/okhttp/issues/1518
but all in vein.
Further more, the very strange thing was that I had two different instances/servers which I made exactly the same in order to track down the issue and one server didn't have any issues.
So in the end:
Instance reboot solved problem with connections.
(or just need to restart networking service)
(c) My server/instance admin
I'm doing an application that will comunicate with a TCP server. I based my work on the following example http://myandroidsolutions.funcode.ro/2013/03/31/android-tcp-connection-enhanced/.
Right now I'm looking how to verify if there is a connection between Client and Server. The mBufferOut.checkError() only give an error after 2/3 tries. I need to know if my message was sent or not.
Thank you
In network communications, you are never sure when (or if) the remote party received the message in full, in part, or at all.
If the connection is broken, then you know for certain that message was not received in full.
But if you need a timely confirmation, the server should send some confirmation message. Your application protocol needs to support this, though.
The same is valid to know if the connection is "up" or "down": TCP does not exchange any packets when idle, unless you activate keep-alive (which is not normally recommended). It may well happen that the party thinks it has a connection, only to send a message and then it fails (perhaps because a router was reset while the connection was idle).
To solve the problem we need to look at the return value of readLine(). If it return null so our tcp connection was closed.