我有一个在C#机器上运行的TCP套接字。我需要通过服务器IP和端口从Android连接该服务器套接字,如下所示:
InetAddress serverAddr = InetAddress.getByName(serverIp);
Socket socket = new Socket(serverAddr, serverPort);
socket.setSoTimeout(10*1000);
如果c#机器没有在Android上运行的套接字,它将挂起:
Socket socket = new Socket(serverAddr, serverPort);
我需要实现5秒钟的超时,就像它在此ip上找不到服务器套接字一样,可能只是超时。
有想法请..
可以帮助您:
使用无参数构造函数创建套接字,如下所示:
Socket socket = new Socket();
然后使用
socket.connect(remoteAddress, timeout);
其它的办法:
Socket socket= new Socket();
socket.connect(new InetSocketAddress(hostip,port_num),connection_time_out);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句