下面是一个实现Telnet功能的例子,很简单,基本Socket编程的东东 import java.net.*; import java.io.*;/** * Telnet - connect to a given host and service * This does not hold a candle to a real Telnet client, but * shows some ideas on how to implement such a thing. * @version $Id: Telnet.java,v 1.9 2002/03/29 15:00:49 ian Exp $ */ public class Telnet { String host; int portNum; public static void main(String[] argv) { new Telnet().talkTo(argv); } private void talkTo(String av[]) { if (av.length >= 1) host = av[0]; else host = "localhost"; if (av.length >= 2) portNum = Integer.parseInt(av[1]); else portNum = 23; System.out.println("Host " + host + "; port " + portNum); try { Socket s = new Socket(host, portNum); // Connect the remote to our stdout new Pipe(s.getInputStream(), System.out).start(); // Connect our stdin to the remote new Pipe(System.in, s.getOutputStream()).start(); } catch(IOException e) { System.out.println(e); return; } System.out.println("Connected OK"); } /* This class handles one half of a full-duplex connection. * Line-at-a-time mode. Streams, not writers, are used. */ class Pipe extends Thread { DataInputStream is; PrintStream os; // Constructor Pipe(InputStream is, OutputStream os) { this.is = new DataInputStream(is); this.os = new PrintStream(os); } // Do something method public void run() { String line; try { while ((line = is.readLine()) != null) { os.print(line); os.print("\r\n"); os.flush(); } } catch(IOException e) { throw new RuntimeException(e.getMessage()); } } }}
有一个方案,利用socks通信。
1.new sockt(hostip)
2.利用inputstream=sockt.getinputstream(),等把socks流与你的输入输出结合。
3.发送相应的指令就可以完成了。
import java.io.*;
//import java.util.*;public class Telnet
{
protected InputStreamReader rdr;
protected OutputStreamWriter wrt;
protected Socket sock;
private boolean crflag=false;
public Telnet(String host,int port) throws IOException
{
TelnetInit(host,port);
}
public void TelnetInit(String host,int port) throws IOException
{
sock = new Socket(host,port);
rdr=new InputStreamReader(sock.getInputStream());
wrt=new OutputStreamWriter(sock.getOutputStream());
}
public static void main(String args[]) throws Exception
{
Telnet telnet = new Telnet(yourip,23);
。。
}
public void send(int c)throws IOException
{
if(c=='\n') wrt.write('\r');
wrt.write(c);
if(c=='\r') wrt.write(0);
wrt.flush();
}
public void send(String s) throws IOException
{
System.out.println("begin send String");
for(int i=0;i<s.length();i++)
{
int c=(int)s.charAt(i);
send(c);
System.out.write(c);
}
}
protected int getChar()
{
try
{
int c;
if(crflag)
{
crflag=false;
c=rdr.read();
if(c!=0) return c;
}
c=rdr.read();
if(c=='\r')crflag=true;
return c;
//c=rdr.read();
//if(c!=0) return c;
//return(-1);
}
catch(IOException e)
{
System.out.println(e);
return(-1);
}
}
private void sendc(int c)
{
try
{
wrt.write(c);
wrt.flush();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
import java.net.*;
import java.io.*;/**
* Telnet - connect to a given host and service
* This does not hold a candle to a real Telnet client, but
* shows some ideas on how to implement such a thing.
* @version $Id: Telnet.java,v 1.9 2002/03/29 15:00:49 ian Exp $
*/
public class Telnet {
String host;
int portNum;
public static void main(String[] argv) {
new Telnet().talkTo(argv);
}
private void talkTo(String av[]) {
if (av.length >= 1)
host = av[0];
else
host = "localhost";
if (av.length >= 2)
portNum = Integer.parseInt(av[1]);
else portNum = 23;
System.out.println("Host " + host + "; port " + portNum);
try {
Socket s = new Socket(host, portNum); // Connect the remote to our stdout
new Pipe(s.getInputStream(), System.out).start(); // Connect our stdin to the remote
new Pipe(System.in, s.getOutputStream()).start(); } catch(IOException e) {
System.out.println(e);
return;
}
System.out.println("Connected OK");
} /* This class handles one half of a full-duplex connection.
* Line-at-a-time mode. Streams, not writers, are used.
*/
class Pipe extends Thread {
DataInputStream is;
PrintStream os; // Constructor
Pipe(InputStream is, OutputStream os) {
this.is = new DataInputStream(is);
this.os = new PrintStream(os);
} // Do something method
public void run() {
String line;
try {
while ((line = is.readLine()) != null) {
os.print(line);
os.print("\r\n");
os.flush();
}
} catch(IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}}
中文版RFC可以去http://www.china-pub.com/computers/emook/emooknew/RFC/rfc.asp找
虽然不全,但是也能找到很多文档的,前提是你知道哪些号码对应的是和telnet有关的
估计和很多高层协议在一个范围
还是想修改这个协议为我所用呢?因为telnet是个使用极为广泛的协议,那是在很多高层和更高层的协议推出以前
用telnet扩展自己的协议
有好几个文件,70k,你要的话可以给我消息。
不过要记得给我加分。