【发布时间】:2012-05-29 18:53:04
【问题描述】:
在我位于 android 设备中的服务器中,如果客户端数量超过特定数量,则服务器关闭套接字。但是在我的客户端(其他 android 设备)中,我强制关闭。我怎样才能优雅地处理它? 这是我客户端上的连接部分:
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals(""))
{
try
{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
SocketAddress sockaddr = new InetSocketAddress(serverAddr, 5000);
nsocket = new Socket();
nsocket.connect(sockaddr);
}catch(Exception e){
Log.i("Connect", "Connection Error");
}
if (nsocket.isConnected()){
score.setText("Your score is " + sc);
serverIp.setVisibility(View.GONE);
connectPhones.setVisibility(View.GONE);
enterIP.setVisibility(View.GONE);
Log.i("Connect", "Socket created, streams assigned");
Log.i("Connect", "Waiting for inital data..." + nsocket.isConnected());
receiveMsg();
}
【问题讨论】:
-
向您通过套接字使用的协议添加一个关闭过程。我们很难帮助您做到这一点,因为我们不知道您的协议是什么。也许服务器可以向客户端发送“稍后再试”消息,然后客户端会关闭套接字?这样,服务器只有在客户端没有响应时才会关闭,并且通常它会干净地发生。
-
也抓到
Exceptionis considered usually a bad practice
标签: java android sockets client client-server