码迷,mamicode.com
首页 > 其他好文 > 详细

Agent proxy

时间:2016-05-13 00:31:11      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

记录,回公司空余时间继续写
源码地址:http://download.csdn.net/detail/happy_boys_/9513905
技术分享
AgentConnect.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jasper.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jasper
 */
public class AgentConnect {

    private final int PORT = 4352;
    private ServerSocket server = null;

    public AgentConnect() throws UnknownHostException, IOException {
        this.server = new ServerSocket();
        this.server.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), PORT));
    }

    public void startServer() throws IOException {
        while (true) {
            Socket lanSocket = server.accept();
            InputStream upIn = lanSocket.getInputStream();
            // instance and set input stream
            UploadFlow up = new UploadFlow(upIn);
            // instance and set output stream
            DownloadFlow down = new DownloadFlow(lanSocket.getOutputStream());

            InetSocketAddress serverInet = up.serverInetAddress();
            Socket inetSocket = new Socket();
            inetSocket.bind(serverInet);

            // set up output stream, start upload thread
            up.startUpload(inetSocket.getOutputStream());
            // set down output stream, start download thread
            down.startDownload(inetSocket.getInputStream());
        }
    }

    public static void main(String[] args) {
        try {
            AgentConnect agent = new AgentConnect();
            agent.startServer();
        } catch (IOException ex) {
            Logger.getLogger(AgentConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



Flow.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jasper.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jasper
 */
public class Flow  extends Thread{
    protected InputStream in;
    protected OutputStream out;

    protected Flow(InputStream in) {
        this.in = in;
    }

    protected Flow(OutputStream out) {
        this.out = out;
    }

    public Flow(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run(){
        // upload
        byte[] data = new byte[1024];
        int len;
        try {
            while(true) {
                len = in.read(data);
                out.write(data,0,len);
            }
        } catch (IOException ex) {
            Logger.getLogger(UploadFlow.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            this.close();
        }
    }

    private void close() {
        try {
            this.in.close();
            this.out.close();
        } catch (IOException ex) {
            Logger.getLogger(UploadFlow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



UploadFlow.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jasper.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jasper
 */
public class UploadFlow extends Flow{

    public UploadFlow(InputStream upIn) {
        super(upIn);
    }

    public InetSocketAddress serverInetAddress() {
        // get server InetAddress by upIn
        return null;
    }

    public void startUpload(OutputStream upOut) {
        this.out = upOut;
        this.start();
    }
}



DownloadFlow.java

package com.jasper.http;

import java.io.InputStream;
import java.io.OutputStream;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jasper
 */
public class DownloadFlow extends Flow{

    /**
     *
     * @param downOut
     */
    public DownloadFlow(OutputStream downOut) {
        super(downOut);
    }

    public void startDownload(InputStream downIn) {
        this.in = downIn;
        this.start();
    }
}

Agent proxy

标签:

原文地址:http://blog.csdn.net/happy_boys_/article/details/51348509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!