Code Bye

C# Socket 服务端 接受 ios Socket 客户端 连接出现问题

参考网络上的写法,服务端和客户端都没有问题。
但是,一旦ios 客户端接入(接入是正常的,能正常的收到信息),再断开。问题就来了,表现如下:
服务器的cpu使用率 开始狂升,导致服务器卡死。
现在不知道是本人服务端的问题,还是ios客户端的问题。
请大婶们解惑。附上代码:
服务端:
public partial class ServerMain : Form
    {
        public ServerMain()
        {
            InitializeComponent();
        }
        private void ServerMain_Load(object sender, EventArgs e)
        {
            this.CmdStar.Enabled = true;
            this.CmdStop.Enabled = false;
        }
        //声明将要用到的类
        private IPEndPoint ServerInfo;//存放服务器的IP和端口信息
        private Socket ServerSocket;//服务端运行的SOCKET
        private Thread ServerThread;//服务端运行的线程
        private Socket[] ClientSocket;//为客户端建立的SOCKET连接
        private int ClientNumb;//存放客户端数量
        private byte[] MsgBuffer;//存放消息数据
        private void CmdStar_Click(object sender, EventArgs e)
        {
            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //提供一个 IP 地址,指示服务器应侦听全部网络接口上的客户端活动
            IPAddress ip = IPAddress.Any;
            ServerInfo = new IPEndPoint(ip, 6600);
            ServerSocket.Bind(ServerInfo);//将SOCKET接口和IP端口绑定
            ServerSocket.Listen(10);//开始监听,并且挂起数为10
            ClientSocket = new Socket[65535];//为客户端提供连接个数
            MsgBuffer = new byte[65535];//消息数据大小
            ClientNumb = 0;//数量从0开始统计
            ServerThread = new Thread(new ThreadStart(RecieveAccept));//将接受客户端连接的方法委托给线程
            ServerThread.Start();//线程开始运行
            CheckForIllegalCrossThreadCalls = false;//不捕获对错误线程的调用
            this.CmdStar.Enabled = false;
            this.CmdStop.Enabled = true;
            this.ClientList.Items.Add("服务于 " + DateTime.Now.ToString() + " 开始运行.");
        }
        //接受客户端连接的方法
        private void RecieveAccept()
        {
            while (true)
            {
                //Accept 以同步方式从侦听套接字的连接请求队列中提取第一个挂起的连接请求,然后创建并返回新的 Socket。
                //在阻止模式中,Accept 将一直处于阻止状态,直到传入的连接尝试排入队列。连接被接受后,原来的 Socket 继续将传入的连接请求排入队列,直到您关闭它。
                ClientSocket[ClientNumb] = ServerSocket.Accept();
                ClientSocket[ClientNumb].BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), ClientSocket[ClientNumb]);
                lock (this.ClientList)
                {
                    this.ClientList.Items.Add(ClientSocket[ClientNumb].RemoteEndPoint.ToString() + " 成功连接服务器.");
                }
                ClientNumb++;
            }
        }
        //回发数据给客户端
        private void RecieveCallBack(IAsyncResult AR)
        {
            try
            {
                Socket RSocket = (Socket)AR.AsyncState;
                int REnd = RSocket.EndReceive(AR);
                //对每一个侦听的客户端端口信息进行接收和回发
                for (int i = 0; i < ClientNumb; i++)
                {
                    try
                    {
                        if (ClientSocket[i].Connected)
                        {
                            //回发数据到客户端
                            ClientSocket[i].Send(MsgBuffer, 0, REnd, SocketFlags.None);
                        }
                        else
                        {
                            //把客户端标记为关闭
                            ClientSocket[i].Disconnect(true);
                        }
                        //同时接收客户端回发的数据,用于回发
                        RSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket);
                    }
                    catch
                    {
                        //把客户端标记为关闭
                        ClientSocket[i].Disconnect(true);
                    }
                }
            }
            catch
            {
            }
        }
        private void CmdStop_Click(object sender, EventArgs e)
        {
            ServerThread.Abort();//线程终止
            ServerSocket.Close();//关闭socket
            this.CmdStar.Enabled = true;
            this.CmdStop.Enabled = false;
            this.ClientList.Items.Add("服务于 " + DateTime.Now.ToString() + " 停止运行.");
        }
        private void ServerMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            ServerThread.Abort();//线程终止
            ServerSocket.Close();//关闭SOCKET
            Application.Exit();
        }
    }

