java telnet实现问题

[复制链接]
查看11 | 回复2 | 2011-5-7 01:45:08 | 显示全部楼层 |阅读模式
代码如下:
*************************************************************************
package network;
import java.net.Socket;
import java.net.InetAddress;
import java.io.*;
import java.nio.ByteBuffer;
/**
* Created by IntelliJ IDEA.
* User: franck
* Date: 2007-4-25
* Time: 16:57:07
* To change this template use File | Settings | File Templates.
*/
public class test_telnet extends Thread {
Socket s = null;
BufferedReader reader = null;
InputStream in = null;
OutputStream out = null;
BufferedReader usr = null;
PrintWriter writer = null;
request req = null;
String host = null;
int port;
public test_telnet(String host, int port) {
this.host = host;
this.port = port;
}
public void run() {
try {
//
String host = "bbs.fudan.edu.cn";
//
s = new Socket(host, 23);
//
String host = "ftp.hjenglish.com";
//
s = new Socket(host, 21);

s = new Socket(host, port);

String line = "";

String usrReply = "";

//String startStr = "请输入帐号(试用请输入'guest', 注册请输入'new'):";

usr = new BufferedReader(new InputStreamReader(System.in));

reader = new BufferedReader(new InputStreamReader(s.getInputStream()));

writer = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

in = s.getInputStream();

out = s.getOutputStream();

System.out.println(s.toString());

req = new request(in, this);

req.start();

//
int totalBinary = -1;

while (true) {

if ((line = usr.readLine().trim()) != null) {

//System.out.print(line);

byte[] binary = (line + "\r\n&quot

.getBytes();
//
writer.print(line+"\n&quot

;

out.write(binary);

out.flush();

if (line.equals("quit&quot

)

break;

}

synchronized (this) {

wait(50);

}

}

writer.close();

usr.close();

reader.close();

in.close();

out.close();

s.close();
} catch (Exception e) {

e.printStackTrace();
} finally {

try {

s.close();

} catch (IOException e) {

e.printStackTrace();

}
}
}
public static void main(String[] args) {
if (args.length == 2) {

String host = args[0];

int port = Integer.parseInt(args[1]);

test_telnet test = new test_telnet(host, port);

test.start();
}
}
class request extends Thread {
//BufferedReader br = null;
InputStream rin = null;
test_telnet telnet = null;
public request(/*BufferedReader r,*/ InputStream in, test_telnet tt) {

//this.br = r;

this.rin = in;

this.telnet = tt;
}
public void run() {

try {

int totalBinary = -1;

String line = "";

InputStreamReader r = new InputStreamReader(in);

while (true) {

if (telnet.s.isClosed()) break;

if ((totalBinary = in.available()) > 0) {

byte[] binary = new byte[totalBinary];

in.read(binary);

line = new String(binary,"gb2312&quot

;

System.out.print(line);

}

synchronized (this) {

wait(20);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

}
}

}
}
**************************************************************************
我启动程序可登陆bbs.fudan.edu.cn,但是我登陆公司的服务器是就只打印出??$这三个字符后就什么反应都没有了,我通过debug调试发现这三个byte分别是[0]=-1 [1]=-3 [2]=36,不知什么原因,但是使用dos telnet上去就不会出现这些问题,屏幕会直接提示要求输入用户名和密码,这是什么原因?请大家指导。
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
公司把ttelnet端口封了吧
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
但是我用cmd.exe是可以telnet上去的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行