I am creating a server app which does the following task
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();
}
}
}
}
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 \n
or \r
character that method will not end until the IOException
gets thrown as a result of disconnect.
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());
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: