消息是第一次发送,为什么我不能再次发送消息(Android套接字编程)

时间:2020-08-26 05:54:56

标签: android sockets client outputstream

我为客户端创建了套接字编程类。连接已建立。服务器向客户端发送消息。客户端可以从服务器获取消息。但是客户只有一个答案发送。为什么?输出流不对吗?

public class Message extends AsyncTask<byte[], Void, Void> {
        Socket s;
        public static byte[] recv;
    
    
        @Override
        protected Void doInBackground(byte[]... voids) {
            byte[] message = voids[0];
            try {
                s = new Socket(destIp, destPort);
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                dos.writeInt(message.length);
                dos.write(message);
                dos.flush();
                InputStream inFromServer = s.getInputStream();
                DataInputStream in = new DataInputStream(inFromServer);
    
                byte[] buffer = new byte[200];
                int read = 0;
                while ((read = in.read(buffer, 0, buffer.length)) != -1) {
    //                in.read(buffer);
                    recv = Arrays.copyOf(buffer, read);
                    Context context = getContext();
                    Intent intentManager = new Intent(context, ManagerService.class);
                    context.startService(intentManager);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

我在启动asynctask的部分中对此进行了修复

new MessageReceive().execute(sendData);

new MessageReceive().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,sendData);
相关问题