BufferedWriter does not flush - java

I have the following problem....
try
{
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));
while(true)
{
String clientRequest = "";
String tempStr = clientInput.readLine();
while(tempStr != null && !tempStr.equals("null"))
{
System.out.println(tempStr);
clientRequest += tempStr + " ";
tempStr = clientInput.readLine();
}
//Parse Request
ArrayList<String> tokenArray = parseRequest(clientRequest);
Calendar c = Calendar.getInstance();
switch(tokenArray.get(0))
{
case "GET":
{
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
clientOutput.write("Server: Java HTTP Server v1.0\r\n");
clientOutput.flush();
break;
//Write File
}
default:
{
clientOutput.write("500\r\n");
clientOutput.flush();
}
}
}
}
Every thing works completely fine up-and-till the clientOutput.write("HTTP....... line,
the client just keeps waiting and waiting... i've attempted to flush after every sucsessive write and yet nothing..... BUT This is the weird part - if i write to and flush before the code enters the while-loop the the writes in the case "GET": works perfectly...... ie
The code does execute all the way to the
clientOutput.flush();
break;
//Write File
.
try
{
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.flush();
while(true)
{
String clientRequest = "";
String tempStr = clientInput.readLine();
while(tempStr != null && !tempStr.equals("null"))
{
System.out.println(tempStr);
clientRequest += tempStr + " ";
tempStr = clientInput.readLine();
}
//Parse Request
ArrayList<String> tokenArray = parseRequest(clientRequest);
Calendar c = Calendar.getInstance();
switch(tokenArray.get(0))
{
case "GET":
{
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
clientOutput.write("Server: Java HTTP Server v1.0\r\n");
clientOutput.flush();
break;
//Write File
}
Here is the code for the client
Socket s = new Socket("localhost", 1337);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter toServer = new BufferedWriter(new PrintWriter(s.getOutputStream()));
toServer.write("GET index.html HTTP/1.1\r\n");
toServer.write("HOST: 127.0.0.1\r\n");
toServer.write("Connection: close\r\n");
toServer.write("\r\n");
toServer.write("null\r\n");
toServer.flush();
while(true)
{
String ss = fromServer.readLine();
if(ss != null && !ss.equals("null"))
System.out.println(ss);
}
Server Class: Strydom_A_201103578_P03
public class Strydom_A_201103578_P03
{
Thread[] threadArray = new Thread[5];
int ClientCount = 0;
public Strydom_A_201103578_P03() throws ClientSizeExceededException
{
ServerSocket httpServer = null;
try
{
httpServer = new ServerSocket(1337);
}
catch (IOException ex)
{
Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
}
while(true)
{
try
{
//Wait for connection
Socket clientSocket = httpServer.accept();
if(ClientCount < 5)
{
threadArray[ClientCount] = new Thread(new clientHandler(clientSocket));
threadArray[ClientCount].start();
ClientCount++;
}
else
{
throw new ClientSizeExceededException();
}
}
catch(IOException ex)
{
}
finally
{
}
}
}
class clientHandler implements Runnable
{
Socket clientSocket;
public clientHandler(Socket clientSocket)
{
this.clientSocket = clientSocket;
}
#Override
public void run()
{
BufferedReader clientInput = null;
BufferedWriter clientOutput = null;
try
{
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));
clientOutput.write(" ");
clientOutput.flush();
while(true)
{
String clientRequest = "";
String tempStr = clientInput.readLine();
while(tempStr != null && !tempStr.equals("null"))
{
System.out.println(tempStr);
clientRequest += tempStr + " ";
tempStr = clientInput.readLine();
}
//Parse Request
ArrayList<String> tokenArray = parseRequest(clientRequest);
Calendar c = Calendar.getInstance();
switch(tokenArray.get(0))
{
case "GET":
{
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
clientOutput.write("Server: Java HTTP Server v1.0\r\n");
clientOutput.flush();
break;
//Write File
}
default:
{
clientOutput.write("500\r\n");
clientOutput.flush();
}
}
}
}
catch (IOException ex)
{
Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try
{
clientInput.close();
clientOutput.close();
}
catch (IOException ex)
{
Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private ArrayList<String> parseRequest(String tempStr)
{
StringTokenizer httpTokens = new StringTokenizer(tempStr, " ");
ArrayList<String> tokens = new ArrayList<>();
while(httpTokens.hasMoreTokens())
tokens.add(httpTokens.nextToken());
return tokens;
}
}
public static void main(String[] args) throws ClientSizeExceededException
{
new Strydom_A_201103578_P03();
}
}
public class TestClient
{
public TestClient()
{
try
{
Socket s = new Socket("localhost", 1337);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter toServer = new BufferedWriter(new PrintWriter(s.getOutputStream()));
toServer.write("GET index.html HTTP/1.1\r\n");
toServer.write("HOST: 127.0.0.1\r\n");
toServer.write("Connection: close\r\n");
toServer.write("\r\n");
toServer.write("null\r\n");
toServer.flush();
while(true)
{
String ss = fromServer.readLine();
if(ss != null && !ss.equals("null"))
System.out.println(ss);
}
}
catch (UnknownHostException ex)
{
Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args)
{
new TestClient();
}
}
Client Class: TestClient
Create a project( or 2) and run the files

The problem here is the PrintWriter. It swallows exceptions. Change it to an OutputStreamWriter. Then you will see any exception that is being swallowed. In general you should avoid PrintWriters and PrintOutputStreams over a network. They swallow exceptions that you need to know about.

You need to change your inner while loop to look for the end of the client's request:
while(tempStr != null && !tempStr.equals("null"))
to:
while(tempStr != null && !tempStr.equals("null") && !tempStr.equals(""))
The client won't disconnect (causing a null) after it sends a request. It will give you a blank line to indicate the end of its request.
Reason why returning the response header right away is working? Maybe the client just reads the 200 and (eventually) disconnects? So when you are reading the client's request, it ends and you get a null eventually.
EDIT:
So running your code, it works fine for me. Both the client and server are sending and receiving both requests and responses. However, the server never disconnects (the client includes a Connection: close header) and the client continues to block on readLine(). Unsurprisinly, when I include the write() and flush() immediately after setting up the connection on the server-side nothing changes except I see HTTP/1.1 200 OK twice on the client's end. Maybe all you need to do is close the clientSocket in a finally{} block at the end of your try/catch{}?

So for the futhering of my now ended suffering - here is what i finally did....
I changed both the server and the clients readers from BufferedReader/Writer to DataInputstream/OutputStream.... And it works perfectly now - ! Thanks to everyone
Aiden

Just do this and it will work..........
Add true as the 2nd parameter in the PrintWriter
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream(), true));

Related

Java BufferedReader frozen [duplicate]

I wanna write the code to let Client send a string to Server, Server print the string and reply a string, then Client print the string Server reply.
My Server
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = null;
Socket s = null;
try {
ss = new ServerSocket(34000);
s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
s.getInputStream()));
OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream());
while (true) {
String string = in.readLine();
if (string != null) {
System.out.println("br: " + string);
if (string.equals("end")) {
out.write("to end");
out.flush();
out.close();
System.out.println("end");
// break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
s.close();
ss.close();
}
}
}
My Client:
public class Client {
public static void main(String[] args) {
Socket socket =null;
try {
socket = new Socket("localhost", 34000);
BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
String string = "";
string = "end";
out.write(string);
out.flush();
while(true){
String string2 = in.readLine();
if(string2.equals("to end")){
System.out.println("yes sir");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
System.out.println("closed client");
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
are there some somethings wrong? if i remove the code "while(true) ..." in client class, it's OK.
you should add "\r\n" at the end of the String which write into stream.
example:
client :
string = "end";
out.write(string + "\r\n");
out.flush();
server :
out.write("to end" + "\r\n");
out.flush();
out.close();
System.out.println("end");
// break;
I don't see the server response.
You do a
System.out.println("br: " + string);
but not a
out.write(string);
out.flush();
Appand "\n" to end of the response from server.
outToClient.writeBytes(sb.toString() + "\n");
You are reading lines but you aren't writing lines. Add a newline, or call BufferedReader.newLine().

How to keep TCP sockets open?

Our application has a ping-pong like conversation with many servers (each server has a corresponding thread where those connections are made). Code below works, but it opens a new connection for every new request and is used only once, which soon leads to reaching max connection cap set by server.
DataProvider.java
public static ZnResult sendTcpQuery(String xml, String url, int port) {
List<ZnXmlResult> results = new ArrayList<>();
String xmlString = xml != null ? new String((xml + "\n").getBytes()) : "";
int error = ZnResult.OK;
try (Socket clientSocket = new Socket(url, port)) {
clientSocket.setSoTimeout(CONNECTION_TIMEOUT);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
try (BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"))) {
outToServer.writeBytes(xmlString);
try (StringWriter responseFromServer = new StringWriter()) {
String readLine;
while ((readLine = inFromServer.readLine()) != null) {
...
}
}
outToServer.close();
clientSocket.close();
}
} catch (Exception ex) {
LOG.error("Exception {}", url + ":" + port, ex);
error = ZnResult.ERR;
}
return error == ZnResult.OK ? new ZnResult(results) : new ZnResult(error);
}
How can I transform it, so everything can be done within one connection?
I figured I would do something like this:
SocketFactory.java
public class SocketFactory {
private static HashMap<String, Socket> socketsByAddress = new HashMap<>();
private static HashMap<Socket, DataOutputStream> outputStreamsBySocket = new HashMap<>();
private static HashMap<Socket, BufferedReader> readersBySocket = new HashMap<>();
public static Socket getSocket(String address) {
String ip = Tools.getIpFromAddress(address);
int port = Tools.getPortFromAddress(address);
Socket socket = socketsByAddress.get(address);
if (socket == null) {
try {
socket = new Socket(ip, port);
socket.setSoTimeout(60000);
socketsByAddress.put(address, socket);
} catch (IOException ex) {
Logger.getLogger(SocketFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
return socket;
}
public static DataOutputStream getOutputStream(Socket socket) {
DataOutputStream outputStream = outputStreamsBySocket.get(socket);
if (outputStream == null) {
try {
outputStream = new DataOutputStream(socket.getOutputStream());
outputStreamsBySocket.put(socket, outputStream);
} catch (IOException ex) {
Logger.getLogger(SocketFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
return outputStream;
}
public static BufferedReader getReader(Socket socket) {
BufferedReader reader = readersBySocket.get(socket);
if (reader == null) {
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
readersBySocket.put(socket, reader);
} catch (IOException ex) {
Logger.getLogger(SocketFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
return reader;
}
}
DataProvider.java
public static ZnResult sendTcpQuery(String xml, String url, int port) {
List<ZnXmlResult> results = new ArrayList<>();
int error = ZnResult.OK;
try {
String xmlString = xml != null ? new String((xml + "\n").getBytes()) : "";
Socket clientSocket = SocketFactory.getSocket(url + ":" + port);
DataOutputStream outToServer = SocketFactory.getOutputStream(clientSocket);
BufferedReader inFromServer = SocketFactory.getReader(clientSocket);
outToServer.writeBytes(xmlString);
try (StringWriter responseFromServer = new StringWriter()) {
String readLine;
while ((readLine = inFromServer.readLine()) != null) {
...
}
}
} catch (Exception ex) {
LOG.error("Exception {}", url + ":" + port, ex);
error = ZnResult.ERR;
}
return error == ZnResult.OK ? new ZnResult(results) : new ZnResult(error);
}
but it just doesn't work and only the first one go through.
This loop reads until the end of the stream.
while ((readLine = inFromServer.readLine()) != null) {
A stream only ends once. i.e. you can't end the stream but later use it again.
What you need to do instead;
have a terminating line which can't occur in your data. e.g. wait for "[EOF]"
send the length of data first and read only that much data.
Try to initiate the Socket object using URL rather than IP address as you were doing in your first code and see if it works for you.

Multithreading with client server program

I am trying to implement multi threading with a client/server program I have been working on. I need to allow multiple clients to connect to the server at the same time. I currently have 4 classes: a Client, a Server, a Protocol and a Worker to handle the threads. The following code is what I have for those classes:
SocketServer Class:
public class SocketServer {
public static void main(String[] args) throws IOException {
int portNumber = 9987;
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
Thread thread = new Thread(new ClientWorker(clientSocket));
thread.start(); //start thread
String inputLine, outputLine;
// Initiate conversation with client
Protocol prot = new Protocol();
outputLine = prot.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = prot.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("quit"))
break;
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
SocketClient Class:
public class SocketClient {
public static void main(String[] args) throws IOException
{
String hostName = "localhost";
int portNumber = 9987;
try (
Socket socket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("quit"))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
Protocol Class:
public class Protocol {
private static final int waiting = 0;
private static final int sentPrompt = 1;
private int status = waiting;
public String processInput(String theInput) {
String theOutput = null;
if (status == waiting) {
theOutput = "Please enter what you would like to retrieve: 'customer' or 'product' ";
status = sentPrompt;
}
else if ( status == sentPrompt ) {
if ( theInput.equalsIgnoreCase("product")) {
File f = new File("product.txt");
Scanner sc = null;
try {
sc = new Scanner(f);
} catch (FileNotFoundException ex) {
Logger.getLogger(Protocol.class.getName()).log(Level.SEVERE, null, ex);
}
while ( sc.hasNextLine() ) {
String line = sc.nextLine();
theOutput = "The current product entries are : " + line;
}
return theOutput;
}
else if ( theInput.equalsIgnoreCase("customer")) {
File f = new File("customer.txt");
Scanner sc = null;
try {
sc = new Scanner(f);
} catch (FileNotFoundException ex) {
Logger.getLogger(Protocol.class.getName()).log(Level.SEVERE, null, ex);
}
while ( sc.hasNextLine() ) {
String line = sc.nextLine();
theOutput = "The current customer entries are : " + line;
}
return theOutput;
}
else if ( theInput.equalsIgnoreCase("quit")) {
return "quit";
}
else {
return "quit";
}
}
return theOutput;
}
}
The ClientWorker Class:
public class ClientWorker implements Runnable {
private final Socket client;
public ClientWorker( Socket client ) {
this.client = client;
}
#Override
public void run() {
String line;
BufferedReader in = null;
PrintWriter out = null;
try {
System.out.println("Thread started with name:"+Thread.currentThread().getName());
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("in or out failed");
System.exit(-1);
}
while (true) {
try {
System.out.println("Thread running with name:"+Thread.currentThread().getName());
line = in.readLine();
//Send data back to client
out.println(line);
//Append data to text area
} catch (IOException e) {
System.out.println("Read failed");
System.exit(-1);
}
}
}
}
When I run the server and client, everything works fine as expected. Then when I try to run another client, it just hangs there and does not prompt the client to give a response. Any insight into what I am missing is greatly appreciated!
Your server code should address implement below functionalities.
Keep accepting socket from ServerSocket in a while loop
Create new thread after accept() call by passing client socket i.e Socket
Do IO processing in client socket thread e.g ClientWorker in your case.
Have a look at this article
Your code should be
ServerSocket serverSocket = new ServerSocket(portNumber);
while(true){
try{
Socket clientSocket = serverSocket.accept();
Thread thread = new ClientWorker(clientSocket);
thread.start(); //start thread
}catch(Exception err){
err.printStackTrace();
}
}
How many times does serverSocket.accept() get called?
Once.
That's how many clients it will handle.
Subsequent clients trying to contact will not have anybody listening to receive them.
To handle more clients, you need to call serverSocket.accept() in a loop.

Java, Sockets, BufferedReader and StringBuilder

Yestarday I wrote a post about Java and Sockets, and today I'm still here because I'm having an issue with BufferedReaders.
I searched some questions here in StackOverflow and I understand the problem, but I can't fix it
My "application" has got two parts: a server and a client, and the scope of the application is to execute MS-DOS commands on the machine where the server is running (the commands are sent by the client).
Now the code (I will post the total code because it's easier to understand, I will put a comment in non-working part of the code) Server:
import java.net.*;
import java.io.*;
public class TCPCmdServer {
public int port;
public ServerSocket server;
public final String version = "Beta 1.0";
TCPCmdServer(int port) {
this.port = port;
if (!createServer())
System.out.println("Cannot start the server");
else {
System.out.println("**********************************************");
System.out.println("Command executer, server version: " + version);
System.out.println("Server running on port " + port);
System.out.println("Code by luc99a alias L99");
System.out.println("**********************************************");
}
}
public boolean createServer() {
try {
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
TCPCmdServer tcp = new TCPCmdServer(5000);
while (true) {
Socket socket = null;
BufferedReader in = null;
BufferedWriter out = null;
try {
socket = tcp.server.accept();
System.out.println("A client has connected");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("Welcome on the server... type the commands you like, type END to close the connection\n");
out.flush();
} catch (IOException exc) {
exc.printStackTrace();
}
if (socket != null && in != null && out != null) {
try {
String cmd = null;
while (!(cmd = in.readLine()).equals("END")) {
System.out.println("Recieved: " + cmd);
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader pRead = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder builder = new StringBuilder();
while ((line = pRead.readLine()) != null) {
builder = builder.append(line + "\n");
}
out.write(builder.toString() + "\n");
//here is sent "EnD"
out.write("EnD \n");
out.flush();
System.out.println(builder.toString());
pRead.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
System.out.println("Closing connection...");
try {
socket.close();
in.close();
out.close();
} catch (IOException excp) {
excp.printStackTrace();
}
}
}
}
}
}
And now the code for the client part
import java.net.*;
import java.io.*;
public class TCPCmdClient {
public Socket socket;
public int port;
public String ip;
public final String version = "Beta 1.0";
TCPCmdClient(String ip, int port) {
this.ip = ip;
this.port = port;
if (!createSocket())
System.out.println("Cannot connect to the server. IP: " + ip + " PORT: " + port);
else {
System.out.println("**********************************************");
System.out.println("Command executer, client version: " + version);
System.out.println("Connected to " + ip + ":" + port);
System.out.println("Code by luc99a alias L99");
System.out.println("**********************************************");
}
}
public boolean createSocket() {
try {
socket = new Socket(ip, port);
} catch (IOException e) {
return false;
}
return true;
}
public static void main(String[] args) {
TCPCmdClient client = new TCPCmdClient("127.0.0.1", 5000);
try {
BufferedReader sysRead = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(client.socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.socket.getOutputStream()));
String response = in.readLine();
System.out.println("Server: " + response);
boolean flag = true;
while (flag) {
System.out.println("Type a command... type END to close the connection");
String cmd = sysRead.readLine();
out.write(cmd + "\n");
out.flush();
if (cmd.equals("END")) {
client.socket.close();
sysRead.close();
in.close();
out.close();
flag = false;
} else {
//The loop doesn't finish because the reader
//listens for a new line
//so I used the string "EnD", sent by the server to
//stop the loop, anyway it doesn't seem to work
//I put a comment in the server where "EnD" is sent
String output;
while (((output = in.readLine()) != null)) {
if (output.equals("EnD")) {
break;
} else {
System.out.println(output);
}
}
System.out.println(" *************************************** ");
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
The problem is that the BufferedReader waits for a new line forever in the while loop (I wrote a comment in the code). I tryed to stop it using a "special string", but it doesn't seem to work.
I can't change the while in
String output;
while (((output = in.readLine()) != null) && output.length > 0)
{
//code here...
}
because in the output of the MS-DOS command (think on "ipconfig") are also present empty lines.
How could I correct it?
Thank you for your help!
your client Sends "EnD " (with a whitespace at the end) and you are comparing to "EnD" without a whitespace. So the two strings are not equal. try to send it without the white space:
out.write("EnD\n");
Space is missing. In TCPCmdClient.java change
if (output.equals("EnD")) {
to
if (output.equals("EnD ")) {

Java server-client readLine() method

I have a client class and a server class.
If client sends message to server, server will send response back to the client, then client will print all the messages it received.
For example,
If Client sends "A" to Server, then Server will send response to client
"1111". So I use readLine() in client class to read the message from server, then client print "1111" in the console.
If Client sends "B" to Server, then Server will send response to client
"2222\n 3333". So the expected printing output from client is:
"2222"
"3333"
So the response message from server to client may have 1 line or 2 lines depending on the message it send from client to server.
My question is that how I can use readLine() to read the message that send from server to client. More specifically, if I use the following codes,
String messageFromServer;
while(( messageFromServer = inputStreamFromServer.readLine()) != null) {
println(messageFromServer);
}
It will only print the first line, and will not print anything else even if I keep sending message from client to server, because readLine() will stops once it has read the first line.
update:
More specifically, I am looking for some methods in the client class to read message that contains 1 or multiple lines from server at a time. I am wondering if there are any ways to do it in client side if I don't want to change the format of the message that sent from server to client.
update 2
To make my question more clear, I will put some sample codes in the following:
This is server:
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(1234);
} catch (IOException e) {
System.err.println("Could not listen on port: 1234.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
}
System.out.println("Connected");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String textFromClient =null;
String textToClient =null;
textFromClient = in.readLine(); // read the text from client
if( textFromClient.equals("A")){
textToClient = "1111";
}else if ( textFromClient.equals("B")){
textToClient = "2222\r\n3333";
}
out.print(textToClient + "\r\n"); // send the response to client
out.flush();
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
The client:
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
try {
socket = new Socket("localhost", 1234);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
}
System.out.println("Connected");
String textToServer;
while((textToServer = read.readLine())!=null){
out.print(textToServer + "\r\n" ); // send to server
out.flush();
String messageFromServer =null;
while(( messageFromServer = textToServer=in.readLine()) != null){
System.out.println(messageFromServer);
}
}
out.close();
in.close();
read.close();
socket.close();
}
private static void debug(String msg)
{
System.out.println("Client: " + msg);
}
}
You shouldn't need to change the format of the data sent by the server, and readLine() should work, but I suspect that the server is not flushing or closing the OutputStream after writing the response which could possibly explain things.
Is the call to readLine() hanging? Are you in control of the server code? If so, can you include it?
Revised classes that work as I believe you expect:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ClientServerTest2
{
public static void main(String[] args) throws Exception
{
Thread serverThread = new Thread(new Server());
serverThread.start();
Thread clientThread = new Thread(new Client());
clientThread.start();
serverThread.join();
clientThread.join();
}
private static class Server implements Runnable
{
#Override
public void run()
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(1234);
Socket clientSocket = null;
clientSocket = serverSocket.accept();
debug("Connected");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String textFromClient = null;
String textToClient = null;
textFromClient = in.readLine(); // read the text from client
debug("Read '" + textFromClient + "'");
if ("A".equals(textFromClient))
{
textToClient = "1111";
}
else if ("B".equals(textFromClient))
{
textToClient = "2222\r\n3333";
}
debug("Writing '" + textToClient + "'");
out.print(textToClient + "\r\n"); // send the response to client
out.flush();
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void debug(String msg)
{
System.out.println("Server: " + msg);
}
}
private static class Client implements Runnable
{
#Override
public void run()
{
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
try
{
socket = new Socket("localhost", 1234);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
debug("Connected");
String textToServer;
textToServer = read.readLine();
debug("Sending '" + textToServer + "'");
out.print(textToServer + "\r\n"); // send to server
out.flush();
String serverResponse = null;
while ((serverResponse = in.readLine()) != null)
debug(serverResponse); // read from server and print it.
out.close();
in.close();
read.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private static void debug(String msg)
{
System.out.println("Client: " + msg);
}
}
Change while(( messageFromServer = inputStreamFromServer.readLine() != null) to while(( messageFromServer = inputStreamFromServer.readLine()) != null)
Actually this shouldn't even compile....
It's a work around.
If you want to send multiple strings like in your case : "2222\n 3333".
You can send them by adding a seperator character (like :) between two strings : "2222: 3333".
Then you can call write from server side as
clientOut.write("2222: 3333\n");
On client side parse recieved String :
messageFromServer = inputStreamFromServer.readLine();
String strArray[] = messageFromServer.split(":");
strArray[0] : 2222
strArray[0] : 3333

Categories