代码如下,主要是ServiceClient() 线程里面 while(true)这个循环造成的,不知道怎么写服务器端程序,求仙人指路。
尝试在ServiceClient()线程里面,让线程睡眠,但是这样出现客户端发来的数据,服务器端好长时间才可以收到。高人有什么其他的服务器端详细写法,贴上来。using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.Net;/// <summary>
///Service 的摘要说明
/// </summary>
public class TCPService
{    static int listenport = 2223;
    Socket clientsocket;    
    List<Client> clients;
    Thread clientservice;
    Thread threadListen;
    static bool IsBegin = false;    public TCPService()
    {
        if (IsBegin == false)
        {
            clients = new List<Client>();
            threadListen = new Thread(new ThreadStart(StartListening));
            threadListen.Start();
            IsBegin = true;
        }
    }    private void StartListening()
    {
        TcpListener listener = new TcpListener(listenport);
        listener.Start();
        while (true)
        {
            try
            {
                Socket s = listener.AcceptSocket();
                clientsocket = s;
                clientservice = new Thread(new ThreadStart(ServiceClient));
                clientservice.Start();
            }
            catch (Exception ex)
            {
                
            }
        }
    }
    private void ServiceClient()
    {
        Socket client = clientsocket;        if (client.Connected)
        {
            //在这里不停的监听客户端发送的数据所以cpu是100%已经被使用
            while (true)
            {
                Byte[] buffer = new Byte[1024];
                int bufLen = 0;
                try
                {
                    bufLen = client.Available;
                    client.Receive(buffer, 0, bufLen, SocketFlags.None);
                    if (bufLen == 0) {
                         continue;
                      
                    }
                       
                }
                catch (Exception ex)
                {
                    return;
                }
                //获取客户端消息
                string clientcommand = System.Text.Encoding.UTF8.GetString(buffer).Substring(0, bufLen);
               
            }
        }
    }
       private void SendToClient(Client cl, string clientCommand)
    {
        Byte[] message = System.Text.Encoding.UTF8.GetBytes(clientCommand);
        Socket s = cl.Sock;
        if (s.Connected)
        {
            s.Send(message, message.Length, 0);
        }
    }}

解决方案 »

  1.   


                    Byte[] buffer = new Byte[1024];
                    int bufLen = 0;
                    try
                    {
                        bufLen = client.Available;
                        client.Receive(buffer, 0, bufLen, SocketFlags.None);
                        if (bufLen == 0) {
                             continue;
                          
                        }
                           
                    }修改为                Byte[] buffer = new Byte[1024];
                    try
                    {
                        int buflen = client.Receive(buffer, 0, 1024, SocketFlags.None);
                        if (bufLen == 0) {
                             return;
                          
                        }
                           
                    }
      

  2.   

    应该是这个Socket通讯服务器问题吧
      

  3.   

    仅仅一个客户端连接,就已经100%了。lizhibin11 的方法真的解决问题了lizhibin11 是真神仙呀。
    全给分了。能否在这里给大家说一下为什么要这样改
      

  4.   

    bufLen = client.Available;这句在没有数据时始终返回0,此时client.Receive(buffer, 0, bufLen, SocketFlags.None)试图接收零字节的数据,也会立即返回,无法阻塞,因此长时间陷入死循环。
      

  5.   

    大仙 lizhibin11  ,你的这个方法解决了问题了。。
    求大仙,略讲原理,,,我的代码是从网上抄来的。所以有些地方还是不太懂如果有其他 高人讲为什么会是100%,另外分数也送上了
      

  6.   


     bool isWatch = true;        #region 1.被线程调用 监听连接端口
            /// <summary>
            /// 被线程调用 监听连接端口
            /// </summary>
            void StartWatch()
            {
                while (isWatch)
                {
                    //threadWatch.SetApartmentState(ApartmentState.STA);
                    //监听 客户端 连接请求,但是,Accept会阻断当前线程
                    Socket sokMsg = sokWatch.Accept();//监听到请求,立即创建负责与该客户端套接字通信的套接字
                    ConnectionClient connection = new ConnectionClient(sokMsg, ShowMsg, RemoveClientConnection);
                    //将负责与当前连接请求客户端 通信的套接字所在的连接通信类 对象 装入集合
                    dictConn.Add(sokMsg.RemoteEndPoint.ToString(), connection);
                    //将 通信套接字 加入 集合,并以通信套接字的远程IpPort作为键
                    //dictSocket.Add(sokMsg.RemoteEndPoint.ToString(), sokMsg);
                    //将 通信套接字的 客户端IP端口保存在下拉框里
                    cboClient.Items.Add(sokMsg.RemoteEndPoint.ToString());
                    ShowMsg("接收连接成功......");
                    //启动一个新线程,负责监听该客户端发来的数据
                    //Thread threadConnection = new Thread(ReciveMsg);
                    //threadConnection.IsBackground = true;
                    //threadConnection.Start(sokMsg);
                }
            } 
            #endregion
      

  7.   

    lizhibin11 神仙回复速度好神速呀,,,不愧为大仙。分数全给你了
      

  8.   

    已经结贴了。。还能否问 lizhibin11 一个问题。
    我的上面代码能否使用阻塞的方法。
      

  9.   

    你这个写法本身就是阻塞的。socket.receive是阻塞方法。
      

  10.   

    我说你一开始的无法阻塞,是指你那个buflen始终返回零,导致socket.receive方法的第三个参数始终为零,当它为零的时候,socket.receive会立即返回,无法阻塞——因为你要接收的字节数为零。