internal enum SOCKET_TYPE_INT : int
/// stream socket
/// datagram socket
Enum SOCKET_TYPE_INT
ByVal socketHandle As IntPtr, _
ByRef socketAddress As sockaddr, _
System.Net.Sockets.Socket.Accept()
socketHandle As IntPtr, _
ByRef socketAddress As sockaddr_in, _
/// Internet socket address structure.
public static bool Bind(string ipAddress, int port, IntPtr socketHandle)
if (socketHandle != IntPtr.Zero)
remoteAddress.sin_family = SocketsConstants.AF_INET;
resultCode = bind(socketHandle, ref remoteAddress,
public static extern int closesocket(IntPtr s);
Declare Function closesocket Lib "ws2_32.dll" (TODO) As TODO
static public void CloseSock(Socket sck)
//you may add sck.Shutdown(SocketShutdown.Both);
// to close the receiving and sending functions of the socket
closesocket(sck.Handle);
/// Use to determine the amount of data pending in the network's input buffer that can be read from socket s.
public static extern int getsockopt(IntPtr s, SocketOptionLevel level, SocketOptionName optname, out int optval, ref int optlen);
Native.getsockopt(socketPtr, Native.SocketOptionLevel.Socket, Native.SocketOptionName.ReceiveTimeout, out optval, ref optlen);
Native.setsockopt(socketPtr, Native.SocketOptionLevel.Socket, Native.SocketOptionName.ReceiveTimeout, ref optval, optlen);
public static extern int ioctlsocket(IntPtr s, Command cmd, ref int argp);
Declare Function ioctlsocket Lib "ws2_32.dll" (TODO) As TODO
//BufferRet = Native.recv(socketPtr, BufferPtr, Buffer, Native.MsgFlags.MSG_PEEK);
//Native.ioctlsocket(this.socketPtr, Native.Command.FIONREAD, ref BufferRet);
this.socketPtr, Native.ControlCode.FIONREAD,
public static extern int recvfrom(IntPtr Socket, IntPtr buf, int len, SendDataFlags flags, ref SockAddr from, IntPtr fromlen);
public static int recvfrom(IntPtr Socket, IntPtr buf, int len, SendDataFlags flags, ref SockAddr from)
return recvfrom(Socket, buf, len, flags, ref from, IntPtr.Zero);
/* An optional pointer to a set of sockets to be checked for readability. */
/* An optional pointer to a set of sockets to be checked for writability. */
/* An optional pointer to a set of sockets to be checked for errors. */
public static extern int SendTo (IntPtr Socket, IntPtr buff, int len, SendDataFlags flags, ref SockAddr To, int tomlen);
/// <summary> Specifies that the data should not be subject to routing. A Windows Sockets service provider can choose to ignore this flag</summary>
/// <summary>Sends OOB data (stream-style socket such as SOCK_STREAM only)</summary>
System.Net.Sockets.Socket (class) Socket.SendTo () - better to use It))
If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
public static extern int setsockopt(IntPtr s, SocketOptionLevel level, SocketOptionName optname, ref int optval, int optlen);
public enum SOCKET_TYPE : short
public enum SocketOptionLevel
Socket = 0xffff,
public enum SocketOptionName
public enum SOCKET_OPTION_NAME : int
#region IPPROTO_IP Socket Options
#region IPPROTO_IPV6 Socket Options
/// Offset to checksum for raw IP socket send.
#region SOL_SOCKET Socket Options
/// socket has had listen()
/// get socket type
/// get socket 5-tuple state
/// ID of a socket group
/// get/set the compartment for a socket
/// Option for opening sockets for synchronous access.
#region IPPROTO_TCP Socket Options
/// Enables or disables the Nagle algorithm for TCP sockets. This option is disabled (set to FALSE) by default.
#region IPPROTO_UDP Socket Options
public Int16 maxSockets;
internal ushort iMaxSockets;
/* Socket, Mode */
IntPtr s, System.Net.Sockets.IOControlCode dwIoControlCode,
if (WSALookupServiceBegin(ref lpRestrictions, 4080, ref hLookup) != (int)SocketError.SocketError)
[Socket.IOControl] on MSDN
public int iSocketType;
public static extern IntPtr WSASocket(ADDRESS_FAMILIES af, SOCKET_TYPE socket_type, PROTOCOL protocol,
IntPtr lpProtocolInfo, Int32 group, OPTION_FLAGS_PER_SOCKET dwFlags);
Declare Function WSASocket Lib "ws2_32.dll" (TODO) As TODO
public enum OPTION_FLAGS_PER_SOCKET : short
// socket has had listen()
// get socket type
// ID of a socket group
System.Net.Sockets.AddressFamily.InterNetwork,
// serialize the endpoint to a SocketAddress and create an array to hold the values. Pin the array.
SocketAddress socketAddress = ipEndPoint.Serialize();
byte[] socketBytes = new byte[socketAddress.Size];
GCHandle handleSocketAddress = GCHandle.Alloc(socketBytes, GCHandleType.Pinned);
// Should copy the first 16 bytes (the SocketAddress has a 32 byte buffer, the size will only be 16,
for (int i = 0; i < socketAddress.Size; ++i)
socketBytes[i] = socketAddress[i];
httpServiceConfigSslKey.pIpPort = handleSocketAddress.AddrOfPinnedObject();
handleSocketAddress.Free();
/// An attempt was made to access a socket in a way forbidden by its access permissions.
/// Too many open sockets.
/// A non-blocking socket operation could not be completed immediately.
/// An operation was attempted on a non-blocking socket that already had an operation in progress.
/// An operation was attempted on something that is not a socket.
/// A required address was omitted from an operation on a socket.
/// A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.
/// A protocol was specified in the socket function call that does not support the semantics of the socket type requested.
/// The support for the specified socket type does not exist in this address family.
/// Only one usage of each socket address (protocol/network address/port) is normally permitted.
/// A socket operation encountered a dead network.
/// A socket operation was attempted to an unreachable network.
/// An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.
/// A connect request was made on an already connected socket.
/// A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
/// A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call.
/// A socket operation failed because the destination host was down.
/// A socket operation was attempted to an unreachable host.
/// A Windows Sockets implementation may have a limit on the number of applications that may use it simultaneously.
/// The Windows Sockets version requested is not supported.
using System.Net.Sockets;
internal SOCKET_TYPE_INT ai_socktype;
ai_socktype = SOCKET_TYPE_INT.SOCK_STREAM,
SOCKET_TYPE_INT
/// The socket address will be used in a call to the bind function.
public static fd_set Create(IntPtr socket)
fd_array = new IntPtr[Size] { socket }
public uint SocketRegistersBaseAddress;
public struct SOCKET_ADDRESS
Structure SOCKET_ADDRESS
internal Int16 maxSockets;
// hence the read goes directly to e.g. socket
public static extern int DsAddressToSiteNames(string computerName, int entryCount, SOCKET_ADDRESS[] socketAddresses, ref IntPtr siteNames);
Prior to calling he DsAddressToSiteNames function, a SOCKET_ADDRESS structure must be created for each address that is to be resolved to a site name. This can be done using the WSAStringToAddress method shown in the sample code.
SOCKET_ADDRESS[] SocketAddresses = new SOCKET_ADDRESS[1];
// Call into WSAStringToAddress to build SOCKET_ADDRESS structure from the address string
SocketAddresses[0].lpSockaddr = pSockAddr;
SocketAddresses[0].iSockaddrLength = Marshal.SizeOf(sockAddr);
result = DsAddressToSiteNames("domaincontroller.mydomain.com", 1, SocketAddresses, ref pSites);
IntPtr SockAddresses, //must free this if using (SocketAddressCount > 1)
[out, optional] Pointer to a ULONG value that receives the number of elements in the SockAddresses array. If this parameter is NULL, socket addresses are not retrieved.
[out, optional] Pointer to an array of SOCKET_ADDRESS structures that receives the socket address data for the domain controller. SockAddressCount receives the number of elements in this array.
public SOCKET_ADDRESS Address;
public SOCKET_ADDRESS Dhcpv4Server;
public SOCKET_ADDRESS Dhcpv6Server;
SOCKADDR socketAddr = (SOCKADDR)Marshal.PtrToStructure(unicastAddr.Address.lpSockAddr, typeof(SOCKADDR));
byte[] saData = socketAddr.sa_data.Skip(2).Take(4).ToArray();
catch (SocketException e)
Console.WriteLine("SocketException caught!!!");
public const int AF_INET = 2; // IP_v4 = System.Net.Sockets.AddressFamily.InterNetwork
public const int AF_INET6 = 23; // IP_v6 = System.Net.Sockets.AddressFamily.InterNetworkV6
phFtpCommand (out) A pointer to a handle that is created if a valid data socket is opened. The fExpectResponse parameter must be set to TRUE for phFtpCommand to be filled.