java实现的多线程代理服务器

发布于 2024-12-08 20:15:18 字数 4367 浏览 0 评论 0原文

我目前正在java中实现一个多线程代理服务器,它将接受来自客户端的消息并将它们转发到另一个服务器,然后该服务器将确认消息的接收。但是,我这样做遇到了麻烦。有人可以指出我做错了什么吗?谢谢。

ProxyApp:

public class ProxyApp {

    public static ServerSocket server = null;
    public static Socket client = null;


    public static void main(String[] args)
    {
        try
        {
            server = new ServerSocket(6789);
            Socket clientsocket = null;

            while(true)
            { 
                client = server.accept();      

                if(client.isConnected())
                {
                    System.out.println("Proxy is currently listening to client on port 6789");
                }
                Thread t1 = new ProxyHandler(client);
                t1.start();

                clientsocket = new Socket(InetAddress.getLocalHost(), 6780);
                if(clientsocket.isBound())
                {
                    System.out.println("Clientsocket successfully connected on port 6780");
                }

                ProxyHandler t2 = new ProxyHandler(clientsocket);
                t2.start();                                                               
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
        }
    }
}

ProxyHandler:

public class ProxyHandler extends Thread {

    private Socket socket;
    private String message;

    public ProxyHandler(Socket socket)
    {
        this.socket = socket;        
    }

    @Override
    public void run()
    {       
        message = "";
        try
        {
            DataInputStream in = new DataInputStream(socket.getInputStream());
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            while(true)
            {
                message = in.readUTF();
                out.writeUTF(message);

                System.out.println(message);
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

ClientClass:

public class ClientClass {

    public static void main(String[] args)
    {
        try
        {
            Socket client = new Socket(InetAddress.getLocalHost(), 6789);

            if(client.isBound())
            {
                System.out.println("Successfully connected on port 6789");
            }

            Scanner scanner = new Scanner(System.in);

            DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
            DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

            while(true)
            {
                String message;

                System.out.print("Enter your message: ");
                message = scanner.next();

                outToProxy.writeUTF(message);
                System.out.println(inFromProxy.readUTF());
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

ServerClass:

public class ServerClass {

    public static void main(String[] args)
    {
        try
        {
            ServerSocket server = new ServerSocket(6780);

            if(server.isBound())
            {
                System.out.println("Server successfully connected on port 6780");
            }

            Socket client = null;
            while(true)
            {
                client = server.accept();

                if(client.isConnected())
                {
                    System.out.println("Proxy is connected");
                }

                DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
                DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

                System.out.println(inFromProxy.readUTF());

                outToProxy.writeUTF("Message has been acknowledged!");
            }    
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

I am currently implementing a multithreaded proxy server in java which will accept messages from clients and forward them to another server which will then acknowledge the reception of the message. However, i'm having trouble doing so. Could someone point out what i am doing wrong? Thanks.

ProxyApp:

public class ProxyApp {

    public static ServerSocket server = null;
    public static Socket client = null;


    public static void main(String[] args)
    {
        try
        {
            server = new ServerSocket(6789);
            Socket clientsocket = null;

            while(true)
            { 
                client = server.accept();      

                if(client.isConnected())
                {
                    System.out.println("Proxy is currently listening to client on port 6789");
                }
                Thread t1 = new ProxyHandler(client);
                t1.start();

                clientsocket = new Socket(InetAddress.getLocalHost(), 6780);
                if(clientsocket.isBound())
                {
                    System.out.println("Clientsocket successfully connected on port 6780");
                }

                ProxyHandler t2 = new ProxyHandler(clientsocket);
                t2.start();                                                               
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
        }
    }
}

ProxyHandler:

public class ProxyHandler extends Thread {

    private Socket socket;
    private String message;

    public ProxyHandler(Socket socket)
    {
        this.socket = socket;        
    }

    @Override
    public void run()
    {       
        message = "";
        try
        {
            DataInputStream in = new DataInputStream(socket.getInputStream());
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            while(true)
            {
                message = in.readUTF();
                out.writeUTF(message);

                System.out.println(message);
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

ClientClass:

public class ClientClass {

    public static void main(String[] args)
    {
        try
        {
            Socket client = new Socket(InetAddress.getLocalHost(), 6789);

            if(client.isBound())
            {
                System.out.println("Successfully connected on port 6789");
            }

            Scanner scanner = new Scanner(System.in);

            DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
            DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

            while(true)
            {
                String message;

                System.out.print("Enter your message: ");
                message = scanner.next();

                outToProxy.writeUTF(message);
                System.out.println(inFromProxy.readUTF());
            }
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

ServerClass:

public class ServerClass {

    public static void main(String[] args)
    {
        try
        {
            ServerSocket server = new ServerSocket(6780);

            if(server.isBound())
            {
                System.out.println("Server successfully connected on port 6780");
            }

            Socket client = null;
            while(true)
            {
                client = server.accept();

                if(client.isConnected())
                {
                    System.out.println("Proxy is connected");
                }

                DataInputStream inFromProxy = new DataInputStream(client.getInputStream());
                DataOutputStream outToProxy = new DataOutputStream(client.getOutputStream());

                System.out.println(inFromProxy.readUTF());

                outToProxy.writeUTF("Message has been acknowledged!");
            }    
        }
        catch(IOException io)
        {
            System.err.println("IOException: " + io.getMessage());
            System.exit(2);
        }
    }
}

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

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2024-12-15 20:15:18

下面代码中的 in 和 out 表示来自和发送到同一台机器的消息:

DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

因此,当您从 in 读取并向 out 写入时,您所做的就是创建一个 echo 服务。

message = in.readUTF();
out.writeUTF(message);

对于代理,您希望从客户端读取并写入服务器,反之亦然。

看来您想要更接近于此的东西:

    client = server.accept();      
    clientsocket = new Socket(InetAddress.getLocalHost(), 6780);
    Thread t1 = new ProxyHandler(client, clientsocket );
    t1.start();
    Thread t2 = new ProxyHandler(clientsocket, client);
    t2.start();

第一个线程的工作是将数据从客户端发送到服务器,第二个线程的工作是将数据从服务器发送到客户端。

The in and out in the following code represent message from and to the same machine:

DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

so when you read from in and write to out, all you are doing is creating an echo service.

message = in.readUTF();
out.writeUTF(message);

For a proxy, you want to read from the client and write to the server, and vice versa.

It seems you want something closer to this:

    client = server.accept();      
    clientsocket = new Socket(InetAddress.getLocalHost(), 6780);
    Thread t1 = new ProxyHandler(client, clientsocket );
    t1.start();
    Thread t2 = new ProxyHandler(clientsocket, client);
    t2.start();

where the job of the first thread is to send data from the client to the server and the second's job is to send from the server to the client.

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