[置顶]C#中Socket服务端代码分享

摘要:
最近,该项目中套接字通信中的服务器代码进行了优化,以确保它可以接受尽可能多的客户端连接,而不会丢失连接和数据包。经过一段时间的反复测试和修改,最终满足了这一要求。服务器代码采用异步通信模式,并使用ManualResetEvent控制线程。ManualResetEvent允许线程通过信令相互通信。在ManualResetEvent上调用WaitOne的线程将阻塞并等待信号。ManualResetEvent的初始状态可以通过将布尔值传递给构造函数来控制。如果初始状态处于终止状态,则为真;否则,这是错误的。

最近在对项目中Socket通讯中的服务端代码进行优化,保证能接受尽可能多的客户端的连接,并且不会丢掉连接,不会掉数据包。经过一段时间的反复测试和修改,终于达到了这一要求。服务端代码采用了异步通讯的方式,并使用ManualResetEvent来对线程进行控制。在程序中,ManualResetEvent 的使用很关键。 ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态,此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。一旦它被终止,ManualResetEvent 将保持终止状态(即对 WaitOne 的调用的线程将立即返回,并不阻塞),直到它被手动重置。可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false。现在贴出主要的代码,欢迎大家指正,代码如下所示:

void ButtonStartListenClick(object sender, System.EventArgs e)
        {
            try
            {
                // Check the port value
                if(textBoxPort.Text == "")
                {
                    MessageBox.Show("Please enter a Port Number");
                    return;
                }
                if (txtConnectNum.Text.Trim() != "")
                {
                    iConnectNum = int.Parse(txtConnectNum.Text.Trim());
                    Flage = 0;
                }
                else
                {
                    MessageBox.Show("Please enter a Connect Number");
                    return;
                }

                string portStr = textBoxPort.Text;
                int port = System.Convert.ToInt32(portStr);
                // Create the listening socket...
                m_mainSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);
                IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port);
                // Bind to local IP Address...
                m_mainSocket.Bind( ipLocal );
                // Start listening...
                m_mainSocket.Listen(10000);
                // Set the event to nonsignaled state.设置无信号状态的事件
                allDone.Reset();
                // Create the call back for any client connections...
                m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
                // Wait until a connection is made before continuing.等待连接创建后继续
                allDone.WaitOne();
                
                UpdateControls(true);
                
            }
            catch(SocketException se)
            {
                MessageBox.Show ( se.Message );
            }

        }


// This is the call back function, which will be invoked when a client is connected
        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                //让它继续处理并建立与客户端的连接
                allDone.Set();

                // Here we complete/end the BeginAccept() asynchronous call
                // by calling EndAccept() - which returns the reference to
                // a new Socket object
                Socket workerSocket = m_mainSocket.EndAccept (asyn);

                // Now increment the client count for this client
                // in a thread safe manner
                Interlocked.Increment(ref m_clientCount);

                if (m_clientCount == 1)
                {
                    lock (this)
                    {
                        stopwatch.Start();
                        dtStart = DateTime.Now;
                        writeLog("Server Start Socket Connect Time"+dtStart.ToString("yyyy-MM-dd HH:mm:ss fff"));
                    }
                }
                
                // Add the workerSocket reference to our ArrayList
                m_workerSocketList.Add(workerSocket);

                // Send a welcome message to client
                string msg = "Welcome client " + m_clientCount + "\n";
                SendMsgToClient(msg, m_clientCount);

                // Update the list box showing the list of clients (thread safe call)
                UpdateClientListControl();

                // Let the worker Socket do the further processing for the
                // just connected client
                WaitForData(workerSocket, m_clientCount);
                            
                // Since the main Socket is now free, it can go back and wait for
                // other clients who are attempting to connect
                m_mainSocket.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
                // Wait until a connection is made before continuing.等待连接创建后继续
                allDone.WaitOne();
            }
            catch(ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0,"1","\n OnClientConnection: Socket has been closed\n");
            }
            catch(SocketException se)
            {
                MessageBox.Show ( se.Message );
            }
            
        }


