Exemplo n.º 1
0
        /// <summary>
        /// 向单个客户端发送数据
        /// </summary>
        /// <param name="rsClient"></param>
        /// <param name="data"></param>
        public void SendData(RegistryClient registryClient, RegistryMessageAction action, String data)
        {
            try
            {
                RegistryMessage rm = new RegistryMessage()
                {
                    Action      = action,
                    MessageBody = data
                };

                String messageData = XmlUtil.SaveXmlFromObj <RegistryMessage>(rm);
                Console.WriteLine("发送数据:{0}", messageData);

                Byte[] msg = Encoding.UTF8.GetBytes(messageData);
                registryClient.Socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendCallback), registryClient);
            }
            catch (Exception ex)
            {
                XTrace.WriteLine("发送数据时发生异常:" + ex.ToString());
                lock (m_RegistryClients)
                {
                    m_RegistryClients.Remove(registryClient);
                    registryClient.Dispose();
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 给所有的客户端发送消息
 /// </summary>
 public void SendDataToAllClient(RegistryMessageAction action, String message)
 {
     foreach (var client in m_RegistryClients)
     {
         SendData(client, action, message);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 向服务器发送数据
        /// </summary>
        /// <param name="message"></param>
        public void SendData(RegistryMessageAction action, String message)
        {
            try
            {
                RegistryMessage regMessage = new RegistryMessage()
                {
                    Action      = action,
                    ClientType  = m_ClientType,
                    MessageBody = message
                };

                String dataMessage = XmlUtil.SaveXmlFromObj <RegistryMessage>(regMessage);
                Console.WriteLine("发送数据:{0}", dataMessage);

                Byte[] data = Encoding.UTF8.GetBytes(dataMessage);
                m_SocketClient.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), m_SocketClient);
            }
            catch (Exception ex)
            {
                OnLostConnection(ex.ToString());
            }
        }