0

我想制作一个有 2 个玩家的游戏。我将使用 udp 服务器和 2 个客户端,我不知道如何将 2 个客户端同时连接到 1 个服务器以及它们将如何通信。我将只使用java。最后鼠标单击将如何与服务器同步

  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.
        }
 }

一个快速的谷歌搜索会出现很多教程和例子,比如这个。还有一些 YouTude 视频。您可能想从其中之一开始。

于 2012-12-10T16:52:36.933 回答