Ejemplo n.º 1
0
        /// <summary>
        /// 开启服务
        /// </summary>
        public void StartServer()
        {
            if (server != null && server.Connected)
            {
                return;
            }
            IPAddress ip = IPAddress.Parse(SERVER_IP);

            server = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(new IPEndPoint(ip, SERVER_PORT));
            server.SendTimeout = 1000 * 10;
            server.Listen(50);

            new Thread(() =>
            {
                while (true)
                {
                    Socket client = server.Accept();
                    var connector = new SocketConnector(new ClientModel {
                        Socket = client, Connected = true
                    }, ReceivedData, OnSocketException);
                    socketConnectors.Add(connector);
                }
            }).Start();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 发送信息
 /// </summary>
 /// <param name="socketConnector">客户端连接器</param>
 /// <param name="messageModel">消息对象</param>
 public void Send(SocketConnector socketConnector, MessageModel messageModel)
 {
     try
     {
         socketConnector.Send(messageModel.ToByteArray());
     }
     catch (Exception)
     {
     }
 }