Java android客户端套接字直到套接字断开连接后才得到响应

本文关键字:套接字 响应 连接 断开 android Java 客户端 | 更新日期: 2023-09-27 18:07:03

我的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

请注意所有的响应是如何同时出现的,这是我强制关闭服务器的时候。

我对套接字没有经验,但我一直在看其他关于这个的帖子,有些人说强制刷新请求。这样我可以在客户机上获得它,但我已经通过服务器发送了它(通过输出获得确认)。

有人想看一下吗?

Java android客户端套接字直到套接字断开连接后才得到响应

尝试在消息末尾添加'n。也不要将'NoDelay '设置为false。您正在执行异步发送。