/// <summary> /// 构造函数,创建广播接收和发送服务器,以及游戏客户端TCP/IP连接 /// </summary> /// <param name="udp1"></param> public BroadcastClient(ref UdpClient udp1, ref UdpClient udp2) { LocalIEP_receive = new IPEndPoint(NetworkControl.localIP, 23333); //选取本机地址,端口为23333 GameClientBroadcast_receive = new UdpClient(LocalIEP_receive); //创建广播接收服务器 ReceiveBroadcastArea = null; //接收范围为所有IP localIEP_send = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); //创建广播发送服务器地址 GameClientBroadcast_send = new UdpClient(localIEP_send); //创建广播发送服务器 ClientIEP_receive = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); Client_receive = udp1 = new UdpClient(ClientIEP_receive);//创建游戏接收客户端 ClientIEP_send = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); Client_send = udp2 = new UdpClient(ClientIEP_send);//创建游戏发送客户端 }
private String Response = "This is a game client"; //客户端验证声明 /// <summary> /// 构造函数,创建广播发送和接收服务器,以及游戏服务器监听器 /// </summary> /// <param name="tcpl"></param> public BroadcastServer(ref UdpClient udp1, ref UdpClient udp2) { LocalIEP_send = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); //选取开始广播服务器地址 GameServerBroadcast = new UdpClient(LocalIEP_send); //创建开始广播服务器 LocalIEP_receive = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); //选取响应接收服务器地址 GameServerBroadcastRecieve = new UdpClient(LocalIEP_receive); //创建响应接收服务器 DeclareBroadcastAreaIEP = new IPEndPoint(IPAddress.Broadcast, 23333); //广播发送范围为默认广播地址,端口为23333 ReceiveBroadcastAreaIEP = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); //创建接收地址段 GameServerIEP_receive = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); //选取游戏服务器地址和端口 GameServer_receive = udp1 = new UdpClient(GameServerIEP_receive); GameServerIEP_send = new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort()); //选取游戏服务器地址和端口 GameServer_send = udp2 = new UdpClient(GameServerIEP_send); //Console.WriteLine(LocalIEP_send.ToString()); //Console.WriteLine(LocalIEP_receive.ToString()); //Console.WriteLine(GameServerListener.ToString()); }
public UdpClient getClient() { while (true) { byte[] buff = GameServer_receive.Receive(ref ReceiveBroadcastAreaIEP); String message = Encoding.Unicode.GetString(buff); //字节流->字符串 String[] chips = message.Split(';'); //拆分数据 Console.WriteLine(message); if (chips[0].Equals(Response)) { UdpClient udpclient = new UdpClient(new IPEndPoint(NetworkControl.localIP, NetworkControl.GetFirstAvailablePort())); udpclient.Connect(new IPEndPoint(IPAddress.Parse(chips[1]), Int32.Parse(chips[2]))); return(udpclient);//返回客户端连接 } } }