get data from client in server socket java

24,871

Solution 1

Problem with your implementation

BufferedReader#readLine:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

In other words, if your client doesn't ever send \nor \r character that method will not end until the IOException gets thrown as a result of disconnect.

Solution

Replace this code:

BufferedReader reader = new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
String clientData = "";
clientData = reader.readLine();

with:

int red = -1;
byte[] buffer = new byte[5*1024]; // a read buffer of 5KiB
byte[] redData;
StringBuilder clientData = new StringBuilder();
String redDataText;
while ((red = clientSocket.getInputStream().read(buffer)) > -1) {
    redData = new byte[red];
    System.arraycopy(buffer, 0, redData, 0, red);
    redDataText = new String(redData,"UTF-8"); // assumption that client sends data UTF-8 encoded
    System.out.println("message part recieved:" + redDataText); 
    clientData.append(redDataText);
}
System.out.println("Data From Client :" + clientData.toString());

InputStream#read:

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

will read as many bytes as it can at the exact moment of its execution - it is basically buffered reading. Since these are raw bytes, when converting them to String you must know its encoding in order to show them correctly (that's the "UTF-8" part). If the encoding in which your client sends bytes is other, you might need to change it in order to get correct text in console output.

I recommend reading the official tutorial lessons:

Solution 2

BufferedReader.readLine() will only return when there's an end-of-line or end-of-stream. Make sure the client side is sending a newline character, or use a different way to read the input stream like:

int ch = 0;
while ((ch = instream.read()) >= 0) {
    // do sometyhing with the character off the input stream.
    System.out.println("Got byte " + ch);
}
// will get here when the input stream is closed.
Share:
24,871
Shaggy
Author by

Shaggy

Updated on February 28, 2020

Comments

  • Shaggy
    Shaggy about 4 years

    I am creating a server app which does the following task

    • Accept connection from client
    • Process each client connection to separate thread
    • Receive data from client
    • send data to client

    I am able to connect client but not able to receive data from client

    Data is being visible in my console only when THAT CLIENT GETS DISCONNECTED..!!!

    Code :-

    public class ServerListener {
    
        public static void main(String[] args) {
            new ServerListener().startServer();
        }
    
        public void startServer() {
            final ExecutorService clientProcessingPool = Executors
                    .newFixedThreadPool(10);
    
            Runnable serverTask = new Runnable() {
                @Override
                public void run() {
                    try {
                        ServerSocket serverSocket = new ServerSocket(8000);
                        System.out.println("Waiting for clients to connect...");
                        while (true) {
                            Socket clientSocket = serverSocket.accept();
                            clientProcessingPool
                                    .submit(new ClientTask(clientSocket));
                        }
                    } catch (IOException e) {
                        System.err.println("Unable to process client request");
                        e.printStackTrace();
                    }
                }
            };
            Thread serverThread = new Thread(serverTask);
            serverThread.start();
        }
    
        private class ClientTask implements Runnable {
            private final Socket clientSocket;
    
            private ClientTask(Socket clientSocket) {
                this.clientSocket = clientSocket;
            }
    
            @Override
            public void run() {
                System.out.println("Got a client !");
                try {
                /* Get Data From Client */
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(clientSocket.getInputStream()));
                    String clientData = "";
                    clientData = reader.readLine();
                    System.out.println("Data From Client :" + clientData);
    
                /* Send Data To Client */
    
                    //Code
    
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }