telnet \socket \ httpproxy 三种客户端实现

1.socket实现方式:

public class TestMonitor {

	public static void main(String[] args) throws IOException {

        Socket socket = null;
       // PrintWriter out = null;
        BufferedReader in = null;
        PrintStream ops= null;
        try {
        	socket = new Socket(args[0], 4444);//参数为服务器的ip地址
        	ops=new PrintStream(socket.getOutputStream());
        	ops.println(args[1]); // 参数是发送到服务器上的请求信息
        	ops.flush();   
//            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host:" + args[0]);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;

//        String fromUser = stdIn.readLine();
//        System.out.println("Client: " + fromUser);
//        out.println(fromUser);
        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;
            String fromUser = stdIn.readLine();
		    if (fromUser != null) {
	                System.out.println("Client: " + fromUser);
	                ops.println(fromUser);
		    }
        }

        ops.close();
        in.close();
        stdIn.close();
        socket.close();
    }
}


2.telnet 实现方式
这个要加个jar包: commons-net-2.0.jar

public class TestTelnet {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TelnetClient tc=new TelnetClient();
		PrintStream ops= null;
		 BufferedReader in = null;
		try {
			tc.connect("172.16.100.87", 4444);//连接的服务器和端口
			System.out.println("test cennection:"+tc.isConnected());		
			ops=new PrintStream(tc.getOutputStream());
			ops.println("/refreshapps"); // 是发送到服务器上的请求信息

			ops.flush();   
			 in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
		  // System.out.println( "AAA"+in.readLine());		
			char[] bs=new char[256];
			int i=0;
		
				while((i=in.read(bs))>-1){
					String str=null;
					if(i==256){
						str=new String(bs);
					}else{
						char[] bs2=new char[i];
						for(int j=0;j<i;j++)
						{
							bs2[j]=bs[j];
						}
						str=new String(bs2);
					}

					System.out.println(str);
				}
				
		} catch (SocketException e) {			
			e.printStackTrace();
		} catch (IOException e) {		
			e.printStackTrace();
		}
		
	}
	

}


3.http proxy 实现方式



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class TestProxy {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// String strUrl="http://blog.csdn.net/cqq/";
		// URL url = new URL(strUrl);
		URL url = new URL("http", "172.16.100.87", 4444, "");//所请求服务器的ip地址和端口
		URLConnection conn = url.openConnection();

		String strProxy = "172.16.10.133";//代理服务器的ip
		String strPort = "4444";//端口
		Properties systemProperties = System.getProperties();
		systemProperties.setProperty("http.proxyHost", strProxy);
		systemProperties.setProperty("http.proxyPort", strPort);

		BufferedReader rd = new BufferedReader(new InputStreamReader(conn
				.getInputStream()));
		String ss = null;
		while ((ss = rd.readLine()) != null) {
			System.out.println(ss);
		}
		rd.close();

	}

}

你可能感兴趣的:(java,.net,socket,Blog,J#)