简体   繁体   English

连接到具有多个客户端的服务器

[英]Connecting to server with multiple clients

i'm trying to create a server that allows several clients, I can connect with one client fine, but I cant with two. 我正在尝试创建一个允许多个客户端的服务器,我可以与一个客户端连接良好,但不能与两个客户端连接。 The was I try to connect two clients to the server is by creating two client objects and one server in a main method. 我尝试将两个客户端连接到服务器的方法是通过在main方法中创建两个客户端对象和一个服务器。 Here is the code for the Server: 这是服务器的代码:

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

        }
    }
}

} }

Here is the method in the client that I use to connect to the server 这是客户端中用于连接服务器的方法

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

} } }}

I think you should only create the server socket once, so move this outside of the while loop: 我认为您应该只创建一次服务器套接字,因此将其移到while循环之外:

    draughtsSS = new ServerSocket(50000);

You don't need to keep re-creating this because when clients connect they automatically get moved to a different socket when you call 您不需要重新创建它,因为当客户端连接时,它们会在您呼叫时自动移至其他套接字

   draughtsSS.accept()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM