我编写了一个 Android 套接字演示,它只是将用户的输入发布到服务器。我通过wifi连接从android客户端建立了一个socket,一切顺利,服务器可以接收到android客户端发送的消息。问题是,然后我关闭了手机的WIFI,但是socket可以正常写入。
安卓客户端代码:
public class MyActivity extends Activity {
private SocketHandlerThread thread;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
thread = new SocketHandlerThread("ScoketTest");
thread.start();
}
class SocketHandlerThread extends HandlerThread {
private Socket socket;
private Handler handler;
public SocketHandlerThread(String name) {
super(name);
}
public SocketHandlerThread(String name, int priority) {
super(name, priority);
}
@Override
public void run() {
try {
socket = new Socket("192.168.60.184", 1990);
} catch (IOException e) {
Log.e("SocketTest", e.getLocalizedMessage(), e);
}
super.run();
}
Handler getHandler() {
if (handler == null) {
handler = new Handler(getLooper());
}
return handler;
}
void send(final String text) {
Runnable runnable = new Runnable() {
public void run() {
Log.e("SocketTest", "Start send text: " + text);
try {
socket.getOutputStream().write((text + "\n").getBytes());
socket.getOutputStream().flush();
} catch (Exception e) {
Log.e("SocketTest", e.getLocalizedMessage(), e);
}
Log.e("SocketTest", "Text has been send:" + text);
}
};
getHandler().post(runnable);
}
@Override
protected void onLooperPrepared() {
runOnUiThread(new Runnable() {
@Override
public void run() {
findViewById(R.id.button).setEnabled(true);
}
});
}
}
public void send(View view) {
String text = ((EditText) findViewById(R.id.text)).getText().toString();
thread.send(text);
}
}
服务器代码:
public class SocketTestServer {
ServerSocket serverSocket;
SocketTestServer() throws IOException {
serverSocket = new ServerSocket(1990);
}
void start() throws IOException {
Socket clientSocket = serverSocket.accept();
clientSocket.getInputStream();
PrintWriter out = new PrintWriter(System.out, true);
BufferedReader in =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
}
public static void main(String[] args) throws IOException {
SocketTestServer server = new SocketTestServer();
server.start();
}
}
我尝试了几部手机。在 Galaxy Nexus(4.2.1) 上按预期抛出异常,但在某些 MOTO 或 HTC 手机上,套接字仍然可以毫无异常地写入,这意味着我可能会丢失一些我认为已成功接收的消息。我怎么知道任何类型的手机上的套接字连接都断开了?
任何建议将不胜感激。
ps 我知道连接更改广播,但在接收广播之前,客户端可能已经通过损坏的套接字写入了一些消息。
虽然在应用协议上添加接收检查可以解决消息丢失的问题,但我更喜欢保证 TCP 协议承诺的传输层的可靠性。