日期:2014-05-16  浏览次数:21174 次

新手求高手指点,TCP服务端发不出去数据,万分感激
初学,刚接触C#和TCP。
小软件,


TCP协议,做服务端,可以接收到客户端发来的数据,但是给客户端发数据出错。
求高手指点,非常感谢!
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;
using System.Net.Sockets;
using System.Threading;


namespace server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket sockets = null;
        Thread thread = null;

        private void button1_Click(object sender, EventArgs e)
        {
            thread = new Thread(new ThreadStart(ThreadCallBack));
            thread.Start();
        }

        private void ThreadCallBack()
        {
            sockets = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress hostIP = IPAddress.Parse(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString());
            IPEndPoint ep = new IPEndPoint(0, 2000);

            sockets.Bind(ep); 
            sockets.Listen(10);
            textBox1.BeginInvoke(new MethodInvoker(delegate
            {
                textBox1.Text += "侦听启动\r\n";
            }));

            while (thread.ThreadState != ThreadState.Aborted)
            {
                Socket sReceive = sockets.Accept();
                
                int bufLen = sReceive.ReceiveBufferSize;
                if (bufLen == 0) continue;
                byte[] bData = new byte[bufLen];
 &nb