简体   繁体   中英

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.

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?

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.

What you want to do is get rid of this if clause, and focus on this line you wrote:

 String msg = dataInputStream.readUTF();

readUTF() is blocking, meaning your code will stop here until socket gets something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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