none
C# SOCKET异步通信 RRS feed

  • 问题

  • 用WPF做异步通信 什么地方是得到客户端的数据 怎么对数据做处理 例如怎么获取数据长度

    代码如下

    namespace 服务器
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            private Thread Listenerthread;
            TcpListener tcpListener = null;
            public MainWindow()
            {
                InitializeComponent();
                Listenerthread = new Thread(new ThreadStart(ListenerthreadMethod));
                Listenerthread.IsBackground = true;
                Listenerthread.Start();
            }
            public void ListenerthreadMethod()
            {
                TcpClient tcpClient = null;
                NetworkStream netStream = null;
                try
                {
                    IPAddress ip = IPAddress.Parse("127.0.0.1");
                    tcpListener = new TcpListener(ip, 1300);
                    tcpListener.Start();
                    MessageBox.Show("服务器建立成功");
                }
                catch
                {
                    MessageBox.Show("不能建立服务器");
                    return;
                }
                byte[] data = new byte[1];
                while (true)
                {
                    try
                    {
                        tcpClient = tcpListener.AcceptTcpClient();
                        netStream = tcpClient.GetStream();
                        netStream.BeginWrite(data, 0, data.Length, new AsyncCallback(DownLoadCallBackF), netStream);
                    }
                    catch
                    {
                    }
                }
            }
            public void DownLoadCallBackF(IAsyncResult ar)
            {
                NetworkStream netStream = (NetworkStream)ar.AsyncState;
                try
                {
                    netStream.EndWrite(ar);
                }
                catch
                {
                    MessageBox.Show("写流文件失败");
                }
                finally
                {
                    if (netStream != null)
                        netStream.Close();
                }
            }
        }
    }
    


    • 已移动 Jie BaoModerator 2012年6月4日 2:45 (发件人:Windows Presentation Foundation)
    • 已编辑 Nav7SL 2012年6月6日 5:04
    2012年6月3日 15:58

答案

  • 您好:)

    判断是否可以读写,然后分别读写(你是服务端,使用

    ReceiveBufferSize

    )进行大小判断!

     try
                    {
                        tcpClient = tcpListener.AcceptTcpClient();
                        netStream = tcpClient.GetStream();
                      
                     if (netStream.CanWrite)
            {
                byte [] data= Encoding.UTF8.GetBytes ("Is anybody there?");
                 netStream.BeginWrite(data, 0, data.Length, new AsyncCallback(DownLoadCallBackF), netStream);
            }
                     if (netStream.CanRead)
            {
                // Reads NetworkStream into a byte buffer.
                byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
    
                // Read can return anything from 0 to numBytesToRead. 
                // This method blocks until at least one byte is read.
                netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);
    
                // Returns the data received from the host to the console.
                string returndata = Encoding.UTF8.GetString (bytes);
    
                Console.WriteLine ("This is what the host returned to you: " + returndata);
    
            }
                    }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处


    2012年6月4日 3:00
    版主