public class SocketPacket
        {
            // Constructor which takes a Socket and a client number
            public SocketPacket(System.Net.Sockets.Socket socket, int clientNumber)
            {
                m_currentSocket = socket;
                m_clientNumber  = clientNumber;
            }
            public System.Net.Sockets.Socket m_currentSocket;
            public int m_clientNumber;
            // Buffer to store the data sent by the client
            public byte[] dataBuffer = new byte[1024];
        }
        // Start waiting for data from the client
        public void WaitForData(System.Net.Sockets.Socket soc, int clientNumber)
        {
            try
            {
                if  ( pfnWorkerCallBack == null )
                {        
                    // Specify the call back function which is to be
                    // invoked when there is any write activity by the
                    // connected client
                    pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
                }
                SocketPacket theSocPkt = new SocketPacket (soc, clientNumber);
                //receiveDone.Reset();
                soc.BeginReceive (theSocPkt.dataBuffer, 0,
                    theSocPkt.dataBuffer.Length,
                    SocketFlags.None,
                    pfnWorkerCallBack,
                    theSocPkt);
                //receiveDone.WaitOne();
            }
            catch(SocketException se)
            {
                MessageBox.Show (se.Message );
            }
        }
        // This the call back function which will be invoked when the socket
        // detects any client writing of data on the stream
        public  void OnDataReceived(IAsyncResult asyn)
        {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState ;
            try
            {
                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client
                //receiveDone.Set();
                int iRx  = socketData.m_currentSocket.EndReceive (asyn);
                char[] chars = new char[iRx +  1];
                // Extract the characters as a buffer
                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int charLen = d.GetChars(socketData.dataBuffer,
                    0, iRx, chars, 0);

                System.String szData = new System.String(chars);
                string msg = "" + socketData.m_clientNumber + ":";
                AppendToRichEditControl(msg + szData);
                //writeFromClientsMsgLog(msg + szData);

                // Send back the reply to the client
                string replyMsg = "Server Reply:" + szData.ToUpper();
                // Convert the reply to byte array
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg);

                Socket workerSocket = (Socket)socketData.m_currentSocket;
                workerSocket.Send(byData);


                if (m_clientCount == iConnectNum && Flage == 0)
                {
                    Interlocked.Increment(ref Flage);
                    string msgTime = "Server End Socket Action Time:";
                    lock(this)
                    {
                        stopwatch.Stop();
                        //lblTime.Text = stopwatch.Elapsed.Seconds.ToString();
                        int itime = stopwatch.Elapsed.Milliseconds;
                        
                        //msgTime += stopwatch.Elapsed.Seconds.ToString()+"--"+itime.ToString();
                        msgTime += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
                    }
                    writeLog(msgTime);

                    writeClientConnectionLog();
                    
                    //UpdateLabelTime(msgTime);
                    //byData = System.Text.Encoding.ASCII.GetBytes(msgTime);
                    //workerSocket.Send(byData);
                }
    
                // Continue the waiting for data on the Socket
                WaitForData(socketData.m_currentSocket, socketData.m_clientNumber );

            }
            catch (ObjectDisposedException )
            {
                System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
            }
            catch(SocketException se)
            {
                if(se.ErrorCode == 10054) // Error code for Connection reset by peer
                {    
                    string msg = "Client " + socketData.m_clientNumber + " Disconnected" + "\n";
                    AppendToRichEditControl(msg);
                    //writeFromClientsMsgLog(msg);

                    // Remove the reference to the worker socket of the closed client
                    // so that this object will get garbage collected
                    m_workerSocketList[socketData.m_clientNumber - 1] = null;
                    UpdateClientListControl();
                }
                else
                {
                    MessageBox.Show (se.Message );
                }
            }
        }


void CloseSockets()
        {
            if(m_mainSocket != null)
            {
                m_mainSocket.Close();
            }
            Socket workerSocket = null;
            for(int i = 0; i < m_workerSocketList.Count; i++)
            {
                workerSocket = (Socket)m_workerSocketList[i];
                if(workerSocket != null)
                {
                    workerSocket.Close();
                    workerSocket = null;
                }
            }    
        }

void SendMsgToClient(string msg, int clientNumber)
        {
            // Convert the reply to byte array
            byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg);

            Socket workerSocket = (Socket)m_workerSocketList[clientNumber - 1];
            //workerSocket.Send(byData);
            workerSocket.BeginSend(byData, 0, byData.Length, 0,
                new AsyncCallback(SendCallback), workerSocket);
        }

        private void SendCallback(IAsyncResult asyn)
        {
            Socket client = (Socket)asyn.AsyncState;
            // 完成数据发送.  
            int bytesSent = client.EndSend(asyn);  
        }

完整的代码可以在我的资源中下载到,资源的名称为C#中Socket服务端通讯的代码


免责声明:文章转载自《[置顶]C#中Socket服务端代码分享》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[接口]mmc/eMMC/SD-card在Java中调用Python下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java socket 实现多个一对一聊天

此程序能够实现同一网络下多个一对一聊天,必须服务器先启动 ,然后客户端启动并且服务器ip要填正确,并且每个客户端的自身编号必须唯一。 服务器端: packagecn.com.test09; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.io.IOExce...

【移动开发】WIFI热点通信(二)

相信大家在上一篇中已经了解了Android中WIFI热点通信的相关操作知识(http://smallwoniu.blog.51cto.com/3911954/1536126),今天我们将在上一篇代码基础之上进行Socket编程,实现一个简单的多人聊天室功能,以达到热点网络上的通信目的。     首先,我们先来看一张最终效果图: <=======>...

[Python之路] 多种方式实现并发Web Server

下面我们使用Python来实现并发的Web Server,其中采用了多进程、多线程、协程、单进程单线程非阻塞的方式。 一、使用子进程来实现并发Web Server 参照 https://www.cnblogs.com/leokale-zz/p/11949208.html 中的代码,我们将其修改为支持并发的简单Web Server: import socke...

C# Socket网络编程精华篇

我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次模型     当然这里我们只讨论重要的四层        01,应用层(Application):应用层是个很广泛的概念,有一些基本相同的系统级TCP/IP应用以及应用协议,也有许多的企业应用和互联网应用。http协议在应用层运行。        02,传输层(Tan...

netty 实现socket服务端编写

1 importjava.net.InetSocketAddress; 2 3 importio.netty.bootstrap.ServerBootstrap; 4 importio.netty.channel.ChannelFuture; 5 importio.netty.channel.ChannelInitializer; 6 impo...

线程中断总结

1、线程中断 每个线程都有一个与之相关联的 Boolean 属性,用于表示线程的中断状态(interrupted status)。中断状态初始时为 false;当另一个线程通过调用Thread.interrupt()中断一个线程时,会出现以下两种情况之一。如果那个线程在执行一个低级可中断阻塞方法,例如Thread.sleep()、Thread.join()...