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开发者_Go百科 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.
精彩评论