示例#1
0
        /// <summary>
        /// 启动socket服务
        /// </summary>
        public void StartSocket()
        {
            int        port     = 8881;
            IPEndPoint localEP  = new IPEndPoint(IPAddress.Any, port);
            Socket     listener = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listener.Bind(localEP);
                listener.Listen(1);
                IsSocketStart = true;
                StandardStreamHelper.Write("等待客户端连接....");
                while (true)
                {
                    Socket sc = listener.Accept();
                    Thread t  = new Thread(new ThreadStart(() => ReceiveData(sc)))
                    {
                        IsBackground = true
                    };
                    t.Start();
                }
            }
            catch (Exception e)
            {
                StandardStreamHelper.Write(e.ToString());
            }
        }
示例#2
0
文件: Program.cs 项目: wturi/DemoList
 private static void Main(string[] args)
 {
     if (args.Length != 0)
     {
         string chromeMessage = "";
         while (!string.IsNullOrEmpty(chromeMessage = StandardStreamHelper.OpenStandardStreamIn()))
         {
             StandardStreamHelper.Write(chromeMessage + "返回的");
             sh.clientConnectionItems.ForEach(s => s.SocketItem.Send(sh.PackData(chromeMessage)));
             if (!sh.IsSocketStart)
             {
                 Task.Run(() =>
                 {
                     sh.StartSocket();
                 });
             }
         }
     }
 }
示例#3
0
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sc"></param>
        private void ReceiveData(Socket sc)
        {
            if (!VerifyAllConnectionOnline())
            {
                sc.Close();
                return;
            }
            clientConnectionItems.Add(new ClientConnectionItem {
                SocketItem = sc
            });
            byte[] buffer = new byte[1024 * 1024];
            StandardStreamHelper.Write("接受到了客户端:" + sc.RemoteEndPoint.ToString() + "连接....");
            int length;

            try
            {
                length = sc.Receive(buffer);//接受客户端握手信息
                sc.Send(PackData(VerifyConnection(AnalyticData(buffer, length), sc)));
            }
            catch
            {
            }
            while (true)
            {
                try
                {
                    length = sc.Receive(buffer);//接受客户端信息
                    string clientMsg = AnalyticData(buffer, length);
                    StandardStreamHelper.Write("接受到客户端数据:" + clientMsg);
                    //发送数据
                    string sendMsg = clientMsg;
                    sc.Send(PackData(sendMsg));
                }
                catch
                {
                    clientConnectionItems.Remove(clientConnectionItems.FirstOrDefault(i => i.SocketItem == sc));
                    StandardStreamHelper.Write(sc.RemoteEndPoint.ToString() + "客户端已经断开连接!");
                    return;
                }
            }
        }