Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用多个客户端连接到服务器_Java - Fatal编程技术网

Java 使用多个客户端连接到服务器

Java 使用多个客户端连接到服务器,java,Java,我正在尝试创建一个允许多个客户端的服务器,我可以很好地连接一个客户端,但不能连接两个客户端。我尝试将两个客户机连接到服务器的方法是在main方法中创建两个客户机对象和一个服务器。以下是服务器的代码: public class DraughtsSever extends JFrame{ JPanel panel; JTextArea gamesList; ServerSocket draughtsSS; JScrollPane scroll; Socket s; int i = 0; Drau

我正在尝试创建一个允许多个客户端的服务器,我可以很好地连接一个客户端,但不能连接两个客户端。我尝试将两个客户机连接到服务器的方法是在main方法中创建两个客户机对象和一个服务器。以下是服务器的代码:

public class DraughtsSever extends JFrame{

JPanel panel;
JTextArea gamesList;
ServerSocket draughtsSS;
JScrollPane scroll;
Socket s;
int i = 0;

DraughtsSever(){
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(350,350));
    gamesList = new JTextArea();
    //scroll.setPreferredSize(new Dimension(350,350));
    gamesList.setPreferredSize(new Dimension(300,300));
    //scroll.add(GamesList);
    //scroll = new JScrollPane(GamesList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    //panel.add(scroll);
    this.add(panel);
    panel.add(gamesList);
    this.setSize(400,400);
    this.setVisible(true);
    this.setTitle("Draughts Server");

    panel.add(gamesList);
    allowConnections();
}

public void allowConnections(){
    gamesList.append("Server listening on port 50000...");
    try{
        while(true){            
            draughtsSS = new ServerSocket(50000);
            //s = draughtsSS.accept();
            new Thread(new TestT(draughtsSS.accept())).start();
            //t.start();
        }
    }
    catch(IOException e){
        System.out.println("Error :"+e.getMessage());
    }   
}


class TestT implements Runnable{

    Socket s;

    TestT(Socket s){
        this.s = s;
    }

    public void run(){
        try{
            PrintWriter out = new PrintWriter(s.getOutputStream());
            Scanner in = new Scanner(s.getInputStream());
            gamesList.append("\n"+s.getInetAddress().toString() +" has connected.");
            JOptionPane.showMessageDialog(new JFrame(), "Successfully connected");
            out.println("hello");
            System.out.println(s.getInetAddress().toString() +" has connected.");
        }
        catch(IOException e){

        }
    }
}
}

以下是客户端中用于连接到服务器的方法

    private void setupConnection(String serverIP, String port){
    int portInt = Integer.parseInt(port);
    try{
        InetAddress IP = InetAddress.getByName(serverIP);
        int intPort = Integer.parseInt(port);
        Socket clientSocket = new Socket(IP,intPort);
        JOptionPane.showMessageDialog(new JFrame(), "Successfully connected to Server");
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
        Scanner in = new Scanner(clientSocket.getInputStream());
        //in.
    }
    catch(Exception e){
        System.out.println("ErrorC :" +e.getMessage());
    }
}
}
}

我认为您应该只创建一次服务器套接字,因此将其移出while循环:

    draughtsSS = new ServerSocket(50000);
您不需要不断地重新创建它,因为当客户端连接时,当您调用时,它们会自动移动到不同的套接字

   draughtsSS.accept()