我的代理服务器没有按预期工作

时间:2015-04-25 18:37:46

标签: java sockets socket.io

我开发了一个简单的多线程代理服务器。请注意,此程序是一个特定服务器的代理。这是我的代码:

    public class Proxy {

    int remotePort;
    InetAddress remoteHost;

    public Proxy(InetAddress remoteHost, int remotePort) {
        this.remotePort = remotePort;
        this.remoteHost = remoteHost;
    }

    public void go() {
        ExecutorService pool = Executors.newCachedThreadPool();
        try (ServerSocket server = new ServerSocket(2015);) {
            System.out.println("Proxy is running....!");
            while (true) {
                Socket socketClient = server.accept();
                System.out.println("client connected...!");
                Socket socketServer = new Socket(remoteHost, remotePort);
                System.out.println("connection established with the server...!");
                Worker p1 = new Worker(socketClient, socketServer);
                Worker p2 = new Worker(socketServer, socketClient);
                pool.execute(p1);
                pool.execute(p2);

            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }

    class Worker implements Runnable {

        Socket from, to;

        public Worker(Socket from, Socket to) {
            this.from = from;
            this.to = to;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
            try (BufferedInputStream bis = new BufferedInputStream(from.getInputStream());
                    BufferedOutputStream bos = new BufferedOutputStream(to.getOutputStream());) {
                while (true) {
                    int octet = bis.read();
                    if (octet == -1) {
                        break;
                    }
                    bos.write(octet);

                }
                bos.flush();
                from.close();
                to.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }

        }

    }

    public static void main(String[] args) {
        InetAddress adr = null;
        try {
            adr = InetAddress.getLocalHost();
        } catch (UnknownHostException ex) {
            System.out.println(ex.getMessage());
        }
        new Proxy(adr, 2020).go();
    }

}

我想用客户端和服务器测试这个代理服务器。客户端发送一个整数数组以找到最大值。轮到他的服务器,返回接收到的数组的最大值。 问题如下:代理服务器没有发挥其作用。数组未发送到服务器,客户端未收到其数组的最大值。 我的服务器代码如下:

    public class ServerTCPMax {

    static void display(int[] tab) {
        for (int u : tab) {
            System.out.print(u + " ");
        }
        System.out.println();

    }

    static int searchMax(int[] t) {
        int max = t[0];
        for (int i = 1; i < t.length; i++) {
            if (t[i] > max) {
                max = t[i];
            }
        }
        return max;
    }

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(2020);
            System.out.println("server is running .......!  ");
            while (true) {
                try (Socket sclient = server.accept()) {
                    System.out.println("Proxy connected...!");
                    ObjectInputStream ois = new ObjectInputStream(sclient.getInputStream());
                    int[] tab = (int[]) ois.readObject();
                    display(tab);
                    int max = searchMax(tab);
                    try (DataOutputStream dos = new DataOutputStream(sclient.getOutputStream())) {
                        dos.writeInt(max);
                        dos.flush();

                    }
                }
            }
        } catch (IOException | ClassNotFoundException ex) {
            System.out.println(ex.getMessage());
        }
    }

}

以下代码代表我的客户:

 public class ClientTCPMax {

    public static void main(String[] args) {
        int[] A = {3, -7, 9, 22, 0, 7, 11, 2};
        try {
            try (Socket socket = new Socket("127.0.0.1", 2015)) {
                System.out.println("connection established with the Proxy ...!");
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(A);
                oos.flush();
                DataInputStream dis = new DataInputStream(socket.getInputStream());
                int max = dis.readInt();
                System.out.println(" the max is : " + max);
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

0 个答案:

没有答案
相关问题