套接字在发送后没有收到数据

时间:2016-09-04 12:37:24

标签: java android sockets inputstream outputstream

我还是比较新手, 所以我遇到了一个小问题

我有我的Android应用程序,通过套接字发送字节数组,然后服务器(也用java制作)接收数据,如果需要,它会将响应发送回客户端。

在实践中,我得到的是:

  1. 服务器继续
  2. 应用程序连接到服务器
  3. App发送字节数组
  4. 服务器接收数据
  5. 服务器检查是否有任何要发回的消息
  6. 如果是,请发送数据
  7. App无法识别输入流
  8. 客户端

    //infinite loop
            while(true) {
                //client has anything to be sent
                if (msg != null) {
                    //open output stream
                    OutputStream outputStream = socket.getOutputStream();
                    DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
    
                    if (msg.length > 0) {
                        dataOutputStream.write(msg, 0, msg.length);
                        dataOutputStream.flush();
                        outputStream.flush();
                        //send message (byte array)
                    }
    
                    InputStream inputStream = socket.getInputStream();
                    if (inputStream.available() > 0) {
                        //never gets in this statement
                        DataInputStream dataInputStream = new DataInputStream(inputStream);
                        String msg = dataInputStream.readUTF();
                        socket.shutdownInput();
                    }
                    disconnect();
                }
            }
    

    服务器端

    try {
                //server inputstream
                InputStream inputStream = client.getInputStream();  
                //sucessfully goes in
                if(inputStream.available() > 0) {
                    BufferedReader inReader = new BufferedReader(new InputStreamReader(inputStream));
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                    //receives all of this properly, no problems
                    try (FileOutputStream fos = new FileOutputStream(filename)) {
                        int size = 1024;
                        int filesize = client.getReceiveBufferSize();
                        byte[] buffer = new byte[1024];
    
                        int bytesReceived;
                        while((bytesReceived = inputStream.read(buffer, 0, size)) > -1) {
                            fos.write(buffer, 0, bytesReceived);
                            output.write(buffer, 0, bytesReceived);
                        }
                        fos.close();
                    }
    
    
                    String message = frame.getMessage();
                    if(message.length() > 0) {
                        try {
                            //goes in here, no problem
                            OutputStream outputStream = client.getOutputStream();
                            DataOutputStream dos = new DataOutputStream(outputStream);
                            dos.writeUTF(message);
                            //writes the utf properly no problem
                            dos.close();
                            outputStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(ReceiveDataThread.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        System.out.println("Message '"+message+"' has been sent to " + frame.getConnection().getIp());
                        //all ok
                    }
                    client.close();
                    client = null;
                    Thread.currentThread().interrupt();
                }
    
            } catch (IOException ex) {
                Logger.getLogger(ReceiveDataThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    

    现在,为什么客户端没有收到服务器的UTF响应?

1 个答案:

答案 0 :(得分:0)

不是套接字专家,但我认为您的错误可能在客户端,在这里:

rf

方法if (inputStream.available() > 0) { //never gets in this statement DataInputStream dataInputStream = new DataInputStream(inputStream); String msg = dataInputStream.readUTF(); socket.shutdownInput(); } 没有阻塞,这意味着如果在发送邮件后没有任何内容可以立即阅读,available()将返回0.

你想要做的就是摆脱这个if子句,并专注于你写的这一行:

available()

String msg = dataInputStream.readUTF(); 正在阻塞,这意味着您的代码将在此处停止,直到socket获得某些内容。