C # uses the Winsock API to implement the synchronous socket service side

Source: Internet
Author: User
Tags socket static class

Since most of the code to explain Winsock is C + +, and Winsock is just some use of Windows APIs, in order to help. NET programmers to understand Winsock2, I have removed the implementation code of the. NET synchronization socket, simplifying it, We have time to debug and see.

Attention

1, can only run in Win2000 above the system

2, only to support the TCP protocol,

3. Support IPv4

Changes

1, remove the counter, log and other logic

2, does not support asynchronous, complete the port model, and so on time, the completion of the port that part also split out to everyone.

In order not to let this post too short, paste some code used in the Winsock function of the prototype, we want to develop high-performance network programs, you have to understand the Winsock of the several functions, you can see "Windows network Programming" 7th, 8, 9 chapters

Ossock#region Ossock
[SuppressUnmanagedCodeSecurity]
Internal Static class Ossock
{
[DllImport ("Ws2_32.dll", CharSet = CharSet.Ansi, SetLastError = True)]
Internal static extern socketerror WSAStartup ([in] short wversionrequested, out Wsadata lpwsadata);
[DllImport ("Ws2_32.dll", CharSet = CharSet.Auto, SetLastError = True)]
Internal static extern safeclosesocket.innersafeclosesocket WSASocket ([in] addressfamily addressfamily, [in] SocketType SocketType, [in] ProtocolType protocoltype, [in] IntPtr protocolinfo, [in] UINT group, [in] Socketconstructorfl AGS flags);
[DllImport ("Ws2_32.dll", CharSet = CharSet.Auto, SetLastError = True)]
Internal static extern unsafe Safeclosesocket.innersafeclosesocket wsasocket ([in] addressfamily addressfamily, [in] SocketType SocketType, [in] ProtocolType protocoltype, [in] byte* pinnedbuffer, [in] UINT group, [in] Socketconstructorfla GS flags);
[DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern socketerror bind ([in] Safeclosesocket sockethandle, [in] byte[] socketaddress, [in] int socketaddre Sssize);
[DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern SocketError listen ([in] Safeclosesocket sockethandle, [in] int backlog);
[ReliabilityContract (Consistency.willnotcorruptstate, Cer.mayfail), DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern socketerror ioctlsocket ([in] Safeclosesocket sockethandle, [in] int cmd, [in, out] ref int ARGP);
[DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern unsafe int recv ([in] IntPtr sockethandle, [in] byte* pinnedbuffer, [in] int len, [in] SocketFlags s Ocketflags);
[DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern unsafe int send ([in] IntPtr sockethandle, [in] byte* pinnedbuffer, [in] int len, [in] SocketFlags s Ocketflags);
[DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern socketerror shutdown ([in] Safeclosesocket sockethandle, [in] int how);
[DllImport ("Ws2_32.dll", SetLastError = True)]
Internal static extern socketerror setsockopt ([in] Safeclosesocket sockethandle, [in] SocketOptionLevel optionlevel, [in ] SocketOptionName optionname, [in] ref int optionValue, [in] int optionlength);
}
#endregion

Studied for a few days. NET socket in the package of IOCP, found that it is indeed somewhat complex, do not intend to go out of the code, and understand the IOCP code, using platform invoke to write an example bar. No matter what. NET development, REMOTING,WCF what, the bottom or a limited number of such functions, really recommend that you take a good look at that time the network program problems, grab dump, see call stack also know exactly what API, why blocking. Learning WinDbg is just a few orders, the key you also have to understand the internal operating mechanism of the CLR, Win32api,crt,il and even assembly.

This article supporting source code

Related Article

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.