-
October 11th, 2016, 01:28 PM
#1
Can't write input to stdin in command line although I can on Eclipse
I wrote a Server-Client model program in java, and it works great on eclipse. However, when I try to run the client via command line (in Windows 10), I can see the input that I type on my keyboard for stdin only after I press enter. Why is that, and how can I solve this?
Here is the client code (not the best one, I know):
Code:
public class MyEchoClient {
public static void main(String[] args) throws IOException{
Socket clientSocket = null; // the connection socket
PrintWriter out = null;
BufferedReader in = null;
String host = args[0], msg, tmp;
int port = Integer.decode(args[1]).intValue(), len;
System.out.println("Connecting to " + host + ":" + port);
try { // Trying to connect to a socket and initialize an output stream
clientSocket = new Socket(host, port); // host and port
out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"), true);
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + host);
System.exit(1);
} catch (IOException e) {
System.out.println("Couldn't get output to " + host + " connection");
System.exit(1);
}
try { // Initialize an input stream
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));
} catch (IOException e) {
System.out.println("Couldn't get input to " + host + " connection");
System.exit(1);
}
System.out.println("Connected to server!");
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
try{
while(true){
while(!userIn.ready()){
if (in.ready()){ // if server has something to write, when client does'nt have something to write
tmp=in.readLine();
len=tmp.length();
System.out.print(tmp);
if ((len>0 && tmp.charAt(tmp.length()-1)=='>') || tmp.contains("UNIDENTIFIED"))
System.out.println();
}
}
System.out.println();
msg = userIn.readLine();
if (msg==null) { break; }
out.println(msg);
tmp=in.readLine();
if (tmp.contains("~")) { break; }
System.out.println(tmp);
}
}
catch(IOException e){ System.out.println("Error with server connection."); }
System.out.println("Exiting...");
out.close();
in.close();
userIn.close();
clientSocket.close();
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|