各位老大帮我看下这个问题,我想做一个SOCKET网络聊天程序,分服务器端和客户端,服务器可以连接多个客户端,现在服务器端可以连接一个客户端,当再另打开一个客户端进行连接却没反应了。我在服务器端定义了一个Socket类数组m_clientList,当服务器启动时,定义一个socket s绑定在6000端口,并初始化Socket数组m_clientList然后 thread=new Thread(new ThreadStart(AccRev));//开始线程
AccRev函数如下
public  void AccRev()
{
int intClientIndex;
Thread.CurrentThread.IsBackground = true;
intClientIndex = GetFreeClient(); 
if (intClientIndex != -1) 
{
m_clientList[intClientIndex].IsIdle = false; 
Socket clientSocket = s.Accept(); 
IPEndPoint remoEP = (IPEndPoint)clientSocket.RemoteEndPoint; 
                    
m_clientList[intClientIndex].ClientIP = remoEP.Address.ToString(); 
m_clientList[intClientIndex].ClientSocket = clientSocket; 
//SetText("connecting from " + m_clientList[intClientIndex].ClientIP); 
this.listBox1.Items.Add("connecting from " + m_clientList[intClientIndex].ClientIP);

byte[] buffer = new byte[1024]; //定义接受字节数字 try 

while (m_clientList[intClientIndex].ClientSocket.Connected == true) 
{  m_clientList[intClientIndex].ClientSocket.Receive(buffer,buffer.Length,0);
string ss=System.Text.Encoding.BigEndianUnicode.GetString(buffer);//将接收到得字符
this.listBox1.Items.Add(ss.Trim()); } 
}  catch (SocketException ex) 

MessageBox.Show(ex.ToString()); 
}  catch (Exception ex) 

MessageBox.Show(ex.ToString()); 

} }请各位帮我看看问题出在哪?当有客户端进行连接时会触发服务器的什么事件吗??

