Beispiel #1
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="o"></param>
        public static void Recive(object o)
        {
            try
            {
                var send = o as Socket;
                while (true)
                {
                    //获取发送过来的消息容器
                    byte[] buffer    = new byte[1024 * 1024 * 20];
                    var    effective = send.Receive(buffer);
                    //有效字节为0则跳过
                    if (effective <= 0)
                    {
                        break;
                    }
                    //这里绑定了IP send.RemoteEndPoint +
                    string   str = Encoding.UTF8.GetString(buffer, 0, effective);
                    string[] ip  = send.RemoteEndPoint.ToString().Split(':');
                    try
                    {
                        string[] th = str.Split(',');
                        if (th[0] is string && th[1] is string)
                        {
                            BuildDataToExcel.AddToExcel(th[0], th[1]);
                        }
                    }
                    catch
                    {
                        //令人窒息的Bug!!!!
                        //SocketBind();
                    }


                    Debug.WriteLine(send.RemoteEndPoint.ToString() + ":" + str);
                    //var buffers = Encoding.UTF8.GetBytes("Recived:" + str);
                    //将得到的字符串再返回给下位机
                    //send.Send(buffers);
                    //拿到值了
                    //ExcelAct(str);
                    //Range["F2"].Value2 = "sss";
                }
            }
            catch
            {
                //SocketBind();
            }
        }
Beispiel #2
0
 private void btnDisconnect_Click(object sender, RibbonControlEventArgs e)
 {
     BuildDataToExcel.AddToExcel("85", "32");
 }