java中的代理服务器

发布于 2024-08-01 13:43:55 字数 3250 浏览 7 评论 0原文

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

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

i have the follwoing code of proxy server. IS if the right approach? Will this be able to handel load/trafffic if deployed comerially??

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




    }

}

now proxy classs

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-08-08 13:43:55

[好吧...够戏弄了:-)]

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

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

最后针对此事回应一下:

这是否能够处理
如果商业部署的话负载/流量??

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

[ OK ... enough teasing :-) ]

It looks like it should work, though:

  1. The stuff that you print to System.err in the Proxy class may be mangled. (As I said before, you cannot just assume that every web page is encoded in ASCII!!)
  2. You should probably be reading much more than 50 bytes at a time .... especially if you want high throughput.
  3. Your main class probably should be using a thread pool rather than creating and throwing away threads. And you probably should put an upper bound on the number of threads you want to allow at any given time.
  4. You probably need to do something about servers that take a long time to deliver their responses, etcetera.

Finally, in response to this:

Will this be able to handle the
load/traffic if it is deployed commercially??

It is impossible to say how much load you could pump through this program. For a start, it will depend on your processor and network interface hardware.

怼怹恏 2024-08-08 13:43:55

它看起来原则上是正确的,但你应该看一下开源版本,例如 TCP Proxy 有关最大化吞吐量、提高弹性等的指导。

It looks about right in principle but you should take a look at an open source version like TCP Proxy for pointers on maximizing throughput, increasing resilience, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文