本人的c# 客户端,可以正常使用

public partial class ClientMain : Form
    {
        public ClientMain()
        {
            InitializeComponent();
        }
        private IPEndPoint ServerInfo;
        private Socket ClientSocket;
        //信息接收缓存
        private Byte[] MsgBuffer;
        //信息发送存储
        private Byte[] MsgSend;
        private void ClientMain_Load(object sender, EventArgs e)
        {
            this.CmdSend.Enabled = false;
            this.CmdExit.Enabled = false;
            this.ServerIP.Text = "127.0.0.1";
            this.ServerPort.Text = "6600";
            //定义一个IPV4,TCP模式的Socket
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            MsgBuffer = new Byte[65535];
            MsgSend = new Byte[65535];
            //允许子线程刷新数据
            CheckForIllegalCrossThreadCalls = false;
            this.UserName.Text = Environment.MachineName;
        }
        private void CmdEnter_Click(object sender, EventArgs e)
        {
            //服务端IP和端口信息设定,这里的IP可以是127.0.0.1,可以是本机局域网IP,也可以是本机网络IP
            ServerInfo = new IPEndPoint(IPAddress.Parse(this.ServerIP.Text), Convert.ToInt32(this.ServerPort.Text));
            try
            {
                //客户端连接服务端指定IP端口,Sockket
                ClientSocket.Connect(ServerInfo);
                //将用户登录信息发送至服务器,由此可以让其他客户端获知
                //ClientSocket.Send(Encoding.UTF8.GetBytes("用户: " + this.UserName.Text + " 进入系统!\n"));
                //开始从连接的Socket异步读取数据。接收来自服务器,其他客户端转发来的信息
                //AsyncCallback引用在异步操作完成时调用的回调方法
                ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);
                this.SysMsg.Text += "登录服务器成功!\n";
                this.CmdSend.Enabled = true;
                this.CmdEnter.Enabled = false;
                this.CmdExit.Enabled = true;
            }
            catch
            {
                MessageBox.Show("登录服务器失败,请确认服务器能否正常工作!");
            }
        }
        private void ReceiveCallBack(IAsyncResult AR)
        {
            try
            {
                //结束挂起的异步读取,返回接收到的字节数。 AR,它存储此异步操作的状态信息以及全部用户定义数据
                int REnd = ClientSocket.EndReceive(AR);
                lock (this.RecieveMsg)
                {
                    this.RecieveMsg.AppendText(Encoding.UTF8.GetString(MsgBuffer, 0, REnd));
                }
                ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
            }
            catch
            {
                MessageBox.Show("已经与服务器断开连接!");
                this.Close();
            }
        }
        private void CmdSend_Click(object sender, EventArgs e)
        {
            MsgSend = Encoding.UTF8.GetBytes(this.UserName.Text + "说:\n" + this.SendMsg.Text + "\n");
            if (ClientSocket.Connected)
            {
                //将数据发送到连接的 System.Net.Sockets.Socket。
                ClientSocket.Send(MsgSend);
                this.SendMsg.Text = "";
            }
            else
            {
                MessageBox.Show("当前与服务器断开连接,无法发送信息!");
            }
        }
        private void CmdExit_Click(object sender, EventArgs e)
        {
            if (ClientSocket.Connected)
            {
                ClientSocket.Send(Encoding.UTF8.GetBytes(this.UserName.Text + "离开了房间!\n"));
                //禁用发送和接受
                ClientSocket.Shutdown(SocketShutdown.Both);
                //关闭套接字,不允许重用
                ClientSocket.Disconnect(false);
            }
            ClientSocket.Close();
            this.CmdSend.Enabled = false;
            this.CmdEnter.Enabled = true;
            this.CmdExit.Enabled = false;
        }
        private void RecieveMsg_TextChanged(object sender, EventArgs e)
        {
            this.RecieveMsg.ScrollToCaret();
        }
        private void SendMsg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyValue == 13)
            {
                e.Handled = true;
                this.CmdSend_Click(this, null); 
            }
        }
    }
解决方案

50


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# Socket 服务端 接受 ios Socket 客户端 连接出现问题