How to create a multi-user server or where to download it?



  • How to create a multi-user server or where to download it?

    The target is that the server has to wait to connect clients. If a client comes in with number one, he's throwing him in group 1 after a client with number two gets here, he's in group 2. If the client comes in with number one, the kinet is in group 1 and the group will be formalized for them, they will be able to communicate only among themselves. How do you do that? I'm tired.

    public class Main {
        public static Socket socket1,socket2;
    
    public static void main(String[] args) throws IOException, InterruptedException {
    
        ServerSocket serverSocket=new ServerSocket(7475);
        while (true){
            Socket socket=serverSocket.accept();
            String asd = new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine();
    
            if (asd.equals("0")){
                socket1=socket;
                MySocket(socket,0);
            }
    
            if (asd.equals("1")){
                socket2=socket;
                MySocket(socket,1);
            }
        }
    }
    
    public static void MySocket(Socket socket,int a){
        new Thread(new Runnable() {
            String exit="";
            @Override
            public void run() {
                try {
                    while (!exit.equals("exit")){
                        String asd = new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine();
                        if (asd.equals("exit")) exit="exit";
                        BufferedWriter bufferedWriter=null;
                        if (a==0) {
                            bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream()));
                        }
                        if (a==1) {
                            bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream()));
                        }
                        bufferedWriter.write(asd);
                        bufferedWriter.flush();
                    }
                    socket.close();
                    System.out.println("CLOSE");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    

    }



  • Stop using cheese soccers, people.

    There are many beautiful libraries that many things do for you. We use our game. http://netty.io/ For example.

    You have a more conceptual problem. We should split the logic, at least two parts:

    1. A service-mm that plays the role of mackerel match. He's following the players' distribution.
    2. Service playing instance. People play it. A service desk can be made (for one group of players). Or do a service where there's a list of rooms to save resources.

    After the distribution of the mm, the service gives ip: the report and password to the service desk and the players are connected to it.

    It's a little work configuration. If there's a number of players in the car, it's good to have a service manager who's watching their condition. Then the MMM will work with this service, and he will be choosing a specific install.

    I described a simplified architecture like what we use.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2