NVTPriter sinf kodi
class NVTPrinter extends Thread {
NVTInputStream inStream;
public NVTPrinter(NVTInputStream in){
//super();
inStream=in;
}
public void run(){
boolean finished=false;
try{
do{
int i=inStream.read();
if(i==-1){
finished=true;
}
else{
System.out.print((char)i);
System.out.flush();
yield();
}
//System.out.println("\n");
}while(!finished);
System.out.println("The Connection is aborted....");
System.exit(1);
}catch(IOException e){
System.out.println("Network Virtual Terminal Error");
}}
NVTOutputStream klassi
class NVTOutputStream extends FilterOutputStream{
int IAC=255;
byte CR=13;
byte LF=10;
public NVTOutputStream(OutputStream output){
super(output);
}
public void write(int i) throws IOException{
super.write(i);
}
public void println(String s){
try{
byte [] bytes=s.getBytes();
for(int i=0;i
super.write(bytes[i]);
}
super.write(CR);
super.write(LF);
super.flush();
}catch(IOException e){
System.out.println(" Input output Error");
} } }
NVTInputStream klassi
class NVTInputStream extends FilterInputStream{
byte IAC=(byte)0xFF;
byte DO=(byte)0xFD;
byte WILL =(byte)0xFB;
byte CR=13;
byte LF=10;
int wont=252;
int dont=254;
int bufferSize=1024;
OutputStream out;
byte [] linebuf=new byte[bufferSize];
int numBytes=0;
public NVTInputStream(InputStream inStream,OutputStream outStream){
super(inStream);
out=outStream;
}
public int read () throws IOException{
boolean recIAC;
int i;
do{
recIAC=false;
i=in.read();
if(i==-1) {
return i;
}
else if(IAC==i){
recIAC=true;
int cmd=in.read();
if(cmd==-1){
return cmd;
}
else{
byte b2=(byte)cmd;
if(b2==IAC){
return 255;
}
if(b2==WILL){
int opt=in.read();
if(opt==-1){
return opt;
}
out.write(255);
out.write(dont);
out.write(opt);
out.flush();
}
if(b2==DO){
int opt=in.read();
if(opt==-1){
return opt;
}
out.write(255);
out.write(wont);
out.write(opt);
out.flush();
} } }
}while(recIAC); return i; }
public String readLine() throws IOException {
numBytes=0;
boolean finished=false;
do{
int i=read();
if(i==-1)
return null;
else{
byte b=(byte)i;
if(b==LF){
if(numBytes>0){
if(linebuf[numBytes-1]==13){
return new String(linebuf,0,numBytes-1);
} } }
linebuf[numBytes]=b;
++numBytes;
}
}while(!finished);
return null; } }
Port klassi
class port extends Thread{
Socket con;
OutputStream out;
NVTInputStream in;
NVTPrinter printer;
public port(String [] args){
System.out.println("length"+args.length);
if(args.length!=2){
System.out.println("Use: Java Telnet host port");
System.exit(2);
}
String destination=args[0];
int port =0;
port=Integer.parseInt(args[1]);
System.out.println("Connecting to "+destination +" on " +port);
try{
con=new Socket(destination,port);
}catch(UnknownHostException e){
System.out.println("Unknown Destination..Try again...");
System.exit(3);
}
catch(IOException e){
System.out.println("Error While Creating the Socket");
System.exit(3);
}
try{
out=con.getOutputStream();
in=new NVTInputStream(con.getInputStream(),out);
}catch(IOException e){
System.out.println("Input Output Error...");
System.exit(3);
}
System.out.println("Connection Established..");
}
public void ProcessInput(){
try{
String line;
boolean finished=false;
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
do{
line=read.readLine();
if(line==null){
finished=true;
}else{
try{
for(int i=0;iout.write(line.charAt(i));
out.write('\n');
}
}catch(IOException e){
System.out.println("Input Output Error...");
System.exit(3);
}
}
}while(!finished);
} catch(IOException e){
System.out.println("Input Output Error...");
System.exit(3);
}
}
public void shutdown(){
try{
con.close();
}catch(IOException e){
System.out.println("Error Closing the Socket");
System.exit(3);
}
}
public void run(){
printer =new NVTPrinter(in);
printer.start();
yield();
this.ProcessInput();
shutdown();
}
}
Telnet sinf namunasi
//This is a sample Telnet app coded using java
import java.net.*;
import java.rmi.UnknownHostException;
import java.io.*;
Public class Telnet {
public static void main(String [] str){
for(int i=0;i
System.out.println("<<>>"+str[i]);
}
port p=new port(str);
p.start();
}
}
Do'stlaringiz bilan baham: |