博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Simple Proxy Server (Java)
阅读量:5940 次
发布时间:2019-06-19

本文共 4785 字,大约阅读时间需要 15 分钟。

hot3.png

因为对java的network programmiing不太了解,所以就试着写个simple proxy server练练手。

虽然问题很多,而且处理简直就可以说是“没有处理”。但是,还是可以通过这个代理浏览网页,并且统计有多少流量流出和流入。代码如下。(这篇帖子就是同个这个proxy发的)。

 

1. 将serverSocket.close()改成if (serverSocket!=null) serverSocket.close();后,异常打印少了很多。没有null pointer exception了。

 2. 将第一次从浏览器读取的request的count进行判断,如果<=0,就直接关掉socket;如果大于0,才进行其他处理。浏览网页速度变快了。不知道为什么。

package network.proxy;import java.io.*;import java.net.*;import java.util.*;// internet server <--serverSocket--> proxy <--clientSocket--> client                                                                                                   public class MyProxy {    public static void main(String[] args) {        try {            new Thread(new SimpleProxyServer(8989)).start();        } catch (IOException e) {            e.printStackTrace();        }    }}class SimpleProxyServer implements Runnable {    private ServerSocket listenSocket;    public SimpleProxyServer(int port) throws IOException {        this.listenSocket = new ServerSocket(port);    }    public void run() {        for (;;) {            try {                Socket clientSocket = listenSocket.accept();                System.out.println("Create a new Thread to handle this connection");                new Thread(new ConnectionHandler(clientSocket)).start();            } catch (IOException e) {                e.printStackTrace();            }        }    }}class ProxyCounter {    static int sendLen = 0;    static int recvLen = 0;    public static void showStatistics() {        System.out.println("sendLen = " + sendLen);        System.out.println("recvLen = " + recvLen);    }}// must close sockets after a transaction                                                                                                                               class ConnectionHandler extends ProxyCounter implements Runnable {    private Socket clientSocket;    private Socket serverSocket;    private static final int bufferlen = 8192;    public ConnectionHandler(Socket clientSocket) {        this.clientSocket = clientSocket;    }    public void run() {        // receive request from clientSocket,                                                                                                                                   //extract hostname,                                                                                                                                                     //create a serverSocket to communicate with the host                                                                                                                    // count the bytes sent and received                                                                                                                                    try {            byte[] buffer = new byte[bufferlen];            int count = 0;            InputStream inFromClient = clientSocket.getInputStream();            count = inFromClient.read(buffer);            String request = new String(buffer, 0, count);            String host = extractHost(request);            // create serverSocket                                                                                                                                                  Socket serverSocket = new Socket(host, 80);            // forward request to internet host                                                                                                                                     OutputStream outToHost = serverSocket.getOutputStream();            outToHost.write(buffer, 0, count);            outToHost.flush();            sendLen += count;            showStatistics();            // forward response from internet host to client                                                                                                                        InputStream inFromHost = serverSocket.getInputStream();            OutputStream outToClient = clientSocket.getOutputStream();            while (true) {                count = inFromHost.read(buffer);                if (count < 0)                    break;                outToClient.write(buffer, 0, count);                outToClient.flush();                recvLen += count;                showStatistics();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                clientSocket.close();                serverSocket.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    private String extractHost(String request) {        int start = request.indexOf("Host: ") + 6;        int end = request.indexOf('\n', start);        String host = request.substring(start, end - 1);        return host;    }}

转载于:https://my.oschina.net/u/158589/blog/64643

你可能感兴趣的文章
(原創) 如何建立一个thread? (OS) (Linux) (C/C++) (C)
查看>>
<气场>读书笔记
查看>>
领域驱动设计,构建简单的新闻系统,20分钟够吗?
查看>>
web安全问题分析与防御总结
查看>>
React 组件通信之 React context
查看>>
ZooKeeper 可视化监控 zkui
查看>>
Linux下通过配置Crontab实现进程守护
查看>>
ios 打包上传Appstore 时报的错误 90101 90149
查看>>
Oracle推出轻量级Java微服务框架Helidon
查看>>
密码概述
查看>>
autoconf,automake,libtool
查看>>
jQuery的技巧01
查看>>
基于泛型实现的ibatis通用分页查询
查看>>
gopacket 使用
查看>>
AlertDialog对话框
查看>>
我的友情链接
查看>>
linux安全---cacti+ntop监控
查看>>
鸟哥的linux私房菜-shell简单学习-1
查看>>
nagios配置监控的一些思路和工作流程
查看>>
通讯组基本管理任务三
查看>>