cannot receive data from telnet - returns ioexception on read either by ...
Well, i need to connect to a telnet port.
After we have connected (just the socket connection), the telnet responds to us.
I need to record this response.
i tried to use both buffered input, as well as inputstream, but i fail to read, as it throws an ioexception.
Also i tried to find out the number of bytes that are responded by the telnet, surprisingly, it says that 21 bytes have been responded.
Here is my code :
public class TelnetClient implements Runnable {
private String userName, password;
private String cmdString = "ls", line;
private Socket telnetSocket;
private byte buff[];
private BufferedReader br;
private InputStream inStream;
private OutputStream outStream;
private InetAddress ipAddress;
private int port = 23;
private MainMenu mm;
private boolean sessionClosed = false;
/** Creates a new instance of TelnetClient */
public TelnetClient() {
}
public TelnetClient(String ipAdd, String uName, String pass) {
try {
this.ipAddress = InetAddress.getByName(ipAdd);
this.userName = uName;
this.password = pass;
}catch(UnknownHostException uhe) {
System.out.println("Cannot Reach to port : " + port);
}
}
public void run() {
try {
int i;
telnetSocket = new Socket(ipAddress, port);
telnetSocket.setSoTimeout(5000);
System.out.println("Established connection to port : " + port);
outStream = telnetSocket.getOutputStream();
inStream = telnetSocket.getInputStream();
br = new BufferedReader(new InputStreamReader(telnetSocket.getInputStream()));
//Thread.sleep(5000);
//outStream.write(userName.getBytes());
//Thread.sleep(5000);
//outStream.write(password.getBytes());
//Thread.sleep(5000);
//buff = cmdString.getBytes();
//outStream.write(buff);
Thread.sleep(5000);
int count = inStream.available();
System.out.println("Number of Bytes Replied : "+count);
//while((i = inStream.read()) != -1) {
//while((line = br.readLine()) != null) {
inStream.read(buff);
System.out.print(buff);
//}
//}
telnetSocket.close();
}catch(UnknownHostException uhe) {
System.out.println("No Connection to port : " + port);
}catch(IOException ioe) {
System.out.println("Cannot Read from port : " + port);
ioe.printStackTrace();
}
catch(InterruptedException ie) {
System.out.println("Thread Interrupted !");
}
}
}
Please help me out, its really urgent and i need to sort this out quickly.
At present this port is on my PC and later i have to make such a connection to a remote unix server thu this application.
I am running a Windows Machine. and using netbeans IDE for development.
Thank You..

