0

2 人のプレーヤーでゲームを作りたいです。udp サーバーと 2 つのクライアントを使用しますが、2 つのクライアントを 1 つのサーバーに同時に接続する方法と、それらが通信する方法がわかりません。私はjava.Atのみを使用します。最後に、マウスクリックがサーバーとどのように同期するか

  public void mousePressed(MouseEvent event) { 
  }  
  public void mouseReleased(MouseEvent event) { 
  }  
  public void mouseEntered(MouseEvent event) { 
  }  
  public void mouseExited(MouseEvent event) { 
  } 

サーバー

public class Provider {
public ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
String[] torino={"null","null"};
Provider() {}
void run()
{
    try {
        //1. creating a server socket (8080=port , 2 =number of connections)
        providerSocket = new ServerSocket(8080, 2 );
        //2. Wait for connection
        System.out.println("Waiting for connection");
        connection = providerSocket.accept();
        System.out.println("Connection received from " + connection.getInetAddress().getHostName());
        //3. get Input and Output streams
        out = new ObjectOutputStream(connection.getOutputStream());
        // flush= clean the object out
        out.flush();
        in = new ObjectInputStream(connection.getInputStream());
        sendMessage("Connection successful");
        //4. The two parts communicate via the input and output streams
        try {
            //take the message from client
            message = (String)in.readObject();
            if (torino[0]=="null") 
                torino[0]=message;
            } else if (torino[1]=="null") {
                torino[1]=message;              
            }
        }
        catch(ClassNotFoundException classnot) {
            System.err.println("Data received in unknown format");
        }
    } catch(IOException ioException) {
        ioException.printStackTrace();
    }
    finally {
        //4: Closing connection
        try {
            in.close();
            out.close();
            providerSocket.close();
        } catch(IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
//method to send messages from server to clients
void sendMessage(String msg)
{
    try {
        out.writeObject(msg);
        out.flush();
        System.out.println("server>" + msg);
    }
    catch(IOException ioException) {
        ioException.printStackTrace();
    }
}

//main method
public static void main(String args[])
{
    Provider server = new Provider();
    while(true) {
        server.run();
    }
}
4

1 に答える 1

0

これで、サーバー ソケットが接続を待機しているポイントがあることをコードで確認できます。それがaccept()メソッドです。その時点で、接続を処理するための新しいスレッドを作成し、メイン スレッドが別の接続を待機し続けるようにする必要があります。numConnections などの変数で行われた接続の数を追跡したい場合があります。

 while(numConnections < 2){
       connection = providerSocket.accept();
       Thread t = new Thread(new ConnectionHandler(connection));
       t.start();
       numConnections++;
 }

これで、 ConnectionHandler クラスが接続の作業を行います。

 class ConnectionHandler implements Runnable{
        Socket connection;
        public ConnectionHandler(Socket connection){
             this.connection = connection;
        }

        public void run(){
           //here you do all the code associated with handling the connection
           //such as your Object Streams and so on.
        }
 }

Google で簡単に検索すると、このようなことを行う多くのチュートリアルと例が見つかります。YouTudeの動画もあります。それらのいずれかから始めたいと思うかもしれません。

于 2012-12-10T16:52:36.933 に答える