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

关于异步通信的问题
我想写一个最简单的异步通信,就是把客户端的一个字符串传入服务器端,在服务器端倒序之后传回客户端显示,不知道是不是我这样写,而且在客户端还有报错,求高人指点。代码如下:
服务器端:
namespace SimpleServer
{
    public partial class Form1 : Form
    {
        private Thread ListenerThread;
        TcpListener tcpListener = null;

        public Form1()
        {
            InitializeComponent();
            ListenerThread = new Thread(new ThreadStart(ListenerThreadMethod));
            ListenerThread.IsBackground = true;
            ListenerThread.Start();
        }

        public void ListenerThreadMethod()
        {
            TcpClient tcpClient = null;
            NetworkStream networkStream = null;
            try
            {
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                tcpListener = new TcpListener(ip, 7777);
                tcpListener.Start();
            }
            catch
            {
                MessageBox.Show("不能建立服务器");
            }
            while (true)
            {
                try
                {
                    tcpClient = tcpListener.AcceptTcpClient();
                    //对收到的请求做处理
                    List<byte> reData = new List<byte>();
                    byte[] reDataBytes = new byte[tcpClient.ReceiveBufferSize];
                    int n = 0;