简体   繁体   中英

Unable to read data from Server in Java

I'm trying to read and write the data to the server using java program. I'm writing the data using console. I can successfully write the data to the server, but the problem comes when I try to read the data sent by the server.

import java.io.*;
import java.net.*;


public class EMSSocketConnection {



    public static void main(String[] args) throws Exception {

        createSocket();
    }

    private static void sendMessage(DataOutputStream out) throws Exception
    {
        try 
        {

            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String userOutput;
            while ((userOutput = stdIn.readLine()) != null)
            {
                out.writeBytes(userOutput);
                out.write('\n');
            }
            out.flush();

        }
        catch(Exception ex)
        {
            System.out.println(ex.getStackTrace());

        }
        finally
        {
            out.close();
        }
    }


    private static void readResponse(DataInputStream in) throws Exception
    {
        try
        {
            byte[] data = new byte[1024];
            in.readFully(data);
            System.out.println(data);


        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());

        }
        finally
        {
            in.close();
        }
    }

    private static void createSocket()
    {

        try
        {
            int port = 2345;
            InetAddress address = InetAddress.getByName("192.100.100.129");
            final Socket client = new Socket(address, port);
            final DataOutputStream out = new DataOutputStream(client.getOutputStream());
            final DataInputStream in = new DataInputStream(client.getInputStream());

            System.out.println("Successfully Connected");



            new Thread() {
                public void run() {
                    synchronized(client)
                    {
                        try {
                            while(true)
                            {
                            sendMessage(out);
                            readResponse(in);
                            }
                        }
                        catch (Exception ex)
                        {
                            System.out.println(ex.getMessage());
                        }
                    }
                }
            }.start();
        }
        catch(Exception ex)
        {
            ex.getStackTrace();
        }
    }

}

Could anyone please tell me how can I successfully read the data from server?

Is your server writing 1024 bytes to its output? If not, your code

byte[] data = new byte[1024];
in.readFully(data);

will block and wait indefinitely.

A far better idiom would be to do this:

byte [] data = new byte[in.available()];
in.readFully(data);

You cannot just read willy-nilly from streams and assume that you'll get your expected data. Streams suffer from IO delays and buffering, and you need to be aware of this, especially when dealing with.network IO.

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