代理服务-端口映射

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);
   }
  }
 }
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值