C # Use the select method to implement the socket server,

Source: Internet
Author: User

C # Use the select method to implement the socket server,

Select is an old but proven socket mode that allows you to write socket programs in message-driven mode. There are many examples of C ++ on the Internet, but there are very few examples of C.

Code:

Namespace Server {class Program {// Thread signal. public static ManualResetEvent allDone = new ManualResetEvent (false); private static Socket handler = null; private static ArrayList g_CliSocketArr = new ArrayList (); private static Object thisLock = new Object (); public Program () {} public static void StartListening () {// Data buffer for incoming data. byte [] bytes = new Byte [1024]; IPAddress ipAddress = IPAddress. parse ("192.168.1.71"); // IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000); // Create a TCP/IP socket. socket listener = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // Bind the socket to the local endpoint and listen for incoming connections. try {listener. bind (localEndPoint); listener. listen (100); // Start an asynchronous socket to listen for connections. console. writeLine ("Waiting for a connection... "); Thread worker = new Thread (new ThreadStart (WorkerThread); // create a Thread to process the worker request. start (); while (true) {Socket sClient = listener. accept (); Console. writeLine ("There is a new connection. "); g_CliSocketArr.Add (sClient) ;}} catch (Exception e) {Console. writeLine (e. toString ();} Console. writeLine ("\ nPress ENTER to continue... "); Console. read ();} public static void WorkerThread () {Socket socket1 = null; ArrayList readList = new ArrayList (); // readList. add (socket0); while (true) {lock (thisLock) {readList. clear (); for (int I = 0; I <g_CliSocketArr.Count; I ++) {readList. add (g_CliSocketArr [I]) ;}} if (readList. count <= 0) {Thread. sleep (100); continue;} try {Socket. select (read list, null, null, 500); for (int I = 0; I <readList. count; I ++) {socket1 = (Socket) readList [I]; Console. writeLine ("There is a new message from client. "); byte [] buffer = new byte [1024]; int recLen = socket1.Receive (buffer); if (recLen> 0) {// recLen = socket1.Receive (buffer );} else {// If 0 is returned, the client has been disconnected. You must disable this socket and then clear the Console from the connection pool. writeLine ("Rece 0 length. "); for (int ii = 0; ii <g_CliSocketArr.Count; ii ++) {Socket s = (Socket) g_CliSocketArr [ii]; if (s = socket1) g_CliSocketArr.RemoveAt (ii);} socket1.Shutdown (SocketShutdown. both); socket1.Close (); break;} socket1.Send (buffer, recLen, SocketFlags. none) ;}} catch (SocketException e) {Console. writeLine ("{0} Error code: {1 }. ", e. message, e. errorCode); for (int ii = 0; ii <g_CliSocketArr.Count; ii ++) {Socket s = (Socket) g_CliSocketArr [ii]; if (s = socket1) g_CliSocketArr.RemoveAt (ii);} socket1.Shutdown (SocketShutdown. both); socket1.Close () ;}} static void Main (string [] args) {StartListening ();}}}



C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.