简体   繁体   English

发送后套接字未接收到数据

[英]Socket not receiving data after sending

I'm still relatively new to socketing, so I have encountered a small problem 我对插口还比较陌生,所以遇到了一个小问题

I have my android app, that is encharged to send a byte array over a socket, and then the server (also made in java) receives the data, and if necessary, it sends a response back to the client. 我有我的Android应用程序,该应用程序负责通过套接字发送字节数组,然后服务器(也是用Java制造)接收数据,并在必要时将响应发送回客户端。

In the practice what I'm getting is: 在练习中,我得到的是:

  1. Server goes on 服务器继续
  2. App connects to server 应用程序连接到服务器
  3. App sends byte array 应用发送字节数组
  4. Server receives data 服务器接收数据
  5. Server checks if theres any message to be sent back 服务器检查是否有任何邮件要发送回
  6. if so, send the data 如果是这样,发送数据
  7. App doesn't recognize the inputstream 应用无法识别输入流

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

Now, why isn't the client receiving the server's UTF response? 现在,为什么客户端没有收到服务器的UTF响应?

Not expert on sockets, but i think your bug might be in client, here: 不是套接字方面的专家,但是我认为您的错误可能在客户端中,在这里:

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

The method available() is not blocking, meaning that if there is nothing to read immediately after the message was sent, available() will return 0. available()方法不会阻塞,这意味着如果在发送消息后没有立即读取的内容, available()将返回0。

What you want to do is get rid of this if clause, and focus on this line you wrote: 您想要做的是摆脱此if子句,并专注于您编写的这一行:

 String msg = dataInputStream.readUTF();

readUTF() is blocking, meaning your code will stop here until socket gets something. readUTF()被阻止,这意味着您的代码将在此处停止,直到套接字得到某些东西为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM