Hi All,
i've a problem in my application: the client must be connect (using SSL Sockets) to the server, and, like a common telnet client or netcat, send/reicive commands and responses from/to the server. Now, when the client is connected to the server and send a simple command like LS (implemented in the server side) print out to video only the first line of the output. How can i do to print out to video the complete response of the server?
These are the sources codes:
Main.java:
package jlc;
import java.io.*;
public class Main {
public static void main(String [] args) throws IOException, Exception {
try {
if(args[0] != null && args[1] != null) {
int ArgsParsed = Integer.parseInt(args[1]);
String command = "";
boolean finito = false;
Client client = new Client();
client.connect(args[0], ArgsParsed);
System.out.println("\nConnected to " + args[0] + " port: " + ArgsParsed + "; your IP is: " + client.address());
String welcome = client.read();
if(welcome != null) {
System.out.println(welcome);
}
while (!finito) {
InputStreamReader tastiera = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(tastiera);
command = buf.readLine();
if (command.equals("ls")) {
client.send((command + "\r").getBytes());
System.out.println(client.read().getBytes());
} else if (command.equals("help")) {
client.send((command + "\r").getBytes());
System.out.println(client.read().getBytes());
} else if (command.equals("exit")) {
client.send((command + "\r").getBytes());
client.read();
client.close();
finito = true;
System.exit(0);
}
}
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\nJavaLightClient 0.2 console version\nUsage:\n\tjava JavaLightClient [IP] [port]");
}
}
}
Client.java:
package jlc;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
class Client {
public Client(SSLSocket s) throws IOException {
this.initialized = true ;
this.sslsocket = s;
this.input = this.sslsocket.getInputStream();
this.output = this.sslsocket.getOutputStream();
}
public Client() {
this.initialized = false ;
this.sslsocket = null;
}
public String address() {
return this.sslsocket.getInetAddress().toString();
}
public void connect( String host, int port) throws IOException {
if( !this.initialized )
this.sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
this.input = this.sslsocket.getInputStream();
this.output = this.sslsocket.getOutputStream();
}
public void send(byte[] byteStream) throws IOException {
this.output.write(byteStream);
this.output.flush();
}
public String read() throws IOException{
String buffer = "";
byte b;
while( (b = (byte)this.input.read()) != (byte)'\n' ) {
buffer += (char)b;
}
return buffer.trim();
}
public void close() throws Exception {
this.input.close();
this.output.close();
}
private SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
private SSLSocket sslsocket = null;
private InputStream input ;
private OutputStream output ;
private boolean initialized = false ;
}
To run it i use this command:
java -jar -Djavax.net.ssl.trustStore=myKeystore -Djavax.net.ssl.trustStorePassword=123456 JLC.jar IP PORT
where myKeystore is an RSA keystore generated using keytool and '123456' the password of that keystore. I've used NetBeans (6.1) to compile it.
Thanks,
Bye
hawake