UDP服务器和多个客户端(1v1游戏)

时间:2015-09-29 10:06:17

标签: multithreading networking

我试图创建一个UDP服务器,每个新游戏实例和客户端都有两个插槽。

它必须是游戏机制的UDP原因。我写了这个:

public class Server {

   private static HashSet<Integer> ports = new HashSet<Integer>(); // these     are player's IDs
   static ArrayList<InetAddress> addresses = new ArrayList<InetAddress>(); 
   public static boolean player1_active = false, player2_active = false;
   public static PlayerCredits p1, p2, temp;
   public static void main(String[] args) throws Exception {

   // The default port     
    int serverport = 8888;        


    DatagramSocket udpServerSocket = new DatagramSocket(serverport);        
        DatagramPacket sendpacket;
    System.out.println("Server started...\n");

    while(true)
    {

        byte[] receiveData = new byte[1024];          


        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);


        udpServerSocket.receive(receivePacket);           


        String clientMessage = (new String(receivePacket.getData())).trim();


        System.out.println("Client Connected - Socket Address: " + receivePacket.getSocketAddress());
        System.out.println("Client message: \"" + clientMessage + "\"");          


        InetAddress clientIP = receivePacket.getAddress();           
                    addresses.add(clientIP);

        System.out.println("Client IP Address & Hostname: " + clientIP + ", " + clientIP.getHostName() + "\n");

        // Get the port number which the recieved connection came from
        int clientport = receivePacket.getPort();
        System.out.println("Adding "+clientport);
        ports.add(clientport);

        // Response message         
        String returnMessage = clientMessage.toUpperCase();          
        System.out.println(returnMessage);
        // Create an empty buffer/array of bytes to send back 
        byte[] sendData  = new byte[1024];




                    temp = new PlayerCredits(clientport, clientIP);

                    if(!player1_active){
                     p1 = new PlayerCredits(clientport, clientIP);
                     player1_active = true;
                    }

                    if(temp.port != p1.port && temp.ad != p1.ad)
                     if(!player2_active){
                        p2 = new PlayerCredits(clientport, clientIP);
                        player2_active = true;
                    }

                    DatagramPacket sendPacket;
                     if(p2 == null){
                         String awaiting = "WAITING FOR SECOND PLAYER";
                         sendData = awaiting.getBytes();
                          sendPacket= new DatagramPacket(sendData, sendData.length, p1.ad, p1.port); 
                            udpServerSocket.send(sendPacket);   
                     }
                         else{
                         String awaiting = "SECOND PLAYER INCOMING";
                         sendData = awaiting.getBytes();
                            if(p2!=null){
                            sendPacket = new DatagramPacket(sendData, sendData.length, p2.ad, p2.port); 
                            udpServerSocket.send(sendPacket);   
                            }
                            sendPacket = new DatagramPacket(sendData, sendData.length, p1.ad, p1.port); 
                            udpServerSocket.send(sendPacket);    
                     }

        }

}
public static class PlayerCredits{
    int port;
    InetAddress ad;
    public PlayerCredits(int p, InetAddress a){
        this.port = p;
        this.ad = a;
    }
}

现在我有两个人连接 - 但你可能会说 - 这是错的。我需要以某种方式实现Multisocket上的每个播放器或其他东西(或通过TCP连接它们,然后使用新的数据报监听新线程)。

据我所知,最好的方法是在主线程上创建一个侦听器服务器(TCP) - 接受连接并将它们传递给&#39; Player的构造函数&#39;而在玩家的课程中,这一切都很有趣。

如果有人可以提供帮助并告诉我,应该如何完成,例如3个步骤(我不需要代码):

  1. 与TCP的连接
  2. 与玩家一起创建新线程并收听UDP
  3. 连接2名玩家时启动游戏
  4. 另外,如何停止第三次连接&#39;并将其重定向到一个新的游戏实例? (导致它的1v1)。

0 个答案:

没有答案
相关问题