关于socket通信,服务器与客户端传递信息,一方发送一方接收,我刚接触这个,不是很懂,请大家帮帮忙给个全代码,急用昂!谢谢

解决方案 »

  1.   

    直接 搜索 "在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架"直接编译 保证好用
      

  2.   

    服务器   private string _ServerIp = string.Empty;
            private Thread processor;
            private TcpListener tcpListener;
            string clientStr;
            public delegate void TextBoxDelegate(string content);
     private void StartListening()
            {
                //创建一个监听对象
                tcpListener = new TcpListener(IPAddress.Any, 8888);
                tcpListener.Start();
                //循环监听
                while (true)
                {
                    try
                    {
                        //取得客户端的连接
                        TcpClient tcpClient = tcpListener.AcceptTcpClient();
                        //取得客户端发过来的字节流
                        NetworkStream clientStream = tcpClient.GetStream();
                        //把字节流读入字节数组
                        byte[] buffer = new byte[1024];
                        clientStream.Read(buffer, 0, 1024);
                        clientStr = System.Text.Encoding.UTF8.GetString(buffer);
                        ReceiveContent( tcpListener.Server.AddressFamily.ToString() +"说:"+ clientStr + "\n");
                        tcpClient.Close();
                        clientStream.Close();
                        Thread.Sleep(500);
                    }
                    catch (SocketException se)
                    {
                        throw new Exception(se.Message);
                    }
                }
            }
            /// <summary>
            /// 处理线程UI控件的问题
            /// </summary>
            /// <param name="RecContent"></param>
            private void ReceiveContent(string RecContent)
            {
                if (this.textBoxContent.InvokeRequired)//如果在UI主线程操作
                {
                    this.textBoxContent.Invoke(new TextBoxDelegate(ReceiveContent), new object[] { RecContent });
                }
                else
                    this.textBoxContent.AppendText(RecContent);
            }
     private void Form1_Load(object sender, EventArgs e)
            {
                processor = new Thread(new ThreadStart(StartListening));
                processor.IsBackground = true;
                processor.Start();
            }
    客户端  private string _ServerIp = string.Empty;
            private Thread processor;
            private TcpListener tcpListener;
            string clientStr;
            public delegate void TextBoxDelegate(string content);
            private void buttonSub_Click(object sender, EventArgs e)
            {
                if (this.textBoxSend.Text == string.Empty)
                { MessageBox.Show("总得说点什么吧!"); return; }
               
                //得到服务器端IP,然后建立一个连接  
                IPAddress ip;
                if (IPAddress.TryParse(this.textBoxIP.Text, out ip))
                {
                    try
                    {
                        TcpClient tcpClient = new TcpClient(this.textBoxIP.Text,8888);
                        //取得数据流  
                        _ServerIp = this.textBoxIP.Text;
                        NetworkStream clientStream = tcpClient.GetStream();
                        //只能向服务器发送字节流,所以要编码  
                        byte[] response = new byte[1024];
                        response = System.Text.Encoding.UTF8.GetBytes(this.textBoxSend.Text.ToCharArray());
                        //通过Write方法把客户端数据发向服务器  
                        clientStream.Write(response, 0, response.Length);
                        //通过Read方法读取过来的数据  
                        if (this.textBoxContent.Text == string.Empty)
                            this.textBoxContent.AppendText("我说:" + this.textBoxSend.Text + "\n");
                        else
                            this.textBoxContent.AppendText(Environment.NewLine + "我说:" + this.textBoxSend.Text + "\n");
                        clientStream.Close();
                        tcpClient.Close();
                    }
                    catch 
                    {
                        MessageBox.Show("无法访问对方主机");
                    }
                }
                else { MessageBox.Show("非法的IP地址!"); }
            }