首页 文章

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

提问于
浏览
0

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

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

在实践中,我得到的是:

  • 服务器继续

  • App连接到服务器

  • App发送字节数组

  • 服务器接收数据

  • 服务器检查是否有任何要发回的消息
    如果是

  • ,则发送数据

  • App无法识别输入流

Client side

//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();
            }
        }

Server side

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

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

    if (inputStream.available() > 0) {
                        //never gets in this statement
                        DataInputStream dataInputStream = new DataInputStream(inputStream);
                        String msg = dataInputStream.readUTF();
                        socket.shutdownInput();
                    }
    

    方法 available() 没有阻塞,这意味着如果在发送消息后没有立即读取, available() 将返回0 .

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

    String msg = dataInputStream.readUTF();
    

    readUTF() 正在阻塞,这意味着你的代码将在此处停止,直到socket获得某些东西 .

相关问题