select是一种比较古老但一直被证明性能很好的socket模式,它可以让你以消息驱动的模式书写socket程序。网上C++的例子很多,但C#的例子极少。
上代码:
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("0.0.0.0");//0.0.0.0表示监听本机的所有IP
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Bi