代理服务-端口映射

import   java.io.*;  
import   java.net.*;
public class ProxySocket
{
 private ServerSocket listenerSocket;
 private Socket serverSocket;
 public ProxySocket()
 {
  try
  {
   System.out.println("服务器启动");//代理程序启动
   this.listenerSocket = new ServerSocket(9633);//代理端口
   while (true)
   {
    try
    {
     this.serverSocket = this.listenerSocket.accept();
     new MyThread(serverSocket).start();//启动一个线程进行监听
    }
    catch (Exception e)
    {
     System.out.println(e);
    }
   }
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
 }
 public static void main(String args[])//程序入口
 {
  new ProxySocket();//初始化代理类,代理程序启动
 }
 class MyThread extends Thread
 {
  private Socket threadSocket;//
  private Socket tomcatSocket;//
  private Thread myThread;
  private DataInputStream in;//输入流
  private DataOutputStream out;//输出流
  public MyThread(Socket socket)
  {
   try
   {
    threadSocket = socket;
    this.tomcatSocket = new Socket("127.0.0.1", 8080);//实际ip地址,端口号
   }
   catch (Exception e)
   {
    System.out.println(e);
   }
  }
  public void run()
  {
   try
   {
    //读取线程 
    new ReadClientWriteTomcatThread(threadSocket, tomcatSocket).start();
   }
   catch (Exception e)
   {
    System.out.println(e);
   }
  }
 }
 class ReadClientWriteTomcatThread extends Thread//读取客户端数据写入系统类
 {
  private DataInputStream read;//输入流
  private DataOutputStream write;//输出流
  private ByteArrayOutputStream swapStream;//字节流
  private Socket threadSocket;
  private Socket tomcatSocket;
  public ReadClientWriteTomcatThread(Socket threadSocketT, Socket tomcatSocketT)
  {
   try
   {
    threadSocket = threadSocketT;
    tomcatSocket = tomcatSocketT;
    read = new DataInputStream(threadSocket.getInputStream());
    write = new DataOutputStream(tomcatSocket.getOutputStream());
    this.swapStream = new ByteArrayOutputStream();
   }
   catch (Exception e)
   {
    System.out.println(e);
   }
  }
  public void run()
  {
   try
   {
    byte[] buf = new byte[100];
    int rc = 0;
    while ((rc = read.read(buf, 0, buf.length)) > 0)//循环读取客户段数据
    {
     this.swapStream.write(buf, 0, rc);
     this.swapStream.flush();
     if (rc < buf.length)
     {
      break;
     }
     //System.out.println(rc);  
    }
    byte[] resBuf = swapStream.toByteArray();
    this.write.write(resBuf, 0, resBuf.length);
    this.write.flush();//输入给系统
    //初始化线程,等待从系统中读取结果
    new ReadTomcatWriteClientThread(threadSocket, tomcatSocket).start();

   }
   catch (Exception e)
   {
    System.out.println(e);
   }
  }
 }
 class ReadTomcatWriteClientThread extends Thread//读取系统数据写入客户端类
 {
  private DataInputStream read;//输入流
  private DataOutputStream write;//输出流
  private ByteArrayOutputStream swapStream;//字节流
  public ReadTomcatWriteClientThread(Socket threadSocket, Socket tomcatSocket)
  {
   try
   {
    read = new DataInputStream(tomcatSocket.getInputStream());//初始化读取系统中数据输入流
    write = new DataOutputStream(threadSocket.getOutputStream());//初始化写入客户端的输出流
    this.swapStream = new ByteArrayOutputStream();//初始化字节流
   }
   catch (Exception e)
   {
    System.out.println(e);
   }
  }
  public void run()
  {
   try
   {
    byte[] buf2 = new byte[100];
    int rc2 = 0;
    while ((rc2 = read.read(buf2, 0, buf2.length)) > 0)//循环读取系统数据
    {
     this.swapStream.write(buf2, 0, rc2);
     this.swapStream.flush();
     if (rc2 < buf2.length)
     {
      break;
     }
    }
    byte[] resBuf2 = swapStream.toByteArray();
    this.write = new DataOutputStream(write);
    this.write.write(resBuf2, 0, resBuf2.length);
    this.write.flush();//输出数据
    this.write.close();
    this.read.close();
   }
   catch (Exception e)
   {
    System.out.println(e);
   }
  }
 }
}  

发布了0 篇原创文章 · 获赞 1 · 访问量 1087
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览