首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 使用 Winsock API 實作同步 Socket 伺服器

     編輯:關於C#

為讓 .NET 開發者深入理解 Winsock2 底層機制,本文將 .NET Socket 同步模型的核心 P/Invoke 封裝代碼剝離簡化,僅保留 TCP/IPv4、Win2000+ 環境下的同步伺服器邏輯,移除計數器、日誌、非同步/IOCP 相關代碼。

關鍵 P/Invoke 宣告

直接對應 ws2_32.dll 核心函數:

[SuppressUnmanagedCodeSecurity]
internal static class OSSOCK
{
    [DllImport("ws2_32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    internal static extern SocketError WSAStartup(short wVersionRequested, out WSAData lpWSAData);

    [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern SafeCloseSocket.InnerSafeCloseSocket WSASocket(
        AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType,
        IntPtr protocolInfo, uint group, SocketConstructorFlags flags);

    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern SocketError bind(SafeCloseSocket socketHandle, byte[] socketAddress, int socketAddressSize);

    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern SocketError listen(SafeCloseSocket socketHandle, int backlog);

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern SocketError ioctlsocket(SafeCloseSocket socketHandle, int cmd, ref int argp);

    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern unsafe int recv(IntPtr socketHandle, byte* pinnedBuffer, int len, SocketFlags socketFlags);

    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern unsafe int send(IntPtr socketHandle, byte* pinnedBuffer, int len, SocketFlags socketFlags);

    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern SocketError shutdown(SafeCloseSocket socketHandle, int how);

    [DllImport("ws2_32.dll", SetLastError = true)]
    internal static extern SocketError setsockopt(SafeCloseSocket socketHandle, SocketOptionLevel optionLevel, SocketOptionName optionName, ref int optionValue, int optionLength);
}

學習建議

  • 深入研讀《Windows 網路程式設計》第 7、8、9 章,掌握這幾個核心 API 的語意與錯誤碼。
  • 理解 .NET Socket 只是薄封裝,底層仍是這些 API。遇到網路疑難雜症時,抓 Dump 分析呼叫堆疊、觀察阻塞於哪個 API,能快速定位問題。
  • 配合 Windbg、CLR 內部運作、Win32 API、CRT、IL 甚至組合語言知識,可建立完整除錯視野。

本文提供的精簡代碼適合作為教學參考,生產環境建議直接使用 .NET 高層 Socket / TcpListener 或成熟網路庫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved