论坛首页 Java企业应用论坛

关于telnet 程序从一个平台跳转另外一个平台的问题解决思考。

浏览 4436 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-28  
首先感谢llade的提示其实问题就是一个线程的同步问题。描述:在正常情况下在本程序向telnet服务端发送命令,服务端返回一个结果然后本程序分析该结果是否正确,并确认服务端是否运行上条命力正常结束。我的问题就是原先的程序未得对方运行完毕就一股脑的把所有命令发送过去。这是错误的。改正的方法就是加了一个检测函数来控制当前命令是否正常结束。在此感谢llade的提示。这里本来使用多线程的后经过修改去掉了。现把有问题的程序代码和没问题的代码都贴出来。
有问题:
package tmh.tvs.util.net.telnet;   
  
import org.apache.commons.net.telnet.*;   
  
import java.io.*;   
import java.net.SocketException;   
import java.util.StringTokenizer;   
  
public class TmhTelnet implements Runnable, TelnetNotificationHandler {   
  
    private static TelnetClient tc = null;   
    private InputStream in;   
    private PrintStream out;   
    public static void main(String args[]){        
        Thread t = new Thread(new TmhTelnet("125.46.86.123",23,"*****","******"));   
        t.start();   
    }   
    public TmhTelnet(String host, int port,String username,String password) {   
        if(intconnect(host,port)){   
            write(username);   
            write(password);   
            writescript("command.txt");   
        }   
    }   
  
    private void writescript(String filename) {   
        try {   
            if(new File(filename).exists()){   
                FileReader f = new FileReader(filename);   
                BufferedReader reader = new BufferedReader(f);   
                String command = "";   
                while(command !=null){   
                    command = reader.readLine();   
                    write(command);   
                }   
            }   
        } catch (FileNotFoundException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        } catch (IOException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
           
           
    }   
    private void write(String command) {   
        try{   
            System.out.println("command:>>>>>>>>>"+command);   
            out.println(command);   
            out.flush();               
        }catch(Exception e){   
            e.printStackTrace();   
        }   
    }   
  
    private boolean intconnect(String host, int port) {   
        try {   
            tc = new TelnetClient();   
            TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(   
                    "VT100", false, false, true, false);   
            EchoOptionHandler echoopt = new EchoOptionHandler(true, false,   
                    true, false);   
            SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,   
                    true, true, true);   
  
            tc.addOptionHandler(ttopt);   
            tc.addOptionHandler(echoopt);   
            tc.addOptionHandler(gaopt);   
            tc.connect(host, port);   
            in = tc.getInputStream();   
            out = new PrintStream(tc.getOutputStream());   
            return true;   
        } catch (Exception e) {   
            e.printStackTrace();   
            try {   
                tc.disconnect();   
            } catch (IOException e1) {   
                // TODO Auto-generated catch block   
                e1.printStackTrace();   
            }   
            return false;   
        }          
    }   
       
    public void receivedNegotiation(int negotiation_code, int option_code) {   
        String command = null;   
        if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {   
            command = "DO";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {   
            command = "DONT";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {   
            command = "WILL";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {   
            command = "WONT";   
        }   
        System.out.println("Received " + command + " for option code "  
                + option_code);   
    }   
  
    /***************************************************************************  
     * Reader thread. Reads lines from the TelnetClient and echoes them on the  
     * screen.  
     **************************************************************************/  
    public void run() {   
        InputStream instr = tc.getInputStream();   
  
        try {   
  
            byte[] buff = new byte[1024];   
            int ret_read = 0;   
            do {   
                ret_read = instr.read(buff);   
                if (ret_read > 0) {   
                    System.out.print(new String(buff, 0, ret_read));   
                }   
  
            } while (ret_read >= 0);   
        } catch (Exception e) {   
            System.err.println("Exception while reading socket:"  
                    + e.getMessage());   
        }   
  
        try {   
            tc.disconnect();   
        } catch (Exception e) {   
            System.err.println("Exception while closing telnet:"  
                    + e.getMessage());   
        }   
    }   
}  


可以正常实现功能的代码
package tmh.tvs.util.net.telnet;

import org.apache.commons.net.telnet.*;

import java.io.*;

public class TmhTelnet    {

	private static TelnetClient tc = null;
	private InputStream in;
	private PrintStream out;
	public static void main(String args[]){	
		new TmhTelnet("125.46.86.123",23,"***","***");
	}
	public TmhTelnet(String host, int port,String username,String password) {
		byte [] a = new byte[1024];
		if(intconnect(host,port)){			
			writescript("command.txt");
			closeconnect();
			
		}
	}

	private void writescript(String filename) {
		try {
			if(new File(filename).exists()){
				FileReader f = new FileReader(filename);
				BufferedReader reader = new BufferedReader(f);
				String command = "";
				while(command !=null){
					command = reader.readLine();
					write(command);
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public  void  write(String command) {
		try{
			System.out.println("command:>>>>>>>>>"+command);
			out.println(command);
			out.flush();
			printresponse();
			//for(int i=0;i<800000000;i++);
			//应该使用线程同步使程序在接受到对方相应后再执行下一条命令这里使用for循环来模拟对方的相应时间	
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private boolean intconnect(String host, int port) {
		try {
			tc = new TelnetClient();
			TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
					"VT100", false, false, true, false);
			EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
					true, false);
			SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,
					true, true, true);

			tc.addOptionHandler(ttopt);
			tc.addOptionHandler(echoopt);
			tc.addOptionHandler(gaopt);
			tc.connect(host, port);
			in = tc.getInputStream();
			out = new PrintStream(tc.getOutputStream());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				tc.disconnect();
				in.close();
				out.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return false;
		}		
	}
	private void closeconnect(){
		try {
			tc.disconnect();
			in.close();
			//out.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	private String printresponse(){
		try {

			byte[] buff = new byte[1024];
			int ret_read = 0;
			do {
				ret_read = in.read(buff);
				String a = new String(buff, 0, ret_read);
				if (a.endsWith(":")|a.endsWith(">")|a.endsWith("]")) {
					System.out.print(a);
					return null;
				}

			} while (ret_read >= 0);
		} catch (Exception e) {
			System.err.println("Exception while reading socket:"
					+ e.getMessage());
		}

		try {
			tc.disconnect();
		} catch (Exception e) {
			System.err.println("Exception while closing telnet:"
					+ e.getMessage());
		}
		return null;
	}
	
}



另外感谢抓包工具,是他让我彻底明白了问题的实质,抓包工具真伟大。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics