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();

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值