服务器端是一个Echo服务器,通过线程读没有反应,难道必须通过异步读吗???
下面是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Diagnostics;
using System.Threading;
using System.IO;namespace LedClientTest
{
    public partial class Form1 : Form
    {
        TcpClient tcpClient;
        Thread readThread = null;
        bool _Continue = false;
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {        }        private void SendData(string data)
        {
            if ((tcpClient == null) || (!tcpClient.Connected))
            {
                ConnectToServer();
            }            try
            {
                NetworkStream stream = tcpClient.GetStream();                byte[] sendBuf = Encoding.Default.GetBytes(data);
                stream.Write(sendBuf, 0, sendBuf.Length);
                Debug.WriteLine("Write OK!");
                stream.Flush();            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        private void ConnectToServer()
        {
            if (tcpClient == null) tcpClient = new TcpClient();            tcpClient.NoDelay = true;
            tcpClient.ReceiveTimeout = 5000;
            tcpClient.SendTimeout = 5000;            tcpClient.Connect(tbIP.Text, Convert.ToInt16(tbPort.Text));            if (readThread == null)
                readThread = new Thread(ReadSocketData);            _Continue = true;
            readThread.Start();        }        void ReadSocketData()
        {
            CheckForIllegalCrossThreadCalls = false;            byte[] readBuff = new byte[1024];
            NetworkStream stream;
            int count;
            while (_Continue)
            {
                if (tcpClient.Connected)
                    stream = tcpClient.GetStream();
                else
                {
                    Thread.Sleep(50);
                    continue;
                }                try
                {
                    count = stream.Read(readBuff, 0, readBuff.Length);
                    string readStr = new string(Encoding.Default.GetChars(readBuff, 0, count));                    listBox1.Items.Add(readStr);
                }
                catch (IOException)
                {
                    continue;
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
        }        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                ConnectToServer();
            }
            catch (Exception ee)
            {                MessageBox.Show(ee.Message);
            }
        }        private void btnSend_Click(object sender, EventArgs e)
        {
            SendData(tbSend.Text);
        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_Continue)
            {
                _Continue = false;
                readThread.Join();
            }
        }    }
}

解决方案 »

  1.   

    我有个问题, 如果这些真的是服务器端代码,通篇连个 Bind 都没有, 那样程序会知道从哪里读数据吗? 没有打开端口,客户端怎么连上来?
      

  2.   

    MSDN中关于NetworkStream.Read的备注 该方法将数据读入 buffer 参数并返回成功读取的字节数。如果没有可以读取的数据,则 Read 方法返回 0。Read 操作将读取尽可能多的可用数据,
    直至达到由 size 参数指定的字节数为止。如果远程主机关闭了连接并且已接收到所有可用数据,Read 方法将立即完成并返回零字节。你使用时,参数size是readBuff.Length 也就是1024 应该一次没有这么多返回数据吧??