while (true)
            {
                mySocket = listener.Accept();//为新建连接创建新的Socket。
                   Log("Get a connect");
                _thread3 = new Thread(new ThreadStart(DDo));
                _thread3.Start();            }
    private void DDo()
        {
            string recvStr = "";
            byte[] recvBytes1 = new byte[1024];
            byte[] recvBytes2 = new byte[1024];
            recvStr = null;
            bool con = false;
            string filePath = "";
            try
            {
                while (true)
                {
                    byte[] recvBytes = new byte[1024];
                    int bytes = 0 ;
                    if (mySocket.Connected == false)
                        {
                            continue;
                        }
                        bytes = mySocket.Receive(recvBytes);//从客户端接受信息
                 }
            }
            .................
            .................以下是相关的操作
            .................
}如上所示: 如果我接受大数据(十M)的数据的同时,有个连接进来,程序就回出错。如何解决并发的问题.          希望高手帮忙解决一下,以前没接触过SOCKET.

解决方案 »

  1.   

    对Socket的操作都必须Lock
    因为Socket不支持真正的并发
      

  2.   

    如何给SOCKET加上锁呢??具体有那些要求?
      

  3.   


    private static volatile Logger _logger;
    public static Logger DefaultLogger
    {
    get
    {
    if (_logger == null)
    {
    lock (syncObj)
    {
    if (_logger == null)
    {
    _logger = new Logger();
    }
    }
    }return _logger;
    }
      

  4.   

    由于小弟是刚接触SOCKET不是很懂,楼上给出的加锁,能否给出例子,有加锁就有解锁.
      

  5.   

    我现在碰到的问题是,好象程序块锁不住,就是局域变量的问题,初始变量con=false,a进程访问了变量 con=true; b进程访问了变量开始应该CON=FALSE 但一开始就受A进程影响了`.
      

  6.   

    在DDo() 中加琐
    public void DDo()
    {
    Monitor.Enter(this);//当前对象加锁  //操作代码 Monitor.Exit(this);//当前对象解锁 
    }