服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一个简单的代理服务器

import java.io.*;
import java.net.*;

public class simpleproxyserver {
  public static void main(string[] argsthrows ioexception {
    try {
      string host = "your proxy server";
      int remoteport = 100;
      int localport = 111;
      // print a start-up message
      system.out.println("starting proxy for " + host + ":" + remoteport
          " on port " + localport);
      // and start running the server
      runserver(host, remoteport, localport)// never returns
    catch (exception e) {
      system.err.println(e);
    }
  }

  /**
   * runs a single-threaded proxy server on
   * the specified local port. it never returns.
   */
  public static void runserver(string host, int remoteport, int localport)
      throws ioexception {
    // create a serversocket to listen for connections with
    serversocket ss = new serversocket(localport);

    final byte[] request = new byte[1024];
    byte[] reply = new byte[4096];

    while (true) {
      socket client = null, server = null;
      try {
        // wait for a connection on the local port
        client = ss.accept();

        final inputstream streamfromclient = client.getinputstream();
        final outputstream streamtoclient = client.getoutputstream();

        // make a connection to the real server.
        // if we cannot connect to the server, send an error to the
        // client, disconnect, and continue waiting for connections.
        try {
          server = new socket(host, remoteport);
        catch (ioexception e) {
          printwriter out = new printwriter(streamtoclient);
          out.print("proxy server cannot connect to " + host + ":"
              + remoteport + ":/n" + e + "/n");
          out.flush();
          client.close();
          continue;
        }

        // get server streams.
        final inputstream streamfromserver = server.getinputstream();
        final outputstream streamtoserver = server.getoutputstream();

        // a thread to read the client's requests and pass them
        // to the server. a separate thread for asynchronous.
        thread t = new thread() {
          public void run() {
            int bytesread;
            try {
              while ((bytesread = streamfromclient.read(request)) != -1) {
                streamtoserver.write(request, 0, bytesread);
                streamtoserver.flush();
              }
            catch (ioexception e) {
            }

            // the client closed the connection to us, so close our
            // connection to the server.
            try {
              streamtoserver.close();
            catch (ioexception e) {
            }
          }
        };

        // start the client-to-server request thread running
        t.start();

        // read the server's responses
        // and pass them back to the client.
        int bytesread;
        try {
          while ((bytesread = streamfromserver.read(reply)) != -1) {
            streamtoclient.write(reply, 0, bytesread);
            streamtoclient.flush();
          }
        catch (ioexception e) {
        }

        // the server closed its connection to us, so we close our
        // connection to our client.
        streamtoclient.close();
      catch (ioexception e) {
        system.err.println(e);
      finally {
        try {
          if (server != null)
            server.close();
          if (client != null)
            client.close();
        catch (ioexception e) {
        }
      }
    }
  }
}

扫描关注微信公众号