The server receives remote client commands .. commandos received are properly treated and then sent back to the client .. but the client can not read the output, nothing appears in out.println ("ServerMessage");
Customer must read the received data from the server ..
Could anyone help?
Thanks!
SERVER:
String MsG2X90aXk11 = "";
Thread.sleep(100);
while ((MsG2X90aXk11 = ShellZ.readLine()) != null) {
BufferedWriter writeCommand = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
String message = MsG2X90aXk11;
writeCommand.write(message+"\n");
writeCommand.flush();
CLIENT:
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String m6x7NXaxn102 = null;
while ((m6x7NXaxn102 = in.readLine()) != null) {
System.out.println(m6x7NXaxn102);
}
You are trying to print the line variable. While your variable that reads each line from the input stream is m6x7NXaxn102
Change:
System.out.println(line);
To:
System.out.println(m6x7NXaxn102);
Or:
Change:
while((m6x7NXaxn102 = in.readLine()) != null)
To:
while((line = in.readLine()) != null)
Hope this helps. :)
Related
I have trying to send multiple lines of code from a client to the server.
Here is the code on the server side
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
//read client input
//multi line
//https://stackoverflow.com/questions/43416889/java-filereader-only-seems-to-be-reading-the-first-line-of-text-document?newreg=2f77b35c458846dbb1290afce8853930
String line = "";
while((line =in.readLine()) != null) {
System.out.println(line);
}
System.out.println("is it here?");
Here is the code on the client side :
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
while (true) {
System.out.print("> ");
//content server input command (put, lamport clock, message)
String command = keyboard.readLine();
if (command.equals("quit")){
break;
}
//read from CSDB/txt1.txt
String message = readFileReturnString("CSDB/txt1.txt", StandardCharsets.UTF_8);
System.out.println(message);
//send to clientHandler through PrintWriter
out.println(command + " 3 \n" + message);
//receive response from ClientHandler (lamport clock)
String serverResponse = input.readLine();
System.out.println(serverResponse + socket);
}
Server side is able to print out all the text that is sent from the client side. However, the while loop doesn't break and System.out.println("is it here?"); has never been executed.
May I know why and how I can solve this problem please?
Your Client is waiting for some response of the Server. But the Server does not send any response. The Server writes to the System.out only. The Server has to write the response with the out.
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
//read client input
//multi line
//https://stackoverflow.com/questions/43416889/java-filereader-only-seems-to-be-reading-the-first-line-of-text-document?newreg=2f77b35c458846dbb1290afce8853930
String line = "";
while((line =in.readLine()) != null) {
System.out.println(line);
out.println(line); // send Server response
}
System.out.println("is it here?");
# talex
Then you need to tell server when it should exit the loop. Yo may send special string or something.
This works fine.
String line = "";
while((line =in.readLine()) != null) {
System.out.println(line);
if (line.equals("break") {
break;
}
}
geniuses.
I want to used socket in Java.
Here is a part of my server side code:
ServerSocket ss = new ServerSocket(this.portNum);
while (!ss.isClosed()) {
Socket socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("reading");
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("read");
System.out.println("writing");
bw.write(this.wsp.parse(new String(sb.toString())).toJSONString());
bw.newLine();
bw.flush();
System.out.println("wrote");
bw.close();
br.close();
socket.close();
}
ss.close();
And my client side (test) code is:
Socket socket = new Socket("143.248.135.60", 44450);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("writing");
bw.write(str);
bw.newLine();
bw.flush();
System.out.println("wrote");
System.out.println("reading");
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("read");
br.close();
bw.close();
socket.close();
Both sides halt after printing "reading."
What's wrong with my codes?
Thank you for your help in advance!
Your application blocks at the servers site at this point:
while ((line = br.readLine()) != null) {
sb.append(line);
}
Your servers will read lines until the source stream gets closed (br.readLine() will return null when the end of stream has been reached). But this doesn't happen. It seems that your're expecting just a single line here so try this instead of the loop at the server side:
System.out.println("reading");
String line = br.readLine();
System.out.println("read");
Now about the name loop on the client side: The server will close the streams and the socket immediately after it has written its own data. So br.readLine() will return null on the client side after the first line was read. So it will do what you're expecting. But it will also works if you're replacing the code as I've suggested it for the server side.
Hope it helps.
Edit based on the clarification of the question (need to read multiple lines):
The easiest way based on your work is to use a control character like "End of transmission" (0x04 on ASCII).
Client code:
System.out.println("writing");
bw.write("Hello");
bw.newLine();
bw.write("World");
bw.newLine();
bw.write(0x04); // EOT control character
bw.newLine(); // This is needed for BufferedReader/Writer - even if we've used a EOT
bw.flush();
System.out.println("wrote");
Continued in the next commend...
Server code:
while ((line = br.readLine()) != null && !(line.length() > 0 && line.charAt(0) == 0x04)) {
sb.append(line).append(System.lineSeparator());
}
If you're not using ASCII or UTF8 please review your used encoding to choose the correct control character.
I've been having a problem with using a while loop surrounding a BufferedReader in Java. I'm doing some experiments with Sockets.
My current code:
InetAddress address = InetAddress.getByName(this.IP);
SocketAddress socketaddress = new InetSocketAddress(address, this.port);
Socket socket = new Socket();
socket.connect(socketaddress);
if(socket.isConnected()){
Executor.logger.info("Connection to proxy established!");
}
else {
Executor.logger.warning("Connection to proxy failed!");
socket.close();
return;
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String temp2;
while((temp2 = in.readLine()) != null){
Executor.logger.info("Running query says " + temp2);
}
But no matter what I've tried the code will not progress to the next line. It does not spam my logger with anything, it just suddenly stops and gets stuck.
This is client-side only and I am not in control of the receiver server-side.
Am I doing something wrong?
Thanks for any help in advance.
Edits
The server is a command-line that accepts a command with variables and returns with a code that tells you the outcome of what you just did. However, when you first connect it returns something like message, blank line, message, blank line which is the loop currently getting stuck.
After testing I have found an alternative variable that can be used to detect whether the BufferedReader is full or not.
If you change the ((temp2 = in.readLine()) != null) from this code:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String temp2;
while((temp2 = in.readLine()) != null){
Executor.logger.info("Running query says " + temp2);
}
And use the boolean (in.ready()) instead:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("login " + this.user + " " + this.password);
out.flush();
String temp2;
while(in.ready()){
temp2 = in.readLine();
Executor.logger.info("Running query says " + temp2);
}
It will stop the loop when the BufferedRead has no more data to read and can be re-initialised again whenever necessary by copying the loop again.
my problem is that I don't know how to get verifier code from the redirected URL. The code is being echoed in the body.
HttpURLConnection igConnection = (HttpURLConnection) instagramURL.openConnection();
igConnection.setDoInput(true);
igConnection.setDoOutput(false);
if (igConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream is = igConnection.getInputStream();
String response = "";
if (is != null){
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer input = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
input.append(line);
}
in.close();
System.out.println(input.toString());
}
}
What I do get is beginning like <html>...
I just wanted to refresh the accessToken in case it expires in the future (checking).
My authorization link is correct according to this pattern
PrintWriter out = new PrintWriter(DoDSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(DoDSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println(fromServer);
if ((fromUser= stdIn.readLine()) != null)
{
//System.out.println(fromUser);
out.println(fromUser);
}
}
In this code for a client, I've created a Print Writer and a buffered reader which communicate with a Server, I also have a separate reader which reads the System.in from the command line.
My problem at the moment is that if the server send the client a multi line string, I will have to press enter to receive each line. How can I edit this code so that every line is printed from the buffered reader from the server, before it checks what the user has typed, rather than checking after every individual line?
Why not do one loop after the other?:
PrintWriter out = new PrintWriter(DoDSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(DoDSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println(fromServer);
}
while ((fromUser = stdIn.readLine()) != null)
{
System.out.println(fromUser);
}