日期:2014-05-17  浏览次数:20760 次

Socket接收数据并绑定给Gridview
如题
实时接收没什么问题,就是刷新绑定gridview老是没有效果。各位大侠给点意见 

        private delegate void ShowMsg(string strMsg);
        private ShowMsg SM;
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
        SM += new ShowMsg(SetLabel);

        dt.Columns.Add("yvalue", typeof(string));
        dt.Rows.Add();
        }
        private void SetLabel(string strMsg)
        {

            gvOpint.DataSource = dt;
            gvOpint.DataBind();
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            Thread td = new Thread(new ThreadStart(Sockelisten));
            td.Start();
        }
        void Sockelisten()
        {

            IPAddress ips = Dns.GetHostAddresses(Dns.GetHostName())[0];
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.2.59"), 6666);
            Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            soc.Bind(ip);
            soc.Listen(10);
            Socket hander = soc.Accept();
            while (true)
            {
                byte[] bytes = new byte[1024];
                int len = hander.Receive(bytes);
                string st = Encoding.Default.GetString(bytes);
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count - 1][0] = st;

                SM(st);