2

我有代理服务器的以下代码。如果是正确的方法吗?如果以商业方式部署,这是否能够处理负载/流量?

package proxyserver;


import com.sun.corba.se.spi.activation.Server;
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;



/**
 *
 * @author user
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    // Variable to track if an error occurred
boolean errorOccurred = false;

//Variables for the host and port parameters


    public static void main(String[] args) {
        // TODO code application logic here

        int localPort = -1;
        int remotePort = -1;
        String remoteHost = "www.youtube.com";

        System.out.print("dwdsw");


        Integer parseLocalPort = new Integer(555);
        Integer parseRemotePort = new Integer(80);
        localPort =80 ;

       remotePort = 80;

        //Create a listening socket at proxy

ServerSocket server = null;
try
{
    server = new ServerSocket(localPort);
}

catch(IOException e)
{
    System.err.println("Error: " + e.getMessage());
    System.exit(-1);
}

//Loop to listen for incoming connection,
//and accept if there is one

Socket incoming = null;
Socket outgoing = null;

while(true)
{
    try
    {
        // Create the 2 sockets to transmit incoming
        // and outgoing traffic of proxy server
        incoming = server.accept();
        outgoing = new Socket(remoteHost, remotePort);

        // Create the 2 threads for the incoming
        // and outgoing traffic of proxy server
        ProxyThread thread1 = new ProxyThread(incoming, outgoing);
        thread1.start();

        ProxyThread thread2 = new ProxyThread(outgoing, incoming);
        thread2.start();
    }
    catch (UnknownHostException e)
    {
        System.err.println("Error: Unknown Host " + remoteHost);
        System.exit(-1);
    }
    catch(IOException e)
    {
        //continue
        System.exit(-2);;
    }
}




    }

}

现在代理类

package proxyserver;

/**
 *
 * @author user
 */
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;

class ProxyThread extends Thread
{
    Socket incoming, outgoing;

    ProxyThread(Socket in, Socket out)
    {
        incoming = in;
        outgoing = out;
    }

    // Overwritten run() method of thread,
    // does the data transfers
    public void run()
    {
        byte[] buffer = new byte[5000];
        int numberRead = 0;
        OutputStream toClient;
        InputStream fromClient;

        try{
            toClient = outgoing.getOutputStream();
            fromClient = incoming.getInputStream();

            while(true)
            {
                numberRead = fromClient.read(buffer, 0, 50);
                if(numberRead == -1)
                {
                    incoming.close();
                    outgoing.close();
                }
                String st = new String(buffer,"US-ASCII");
                System.out.println("\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\nXXXXXXXXXXXXXXXX\n\n" + st);


                toClient.write(buffer, 0, numberRead);
            }
        }
        catch(IOException e)
        {
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
        }
    }
}
4

2 回答 2

1

它在原则上看起来是正确的,但你应该看看像TCP 代理这样的开源版本,以获得关于最大化吞吐量、增加弹性等的指针。

于 2009-08-12T15:15:45.897 回答
1

[好吧......足够的戏弄:-)]

不过,它看起来应该可以工作:

  1. 您在 Proxy 类中打印到 System.err 的内容可能会被损坏。(正如我之前所说,你不能假设每个网页都是用 ASCII 编码的!!)
  2. 您一次可能应该读取超过 50 个字节......特别是如果您想要高吞吐量。
  3. 您的主类可能应该使用线程池,而不是创建和丢弃线程。而且您可能应该在任何给定时间为要允许的线程数设置一个上限。
  4. 您可能需要对需要很长时间才能提供响应的服务器做一些事情,等等。

最后,针对这个问题:

如果在商业上部署,这是否能够处理负载/流量?

不可能说你可以通过这个程序泵出多少负载。首先,它将取决于您的处理器和网络接口硬件。

于 2009-08-12T15:16:47.453 回答