예제 #1
0
        /// <summary>
        /// 封装消息
        /// </summary>
        /// <param name="message">消息对象</param>
        /// <returns>封装后的字节数组</returns>
        public override byte[] EncapsulatedMessage(INetworkMessage message)
        {
            UdpNetworkInfo networkInfo = message as UdpNetworkInfo;

            byte[] bytes = Encoding.UTF8.GetBytes(networkInfo.Message);
            return(bytes);
        }
예제 #2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="client">客户端</param>
        /// <returns>接收到的消息对象</returns>
        protected override INetworkMessage ReceiveMessage(Socket client)
        {
            try
            {
                if (_serverEndPoint == null)
                {
                    _serverEndPoint = new IPEndPoint(IPAddress.Parse(Main.m_Network.ServerIP), Main.m_Network.ServerPort);
                }

                byte[] buffer = new byte[1024];
                int    length = client.ReceiveFrom(buffer, ref _serverEndPoint);

                UdpNetworkInfo info = new UdpNetworkInfo();
                info.Message = Encoding.UTF8.GetString(buffer, 0, length);
                return(info);
            }
            catch (Exception)
            {
                return(null);
            }
        }