开发者

A problem when creating the socket

I've a android app which try to connect to server. But there is a problem when I create a socket. the application hang when it try implement the following line:

Socket socket= null;
socket = new Socket("192.168.0.15", 6565);

note that I have added this permission:

<uses-permission android:name="android.permission.INTERNET" />

also I used the debug but it hang when it reach to creating socket line and I've got this detail about the error:

java.net.SocketTimeoutException: Socket is not connected id=829731319824

and I am sure that port is not bound. I appr开发者_如何学Goeciate your time.


Your connection probably cannot be established or is taking some time to be established.

Operations on java.net.Socket are blocking that's why your application appears to be hung. To avoid "hanging" the UI, you should probably try to establish the connection using another thread so your UI thread won't block and will remain responsive.


Obviously your connection cannot be established and the problem lies not within your code but the network setup. To avoid your app to hang, make sure you catch the SocketTimeoutException to be able to show an error message to the user along with some buttons to, for instance, try again or cancel the connection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