我的Android应用程序在套接字关闭之前不会收到数据(通常我强行关闭服务器,然后响应进入客户端)。
任何关心的人都会看到一些代码:
-Server
private void receiveCallback(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
int received = socket.EndReceive(ar);
byte[] dataBuf = new byte[received];
Array.Copy(_buffer, dataBuf, received);
string text = Encoding.ASCII.GetString(dataBuf);
form.logMessage("Recieved: " + text);
//Check commands
if (!String.IsNullOrWhiteSpace(text))
{
sendText(form.onCommand(text), socket);
}
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
public void sendText(string message,Socket socket)
{
byte[] data = Encoding.ASCII.GetBytes(message);
form.logMessage("Sending: " + message);
socket.NoDelay = true;
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(sendCallback), socket);
form.logMessage("Sent!");
socket.NoDelay = false;
}
- 客户端
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "Clicked send button!");
String babam = ((EditText)findViewById(R.id.requestText)).getText().toString();
Log.i(TAG,"Going to send: " + babam);
new Thread(new ResponseThread(babam)).start();
}
});
public class ResponseThread implements Runnable{
String toSend;
public ResponseThread(String toSend)
{
this.toSend = toSend;
}
@Override
public void run() {
try {
PrintStream stream = new PrintStream(socket.getOutputStream(),true);
stream.println(toSend);
Log.i(TAG, "Sent request!");
Log.i(TAG, "Waiting for reponse...");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = in.readLine();
Log.i(TAG,"Response: " + response);
statusText.setText(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的一些输出:
-Server
[8/9/2015 9:04:28 PM] Recieved: roll
[8/9/2015 9:04:28 PM] Rolled dice! 6 + 4 = 10
[8/9/2015 9:04:28 PM] Sending: Rolled!
[8/9/2015 9:04:28 PM] Sent!
[8/9/2015 9:04:28 PM] Recieved:
[8/9/2015 9:04:29 PM] Recieved: roll
[8/9/2015 9:04:29 PM] Rolled dice! 5 + 1 = 6
[8/9/2015 9:04:29 PM] Sending: Rolled!
[8/9/2015 9:04:29 PM] Sent!
[8/9/2015 9:04:29 PM] Recieved:
- 客户端
08-09 20:48:29.133 12801-12801/com.artish1.sockettest I/Client﹕ Clicked send button!
08-09 20:48:29.133 12801-12801/com.artish1.sockettest I/Client﹕ Going to send: roll
08-09 20:48:29.158 12801-13029/com.artish1.sockettest I/Client﹕ Sent request!
08-09 20:48:29.158 12801-13029/com.artish1.sockettest I/Client﹕ Waiting for reponse...
08-09 20:49:04.371 12801-12801/com.artish1.sockettest I/Client﹕ Clicked send button!
08-09 20:49:04.371 12801-12801/com.artish1.sockettest I/Client﹕ Going to send: roll
08-09 20:49:04.382 12801-13280/com.artish1.sockettest I/Client﹕ Sent request!
08-09 20:49:04.382 12801-13280/com.artish1.sockettest I/Client﹕ Waiting for reponse...
08-09 20:49:37.399 12801-13029/com.artish1.sockettest I/Client﹕ Response: Rolled!YepRolled!Yep
08-09 20:49:37.400 12801-13280/com.artish1.sockettest I/Client﹕ Response: null
请注意所有响应是如何同时出现的,那就是我强行关闭服务器的时候。
我对套接字没有经验,但我一直在查看有关此问题的其他帖子,有人说要强制刷新请求。这样我可以在客户端上获取它,但我已经通过服务器发送它(通过输出获得确认)。
有人想带一只雄鹅吗?
答案 0 :(得分:-1)
尝试在邮件末尾添加\n
。也不要将“NoDelay”设置为false。你正在进行异步发送。