在java中挖掘两个套接字客户端

我正在尝试互连连接到单个远程服务器的两个套接字客户端.
案例是:

> Client_1]连接到服务器
> Client_2]连接到服务器
> Server]在Client_1和Client_2之间创建隧道
> Client_1]写“东西”
> Client_2](等待某些消息)收到Client_1的“内容”

反之亦然.

那是我的代码:

package jtestsock;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

/**
 *
 * @author massimodeluisa
*/
public class Server extends Thread{

private List<Socket> clients;
private ServerSocket server;
private int port = 5001;
private BufferedReader input;
private PrintWriter output;

public Server() {
    try {
        server = new ServerSocket(port);

    } catch (IOException e) {
        System.out.println("Impossibile istanziare il server: "+e.getMessage());
    }
}

@Override
public void run() {
    System.out.println("Waiting for client message...");

    //
    // The server do a loop here to accept all connection initiated by the
    // client application.
    //
    while (true) {
        try {
            Socket socket = server.accept();
            System.out.println("Connection Received!");

            clients.add(socket);


            /* read response */
            input = new BufferedReader(
                    new InputStreamReader(
                    socket.getInputStream()));

            output = new PrintWriter(
                    new BufferedWriter(
                    new OutputStreamWriter(
                    socket.getOutputStream())));

            if(clients.size()>0){
                Socket first    = new Socket();
                Socket second   = new Socket();

                first = clients.get(1);
                second= clients.get(2);     // || second = socket;

                // ??? Tunneling input and output between two clients
            }

        } catch (IOException e) {
            System.out.println("Client connection error: "+e.getMessage());
        }
    }
}
}

有人可以帮我吗?
谢谢 :)

更新:

我想在两个客户端之间建立一个Point to Point连接,传递给我的服务器,就像一个代理服务器……

服务器必须接受两个以上的连接,并在服务器上创建两个线程,用于写入和读取将消息从一个客户端重定向到另一个客户端,CPU将饱和.

(Ps.抱歉我的英文XD)

解决方法:

我会这样做(简化版):

class Server extends Thread
    ...
        public void run() {
            while (true) {
                try {
                    Socket s1 = server.accept();
                    Socket s2 = server.accept();
                    new Client(s1, s2).start();  // reads from s1 and redirects to s2
                    new Client(s2, s1).start();  // reads from s2 and redirects to s1
                } catch (IOException e) {
                    System.out.println("Client connection error: " + e.getMessage());
                }
            }
        }

class Client extends Thread {
    Socket s1;
    Socket s2;

    Client(Socket s1, Socket s2) {
        this.s1 = s1;
        this.s2 = s2;
    }

    public void run() {
        try {
            InputStream is = s1.getInputStream(); 
            OutputStream os = s2.getOutputStream();
            for (int i; (i = is.read()) != -1; i++) {
                os.write(i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}
上一篇:Missing artifact jdk.tools:jdk.tools:jar:1.6


下一篇:HTML转PDF目前最优解决方案:headless chrome