| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:C#基于TCP协议的通信无法接收服务器发送的数据
只看楼主 加入收藏
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
C#基于TCP协议的通信无法接收服务器发送的数据
C#基于TCP协议的通信无法接收服务器发送的数据,请帮忙解决,代码如下:
  private void btnOpen_Click(object sender, EventArgs e)
        {
            if (!(Client == null) && Client.Connected)  
            {
                MessageBox.Show("连接已建立");
                return;
            }
            try
            {
                strIP = txtIP.Text.Trim();
                //将IP地址字符串转化为IPAddress实例
                ip = IPAddress.Parse(strIP);
                int port = Int32.Parse(txtPort.Text.Trim());
                Client = new TcpClient();
                ServerEP = new IPEndPoint(ip, port);
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Client.Connect(ServerEP);
                if (Client.Connected)
                {
                    MessageBox.Show("连接成功");
                    if (!txtRcv.InvokeRequired)
                    {
                        Thread thread = new Thread(new ThreadStart(Receive));
                        thread.Start();
                    }
                    else
                    {
                        txtRcv.Invoke(new SynReceive(Receive));
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
/// <summary>
        /// 声明获取事件的委托
        /// </summary>
        private delegate void SynReceive();
        /// <summary>
        /// 线程接收函数
        /// </summary>
        public void Receive()
        {
            int buffersize=1024;
            byte[] rcvBuffer=new byte[buffersize];
            NetworkStream Stream=Client.GetStream();
            while(true)
            {
               
                String strRcv=ServerEP.ToString()+"\r\n";
                int recBytes=Stream.Read(rcvBuffer,0,rcvBuffer.Length);
                strRcv=Encoding.ASCII.GetString(rcvBuffer);
                //txtRcv.AppendText(strRcv);
               
            }

        }
搜索更多相关主题的帖子: 服务器 IP地址 private 字符串 return 
2011-06-02 13:35
ykp
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2011-3-23
收藏
得分:0 
没人理我啊
2011-06-04 19:53
快速回复:C#基于TCP协议的通信无法接收服务器发送的数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016352 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved