Example #1
0
 /// <summary>
 /// 初始化网络服务,获取本机地址,开始广播服务器地址
 /// </summary>
 /// <returns></returns>
 public bool initService()
 {
     //获取本机地址
     #region
     IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
     //获取本地可用IPv4地址
     foreach (IPAddress ipa in ips)
     {
         if (ipa.AddressFamily == AddressFamily.InterNetwork)
         {
             localIP = ipa;
             break;
         }
     }
     #endregion
     //广播服务器地址
     #region
     try
     {
         ClientList             = new Dictionary <int, IPEndPoint>();
         RequestList_receive    = new Queue <String>();                                     //请求接收队列
         CommandList_send       = new Queue <String>();                                     //指令发送队列
         broadcastServer        = new BroadcastServer(ref Server_receive, ref Server_send); //广播服务器
         GameServer_send        = broadcastServer.LocalIEP_send;                            //广播服务器发送地址
         GameServer_receive     = broadcastServer.LocalIEP_receive;                         //广播服务器接收地址
         ServerBroadcast_thread = new Thread(() =>
         {
             while (true)
             {
                 broadcastServer.sendStartBroadcast();
                 Thread.Sleep(1000);
             }
         });                             //服务器发送广播
         ServerBroadcast_thread.IsBackground = true;
         ServerBroadcast_thread.Start(); //服务器开始广播
         state = State.Suspend;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(false);
     }
     #endregion
     return(localIP != null);
 }
Example #2
0
        /// <summary>
        /// 创建游戏服务器
        /// </summary>
        public void CreatGameServer()
        {
            GameClientsBroadcastList = new List <IPEndPoint>();                             //客户端广播地址表
            Clients             = new List <UdpClient>();                                   //客户端Udp连接
            RequestList_receive = new Queue <String>();                                     //游戏请求接收队列
            CommandList_send    = new Queue <String>();                                     //游戏指令发送队列
            broadcastServer     = new BroadcastServer(ref Server_receive, ref Server_send); //游戏广播服务器
            GameServer_send     = broadcastServer.LocalIEP_send;                            //游戏广播服务器发送地址
            GameServer_receive  = broadcastServer.LocalIEP_receive;                         //游戏广播服务器接收地址
            CreatGame_thread    = new Thread(() =>
            {
                while (true)
                {
                    broadcastServer.sendStartBroadcast();
                    Thread.Sleep(1000);
                    Console.WriteLine("sendbroadcast~");
                }
            });//服务器发送广播
            Thread receiveIPList_thread = new Thread(() =>
            {
                while (GameClientsBroadcastList.Count < PlayerCount)
                {
                    IPEndPoint P_temp = broadcastServer.receiveResponse();
                    Console.WriteLine("a new broadcastclient");
                    if (!isIPExist(P_temp))
                    {
                        GameClientsBroadcastList.Add(P_temp);
                    }
                }
                CreatGame_thread.Abort();
            });//服务器接收响应

            CreatGame_thread.IsBackground     = true;
            receiveIPList_thread.IsBackground = true;
            CreatGame_thread.Start();     //服务器开始游戏广播
            receiveIPList_thread.Start(); //服务器开始接收加入游戏响应
        }
Example #3
0
 /// <summary>
 /// 创建游戏服务器
 /// </summary>
 public void CreatGameServer()
 {
     GameClientsBroadcastList = new List<IPEndPoint>();//客户端广播地址表
     Clients = new List<UdpClient>();//客户端Udp连接
     RequestList_receive = new Queue<String>();//游戏请求接收队列
     CommandList_send = new Queue<String>();//游戏指令发送队列
     broadcastServer = new BroadcastServer(ref Server_receive, ref Server_send);//游戏广播服务器
     GameServer_send = broadcastServer.LocalIEP_send;//游戏广播服务器发送地址
     GameServer_receive = broadcastServer.LocalIEP_receive;//游戏广播服务器接收地址
     CreatGame_thread = new Thread(() =>
     {
         while (true)
         {
             broadcastServer.sendStartBroadcast();
             Thread.Sleep(1000);
             Console.WriteLine("sendbroadcast~");
         }
     });//服务器发送广播
     Thread receiveIPList_thread = new Thread(() =>
     {
         while (GameClientsBroadcastList.Count < PlayerCount)
         {
             IPEndPoint P_temp = broadcastServer.receiveResponse();
             Console.WriteLine("a new broadcastclient");
             if (!isIPExist(P_temp))
             {
                 GameClientsBroadcastList.Add(P_temp);
             }
         }
         CreatGame_thread.Abort();
     });//服务器接收响应
     CreatGame_thread.IsBackground = true;
     receiveIPList_thread.IsBackground = true;
     CreatGame_thread.Start();//服务器开始游戏广播
     receiveIPList_thread.Start();//服务器开始接收加入游戏响应
 }