不好意思,重贴一下:import java.net.*;
import java.io.*;public class JTelnetC {
Socket sock; 
public JTelnetC(String h, int p){
try
{
sock = new Socket(h, p);
new InputPipe(sock.getOutputStream()).start();
new OutputPipe(sock).start();
}
catch(IOException e)
{
System.out.println(e);
return;
}
}
public class InputPipe extends Thread {
DataInputStream is;
PrintStream     os;
public InputPipe(OutputStream os)
{
this.is = new DataInputStream(System.in);
this.os = new PrintStream(os);
}
public void run() 
{
String line;
try 
{


while(true) 
{
line = is.readLine();
os.print(line);
os.print("\r\n");
os.flush();
}

catch(IOException e) 
{
throw new RuntimeException(e.getMessage());
}
}
}
public class OutputPipe extends Thread {
InputStream     in;
OutputStream    ot;
PrintStream     os;
public OutputPipe(Socket sock)
{
try
{
this.ot = sock.getOutputStream();
this.in = sock.getInputStream();
this.os = new PrintStream(System.out);
}
catch(IOException e)
{
System.out.println(e);
}
}

public void run()
{
String line;
int    i;
try
{
while(true) 
{
i = in.read();
if (i == 255) 
{
int i1 = in.read();
int i2 = in.read();
tel_net(i1,i2);
}
else
{
os.print((char)i);
os.flush();
}
}

catch(IOException e)
{
throw new RuntimeException(e.getMessage());
}
}
private void tel_net(int i1, int i2)
{
int i = i1;
if (i == 253)
{
i = i2;
if (i == 1)  wont(i);
else if (i == 24) wont(i);
else if (i == 31) wont(i);
else if (i == 35) wont(i);
else if (i == 36) wont(i);
else if (i == 39) wont(i);
}
}
private void wont(int i)
{
try 
{  
ot.write(255);
ot.write(252);
ot.write(i);
}
catch (IOException e)
{
System.out.println(e);
}
}

public static void main(String args[]) 
{
// String host = "bbs.dlut.edu.cn";
String host = "202.118.66.5";
int    port = 23;
new JTelnetC(host, port);
}
}

解决方案 »

  1.   

    我怀疑是你的协议处理方面出了问题:
    仔细审查一下,及相关函数的代码
    ...
    if (i == 255) 
    {
    int i1 = in.read();
    int i2 = in.read();
    tel_net(i1,i2);
    ... } 我这里没有协议资料,不便多言通信方面应该没有什么问题
      

  2.   

    不好意思,JAVA初学者,不大懂,错误是没有的,现在就是运行以后一点信息都没有返回,很郁闷,不知该怎么弄,怎么继续与TELNET服务器交互。哪位热心大虾帮忙看看,其实在编辑器里挺短的,到这里就显得这么长了。