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..

[2913 byte] By [vaibhavpinglea] at [2007-10-2 9:04:37]
# 1

I posted a simple Telnet client that runs a command on a Unix server. You can find it in reply 7 of [url=http://forum.java.sun.com/thread.jspa?forumID=31&threadID=437231]Telnet to Unix box from Java app[/url].

If all you need is to run a simple command it should be sufficient. Otherwise you might want to take a look at [url=http://jakarta.apache.org/commons/net/]Jakarta commons Net[/url]

pkwoostera at 2007-7-16 23:11:38 > top of Java-index,Archived Forums,Socket Programming...