Simple Proxy Server (Java)

Source: Internet
Author: User

Although there are many problems, they can be said to be "not handled ". However, you can still browse the Web page through this proxy and count the outbound traffic and inbound traffic. The Code is as follows. (This post is sent by the same proxy ).

 

1. Change serverSocket. close () to if (serverSocket! = Null) serverSocket. close ();, exception printing is much less. No null pointer exception.

2. Determine the count of the request read from the browser for the first time. If it is <= 0, the socket is directly disabled. If it is greater than 0, other processing is performed. The speed of Web browsing has become faster. I don't know why.


View sourceprint? 001 package network. proxy;

002

003 import java. io .*;

004 import java.net .*;

005 import java. util .*;

006

007 // internet server <-- serverSocket --> proxy <-- clientSocket --> client

008

009 public class MyProxy {

010 public static void main (String [] args ){

011 try {

012 new Thread (new SimpleProxyServer (8989). start ();

013} catch (IOException e ){

014 e. printStackTrace ();

015}

016}

017}

018

019 class SimpleProxyServer implements Runnable {

020 private ServerSocket listenSocket;

021 public SimpleProxyServer (int port) throws IOException {

022 this. listenSocket = new ServerSocket (port );

023}

024

025 public void run (){

026 (;;){

027 try {

028 Socket clientSocket = listenSocket. accept ();

029 System. out. println ("Create a new Thread to handle this connection ");

030 new Thread (new ConnectionHandler (clientSocket). start ();

031} catch (IOException e ){

032 e. printStackTrace ();

033}

034}

035}

036}

037

038

039 class ProxyCounter {

040 static int sendLen = 0;

041 static int recvLen = 0;

042

043 public static void showStatistics (){

044 System. out. println ("sendLen =" + sendLen );

045 System. out. println ("recvLen =" + recvLen );

046}

047}

048

049 // must close sockets after a transaction

050 class ConnectionHandler extends ProxyCounter implements Runnable {

051 private Socket clientSocket;

052 private Socket serverSocket;

053

054 private static final int bufferlen = 8192;

055

056 public ConnectionHandler (Socket clientSocket ){

057 this. clientSocket = clientSocket;

058}

059 public void run (){

060 // receive request from clientSocket,

061 // extract hostname,

062 // create a serverSocket to communicate with the host

063 // count the bytes sent and received

064 try {

065 byte [] buffer = new byte [bufferlen];

066 int count = 0;

067

068 InputStream inFromClient = clientSocket. getInputStream ();

069 count = inFromClient. read (buffer );

070 String request = new String (buffer, 0, count );

071 String host = extractHost (request );

072 // create serverSocket

073 Socket serverSocket = new Socket (host, 80 );

074 // forward request to internet host

075 OutputStream outToHost = serverSocket. getOutputStream ();

076 outToHost. write (buffer, 0, count );

077 outToHost. flush ();

078 sendLen + = count;

079 showStatistics ();

080 // forward response from internet host to client

081 InputStream inFromHost = serverSocket. getInputStream ();

082 OutputStream outToClient = clientSocket. getOutputStream ();

083 while (true ){

084 count = inFromHost. read (buffer );

085 if (count <0)

086 break;

087 outToClient. write (buffer, 0, count );

088 outToClient. flush ();

089 recvLen + = count;

090 showStatistics ();

091}

092} catch (IOException e ){

093 e. printStackTrace ();

094} finally {

095 try {

096 clientSocket. close ();

097 serverSocket. close ();

098} catch (IOException e ){

099 e. printStackTrace ();

100}

101}

102}

103

104 private String extractHost (String request ){

105 int start = request. indexOf ("Host:") + 6;

106 int end = request. indexOf ('\ n', start );

107 String host = request. substring (start, end-1 );

108 return host;

109}

110}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.