2 years ago
#21036
Oside
Why is InputStreamReader not ready to read data (I am relatively new to java networking so this is probably a simple issue)
I am having some trouble with Input Stream reader as its never ready to receive data from my client script
This is my server side code (There are a lot of variables and things that are not used but there going to be implemented later) also I Apologize about the formatting of the code I don't really use stack overflow that often so I don't know how to format that well.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Stream {
public static void main(String[] args) throws Exception {
InputStreamReader input = null;
OutputStreamWriter output = null;
ServerSocket serverSocket = null;
Socket ss = null;
BufferedReader buffIn = null;
BufferedWriter buffOut = null;
serverSocket = new ServerSocket(700);
try {
ss = serverSocket.accept();
System.out.print("new client recieved: " + ss);
input = new InputStreamReader(ss.getInputStream());
output = new OutputStreamWriter(ss.getOutputStream());
buffIn = new BufferedReader(input);
buffOut = new BufferedWriter(output);
} catch (IOException e) {
e.printStackTrace();
}
**System.out.print(input.ready());
while (!buffIn.readLine().equalsIgnoreCase("Button")){**
try {
System.out.print("here");
System.out.println("msg from client: " + buffIn.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
try {
System.out.print("close");
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The bolded part of the code is where the errors are occurring, the code never progresses past that while loop because the input stream reader is never ready to read data. I know this because the system out print always says false.
I'm not sure if this is relevant but just in case I will post the client side code (once again there are some unused vars)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception {
Socket socket = null;
InputStreamReader input = null;
OutputStreamWriter output = null;
BufferedReader buffIn = null;
BufferedWriter buffOut = null;
socket = new Socket("localhost", 700);
try {
input = new InputStreamReader(socket.getInputStream());
output = new OutputStreamWriter(socket.getOutputStream());
buffIn = new BufferedReader(input);
buffOut = new BufferedWriter(output);
} catch (IOException e) {
e.printStackTrace();
}
Scanner src = new Scanner(System.in);
String msg = src.nextLine();
while(!buffIn.readLine().equalsIgnoreCase("Button")){
try {
buffOut.write(msg);
buffOut.newLine();
buffOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java
sockets
networking
server
inputstreamreader
0 Answers
Your Answer