f8f
c#socket±à³Ì»ù´¡
Microsoft.Net FrameworkΪӦÓóÌĞò·ÃÎÊInternetÌṩÁË·Ö²ãµÄ¡¢¿ÉÀ©Õ¹µÄÒÔ¼°ÊܹÜϽµÄÍøÂç·şÎñ£¬ÆäÃû×Ö¿Õ¼äSystem.NetºÍSystem.Net.Sockets°üº¬·á¸»µÄÀà¿ÉÒÔ¿ª·¢¶àÖÖÍøÂçÓ¦ÓóÌĞò¡£.NetÀà²ÉÓõķֲã½á¹¹ÔÊĞíÓ¦ÓóÌĞòÔÚ²»Í¬µÄ¿ØÖƼ¶±ğÉÏ·ÃÎÊÍøÂ磬¿ª·¢ÈËÔ±¿ÉÒÔ¸ù¾İĞèҪѡÔñÕë¶Ô²»Í¬µÄ¼¶±ğ±àÖƳÌĞò£¬ÕâĞ©¼¶±ğ¼¸ºõÄÒÀ¨ÁËInternetµÄËùÓĞĞèÒª --´ÓsocketÌ×½Ó×Öµ½ÆÕͨµÄÇëÇó/ÏìÓ¦£¬¸üÖØÒªµÄÊÇ£¬ÕâÖÖ·Ö²ãÊÇ¿ÉÒÔÀ©Õ¹µÄ£¬Äܹ»ÊÊÓ¦Internet²»¶ÏÀ©Õ¹µÄĞèÒª¡£
Å׿ªISO/OSIÄ£Ğ͵Ä7²ã¹¹¼Ü£¬µ¥´ÓTCP/IPÄ£ĞÍÉϵÄÂß¼²ãÃæÉÏ¿´£¬.NetÀà¿ÉÒÔÊÓΪ°üº¬3¸ö²ã´Î£ºÇëÇó/ÏìÓ¦²ã¡¢Ó¦ÓÃĞÒé²ã¡¢´«Êä²ã¡£ WebReqeustºÍWebResponse ´ú±íÁËÇëÇó/ÏìÓ¦²ã£¬Ö§³ÖHttp¡¢TcpºÍUdpµÄÀà×é³ÉÁËÓ¦ÓÃĞÒé²ã£¬¶øSocketÀà´¦ÓÚ´«Êä²ã¡£
´«Êä²ãλÓÚÕâ¸ö½á¹¹µÄ×îµ×²ã£¬µ±ÆäÉÏÃæµÄÓ¦ÓÃĞÒé²ãºÍÇëÇó/ÏìÓ¦²ã²»ÄÜÂú×ãÓ¦ÓóÌĞòµÄÌØÊâĞèҪʱ£¬¾ÍĞèҪʹÓÃÕâÒ»²ã½øĞĞSocketÌ×½Ó×Ö±à³Ì¡£
¶øÔÚ.NetÖĞ£¬System.Net.Sockets ÃüÃû¿Õ¼äΪĞèÒªÑÏÃÜ¿ØÖÆÍøÂç·ÃÎʵĿª·¢ÈËÔ±ÌṩÁË Windows Sockets (Winsock) ½Ó¿ÚµÄÍйÜʵÏÖ¡£System.Net ÃüÃû¿Õ¼äÖеÄËùÓĞÆäËûÍøÂç·ÃÎÊÀ඼½¨Á¢ÔÚ¸ÃÌ×½Ó×Ö SocketʵÏÖÖ®ÉÏ£¬ÈçTCPClient¡¢TCPListener ºÍ UDPClient Àà·â×°Óйش´½¨µ½ Internet µÄ TCP ºÍ UDP Á¬½ÓµÄÏêϸĞÅÏ¢£»NetworkStreamÀàÔòÌṩÓÃÓÚÍøÂç·ÃÎʵĻù´¡Êı¾İÁ÷µÈ£¬³£¼ûµÄĞí¶à Internet·şÎñ¶¼¿ÉÒÔ¼ûµ½SocketµÄ×ÙÓ°£¬ÈçTelnet¡¢Http¡¢Email¡¢EchoµÈ£¬ÕâĞ©·şÎñ¾¡¹ÜͨѶĞÒéProtocolµÄ¶¨Ò岻ͬ£¬µ«ÊÇÆä»ù´¡µÄ´«Ê䶼ÊDzÉÓõÄSocket¡£
Æäʵ£¬Socket¿ÉÒÔÏóÁ÷StreamÒ»Ñù±»ÊÓΪһ¸öÊı¾İͨµÀ£¬Õâ¸öͨµÀ¼ÜÉèÔÚÓ¦ÓóÌĞò¶Ë£¨¿Í»§¶Ë£©ºÍÔ¶³Ì·şÎñÆ÷¶ËÖ®¼ä£¬¶øºó£¬Êı¾İµÄ¶ÁÈ¡£¨½ÓÊÕ£©ºÍĞ´È루·¢ËÍ£©¾ùÕë¶ÔÕâ¸öͨµÀÀ´½øĞĞ¡£
¿É¼û£¬ÔÚÓ¦ÓóÌĞò¶Ë»òÕß·şÎñÆ÷¶Ë´´½¨ÁËSocket¶ÔÏóÖ®ºó£¬¾Í¿ÉÒÔʹÓÃSend/SentTo·½·¨½«Êı¾İ·¢Ë͵½Á¬½ÓµÄSocket,»òÕßʹÓÃReceive/ReceiveFrom·½·¨½ÓÊÕÀ´×ÔÁ¬½ÓSocketµÄÊı¾İ£»
Õë¶ÔSocket±à³Ì£¬.NET ¿ò¼ÜµÄ Socket ÀàÊÇ Winsock32 API ÌṩµÄÌ×½Ó×Ö·şÎñµÄÍйܴúÂë°æ±¾¡£ÆäÖĞΪʵÏÖÍøÂç±à³ÌÌṩÁË´óÁ¿µÄ·½·¨£¬´ó¶àÊıÇé¿öÏ£¬Socket Àà·½·¨Ö»Êǽ«Êı¾İ·âË͵½ËüÃǵı¾»ú Win32 ¸±±¾Öв¢´¦ÀíÈκαØÒªµÄ°²È«¼ì²é¡£Èç¹ûÄãÊìϤ Winsock APIº¯Êı£¬ÄÇôÓÃSocketÀà±àĞ´ÍøÂç³ÌĞò»á·Ç³£ÈİÒ×£¬µ±È»£¬Èç¹ûÄã²»Ôø½Ó´¥¹ı£¬Ò²²»»áÌ«À§ÄÑ£¬¸úËæÏÂÃæµÄ½â˵£¬Äã»á·¢¾õʹÓà SocketÀ࿪·¢windows ÍøÂçÓ¦ÓóÌĞòÔÀ´Óйæ¿ÉÑ°£¬ËüÃÇÔÚ´ó¶àÊıÇé¿öÏÂ×ñÑ´óÖÂÏàͬµÄ²½Öè¡£
ÔÚʹÓÃ֮ǰ£¬ÄãĞèÒªÊ×ÏÈ´´½¨Socket¶ÔÏóµÄʵÀı£¬Õâ¿ÉÒÔͨ¹ıSocketÀàµÄ¹¹Ôì·½·¨À´ÊµÏÖ£º
public Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType);
ÆäÖĞ£¬addressFamily ²ÎÊıÖ¸¶¨ Socket ʹÓõÄÑ°Ö··½°¸£¬socketType ²ÎÊıÖ¸¶¨ Socket µÄÀàĞÍ£¬protocolType ²ÎÊıÖ¸¶¨ Socket ʹÓõÄĞÒé¡£
ÏÂÃæµÄʾÀıÓï¾ä´´½¨Ò»¸ö Socket£¬Ëü¿ÉÓÃÓÚÔÚ»ùÓÚ TCP/IP µÄÍøÂ磨Èç Internet£©ÉÏͨѶ¡£
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ÈôҪʹÓà UDP ¶ø²»ÊÇ TCP£¬ĞèÒª¸ü¸ÄĞÒéÀàĞÍ£¬ÈçÏÂÃæµÄʾÀıËùʾ£º
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Ò»µ©´´½¨ Socket£¬ÔÚ¿Í»§¶Ë£¬Ä㽫¿ÉÒÔͨ¹ıConnect·½·¨Á¬½Óµ½Ö¸¶¨µÄ·şÎñÆ÷£¬²¢Í¨¹ıSend/SendTo·½·¨ÏòÔ¶³Ì·şÎñÆ÷·¢ËÍÊı¾İ£¬¶øºó¿ÉÒÔͨ¹ıReceive/ReceiveFrom´Ó·şÎñ¶Ë½ÓÊÕÊı¾İ£»¶øÔÚ·şÎñÆ÷¶Ë£¬ÄãĞèҪʹÓÃBind·½·¨°ó¶¨ËùÖ¸¶¨µÄ½Ó¿ÚʹSocketÓëÒ»¸ö±¾µØÖÕ½áµãÏàÁª£¬²¢Í¨¹ıListen·½·¨ÕìÌı¸Ã½Ó¿ÚÉϵÄÇëÇ󣬵±ÕìÌıµ½Óû§¶ËµÄÁ¬½Óʱ£¬µ÷ÓÃAcceptÍê³ÉÁ¬½ÓµÄ²Ù×÷£¬´´½¨ĞµÄSocketÒÔ´¦Àí´«ÈëµÄÁ¬½ÓÇëÇó¡£Ê¹ÓÃÍê Socket ºó£¬¼ÇסʹÓà Shutdown ·½·¨½ûÓà Socket£¬²¢Ê¹Óà Close ·½·¨¹Ø±Õ Socket¡£Æä¼äÓõ½µÄ·½·¨/ º¯ÊıÓĞ£º
Socket.Connect·½·¨:½¨Á¢µ½Ô¶³ÌÉ豸µÄÁ¬½Ó
public void Connect(EndPoint remoteEP)£¨ÓĞÖØÔØ·½·¨£©
Socket.Send ·½·¨:´ÓÊı¾İÖеÄָʾλÖÿªÊ¼½«Êı¾İ·¢Ë͵½Á¬½ÓµÄ Socket¡£
public int Send(byte[], int, SocketFlags);(ÓĞÖØÔØ·½·¨)
Socket.SendTo ·½·¨ ½«Êı¾İ·¢Ë͵½Ìض¨ÖÕ½áµã¡£
public int SendTo(byte[], EndPoint);£¨ÓĞÖØÔØ·½·¨£©
Socket.Receive·½·¨:½«Êı¾İ´ÓÁ¬½ÓµÄ Socket ½ÓÊÕµ½½ÓÊÕ»º³åÇøµÄÌض¨Î»Öá£
public int Receive(byte[],int,SocketFlags);
Socket.ReceiveFrom·½·¨£º½ÓÊÕÊı¾İ»º³åÇøÖĞÌض¨Î»ÖõÄÊı¾İ²¢´æ´¢ÖÕ½áµã¡£
public int ReceiveFrom(byte[], int, SocketFlags, ref EndPoint);
Socket.Bind ·½·¨£ºÊ¹ Socket ÓëÒ»¸ö±¾µØÖÕ½áµãÏà¹ØÁª£º
public void Bind( EndPoint localEP );
Socket.Li
103f
sten·½·¨£º½« Socket ÖÃÓÚÕìÌı״̬¡£
public void Listen( int backlog );
Socket.Accept·½·¨:´´½¨Ğ嵀 Socket ÒÔ´¦Àí´«ÈëµÄÁ¬½ÓÇëÇó¡£
public Socket Accept();
Socket.Shutdown·½·¨:½ûÓÃij Socket Éϵķ¢ËͺͽÓÊÕ
public void Shutdown( SocketShutdown how );
Socket.Close·½·¨:Ç¿ÖÆ Socket Á¬½Ó¹Ø±Õ
public void Close();
¿ÉÒÔ¿´³ö£¬ÒÔÉÏĞí¶à·½·¨°üº¬EndPointÀàĞ͵IJÎÊı£¬ÔÚInternetÖĞ£¬TCP/IP ʹÓÃÒ»¸öÍøÂçµØÖ·ºÍÒ»¸ö·şÎñ¶Ë¿ÚºÅÀ´Î¨Ò»±êʶÉ豸¡£ÍøÂçµØÖ·±êʶÍøÂçÉϵÄÌض¨É豸£»¶Ë¿ÚºÅ±êʶҪÁ¬½Óµ½µÄ¸ÃÉ豸ÉϵÄÌض¨·şÎñ¡£ÍøÂçµØÖ·ºÍ·şÎñ¶Ë¿ÚµÄ×éºÏ³ÆΪÖÕ½áµã£¬ÔÚ .NET ¿ò¼ÜÖĞÕıÊÇÓÉ EndPoint Àà±íʾÕâ¸öÖÕ½áµã£¬ËüÌṩ±íʾÍøÂç×ÊÔ´»ò·şÎñµÄ³éÏó£¬ÓÃÒÔ±êÖ¾ÍøÂçµØÖ·µÈĞÅÏ¢¡£.NetͬʱҲΪÿ¸öÊÜÖ§³ÖµÄµØÖ·×嶨ÒåÁË EndPoint µÄ×Ó´ú£»¶ÔÓÚ IP µØÖ·×壬¸ÃÀàΪ IPEndPoint¡£IPEndPoint Àà°üº¬Ó¦ÓóÌĞòÁ¬½Óµ½Ö÷»úÉϵķşÎñËùĞèµÄÖ÷»úºÍ¶Ë¿ÚĞÅÏ¢£¬Í¨¹ı×éºÏ·şÎñµÄÖ÷»úIPµØÖ·ºÍ¶Ë¿ÚºÅ£¬IPEndPoint ÀàĞγɵ½·şÎñµÄÁ¬½Óµã¡£