解决方案 »

  1.   

    this.listBox1.Items.Add("connecting from " + m_clientList[intClientIndex].ClientIP);
    这句不会报错吗?
    在Socket clientSocket = s.Accept(); 这句上还有有个while(true)的循环才能持续的接收请求的
      

  2.   


    public void AccRev()
            {
                int intClientIndex;
                Thread.CurrentThread.IsBackground = true;
                intClientIndex = GetFreeClient();
                if (intClientIndex != -1)
                {
                    m_clientList[intClientIndex].IsIdle = false;
                    while (true)
                    {
                        Socket clientSocket = s.Accept();
                        IPEndPoint remoEP = (IPEndPoint)clientSocket.RemoteEndPoint;                    m_clientList[intClientIndex].ClientIP = remoEP.Address.ToString();
                        m_clientList[intClientIndex].ClientSocket = clientSocket;
                        //SetText("connecting from " + m_clientList[intClientIndex].ClientIP); 
                        this.listBox1.Items.Add("connecting from " + m_clientList[intClientIndex].ClientIP);                    byte[] buffer = new byte[1024]; //定义接受字节数字                    try
                        {
                            while (m_clientList[intClientIndex].ClientSocket.Connected == true)
                            {                            m_clientList[intClientIndex].ClientSocket.Receive(buffer, buffer.Length, 0);
                                string ss = System.Text.Encoding.BigEndianUnicode.GetString(buffer);//将接收到得字符
                                this.listBox1.Items.Add(ss.Trim());                        }
                        }                    catch (SocketException ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }                    catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }            }        }
    试试这个
      

  3.   

    加了while(true)还是不能解决问题啊,我想来想去应该是没有增加新的接受连接的线程导致的,所以我现在修改成下面,基本还凑合了,但是这样做老感觉心里不踏实,请各位帮忙给看看有什么问题存在 public  void AccRev()
    {
    int intClientIndex;
    Thread.CurrentThread.IsBackground = true; while (true) 

    intClientIndex = GetFreeClient(); 
    if (intClientIndex != -1) 
    {

    Socket clientSocket = s.Accept(); 

    thread=new Thread(new ThreadStart(AccRev));//开始新线程监听
    thread.Start();
    IPEndPoint remoEP = (IPEndPoint)clientSocket.RemoteEndPoint; 
    m_clientList[intClientIndex].IsIdle = false;                
    m_clientList[intClientIndex].ClientIP = remoEP.Address.ToString(); 
    m_clientList[intClientIndex].ClientSocket = clientSocket; 
    //SetText("connecting from " + m_clientList[intClientIndex].ClientIP); 
    this.listBox1.Items.Add("connecting from " + m_clientList[intClientIndex].ClientIP);
    //发一个消息给首个连接的客户端
    if(intClientIndex==0)
    {
    SendMessage(m_clientList[intClientIndex]);
    }

    byte[] buffer = new byte[1024]; //定义一个接受字节数字 try 

    while (m_clientList[intClientIndex].ClientSocket.Connected == true) 
    {  m_clientList[intClientIndex].ClientSocket.Receive(buffer,buffer.Length,0);
    string ss=System.Text.Encoding.BigEndianUnicode.GetString(buffer);//将接收到得字符存储
    ss="["+m_clientList[intClientIndex].ClientIP+"]"+ss;
    this.listBox1.Items.Add(ss.Trim());
     } 
    }  catch (SocketException ex) 

    MessageBox.Show(ex.ToString()); 
    }  catch (Exception ex) 

    MessageBox.Show(ex.ToString()); 

    }
    } }
      

  4.   

    private void ListenClientConnect()
            {
                while (true)
                {
                    TcpClient newClient = null;
                    try
                    {
                        //等待用户进入
                        newClient = myListener.AcceptTcpClient();
                    }
                    catch
                    {
                        //当单击“停止监听”或者退出此窗体时AcceptTcpClient()会产生异常
                        //因此可以利用此异常退出循环
                        break;
                    }
                    //每接受一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息
                    ParameterizedThreadStart pts = new ParameterizedThreadStart(ReceiveData);
                    Thread threadReceive = new Thread(pts);
    参考一下这个,你的代码乱七八糟的我看不明白
      

  5.   

    你虽然在外面while(true)了,你没看见里面也有个while(true),他就在里面循环了,等到第一个断开外层才会循环,这么弱智的问题,你应该对每个客户端再新建一个线程,或者使用beginreceive标明接收回调
      

  6.   

     this.listBox1.Items.Add("connecting from " + m_clientList[intClientIndex].ClientIP);
        这里没报错  奇怪了  这个挂线程了   用委托   public  void AccRev()
            {
                int intClientIndex;
                Thread.CurrentThread.IsBackground = true;
                while (true) 
                { 
                    intClientIndex = GetFreeClient(); 
                    if (intClientIndex != -1) 
                    {
                        while(true)
                        {
                        Socket clientSocket = s.Accept(); 
                        thread=new Thread(new ThreadStart(StartReceive));//开始新线程监听
                           thread.Start();
                        }
                    }
            }
           public void   StartReceive()
           {             IPEndPoint remoEP = (IPEndPoint)clientSocket.RemoteEndPoint; 
                 m_clientList[intClientIndex].IsIdle = false;                
                 m_clientList[intClientIndex].ClientIP = remoEP.Address.ToString(); 
                 m_clientList[intClientIndex].ClientSocket = clientSocket; 
                 //SetText("connecting from " + m_clientList[intClientIndex].ClientIP); 
                this.listBox1.Items.Add("connecting from " + m_clientList[intClientIndex].ClientIP);
                 //发一个消息给首个连接的客户端
                   if(intClientIndex==0)
                 {
                      SendMessage(m_clientList[intClientIndex]);
                 }
                                
                 byte[] buffer = new byte[1024]; //定义一个接受字节数字               try 
                 { 
                     while (m_clientList[intClientIndex].ClientSocket.Connected == true) 
                     {            m_clientList[intClientIndex].ClientSocket.Receive(buffer,buffer.Length,0);
              string ss=System.Text.Encoding.BigEndianUnicode.GetString(buffer);//将接收到得字符存储
                     ss="["+m_clientList[intClientIndex].ClientIP+"]"+ss;
                   this.listBox1.Items.Add(ss.Trim());
                        } 
              }                     catch (SocketException ex) 
                        { 
                            MessageBox.Show(ex.ToString()); 
                        }                     catch (Exception ex) 
                        { 
                            MessageBox.Show(ex.ToString()); 
                        } 
                    }}