Remarks
The bind function is used on an unconnected socket before subsequent calls to the connect or listen functions. It is not neccessary to use bind for connect(), but is required for listen(). This binding process is performed by the connect() function implicitly.
.
Parameter
socketHandle An open socket handle value (a SOCKET in C++, an
IntPtr in .NET, or just a 32-bit integer).
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
.
Parameter
socketAddress A sockaddr_in structure used to identify the IP address and port to bind to.
.
Parameter
addressLength The length of the supplied socketAddress parameter.
.
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);
.
Documentation
[closesocket] on MSDN
.
/// Use to determine the amount of data pending in the network's input buffer that can be read from socket s.
.
/// 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,
.
Documentation
[ioctlsocket] on MSDN
.
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 .
TODO - a short description
3/17/2017 2:18:13 AM - -24.94.14.150
.
public static extern int SendTo (IntPtr Socket, IntPtr buff, int len, SendDataFlags flags, ref SockAddr To, int tomlen);
Click to read this page 10/16/2009 1:27:57 AM - anonymous
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
TODO - a short description
4/11/2012 8:40:24 AM - -194.176.224.41
.
/// <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 .
TODO - a short description
3/17/2017 2:18:13 AM - -24.94.14.150
.
public static extern int setsockopt(IntPtr s, SocketOptionLevel level, SocketOptionName optname, ref int optval, 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 enum SOCKET_TYPE : short
.
/// stream socket
.
/// datagram socket
.
Documentation
[SOCKET_TYPE] on MSDN
.
public enum SocketOptionLevel
.
Socket = 0xffff,
.
Documentation
[SocketOptionLevel] on MSDN
.
public enum SocketOptionName
.
Documentation
[SocketOptionName] on MSDN
.
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.
.
/// Enables or disables the Nagle algorithm for TCP sockets. This option is disabled (set to FALSE) by default.
.
#region IPPROTO_UDP Socket Options
.
Documentation
[SOCKET_OPTION_NAME] on MSDN
.
Summary
Cleans up the Windows Sockets resources. Ensure this method is called after every successful call to WSAStartup().
.
Summary
Data structure for the Windows Socket API
.
public Int16 maxSockets;
.
internal ushort iMaxSockets;
.
/* Socket, Mode */
.
IntPtr s, System.Net.Sockets.IOControlCode dwIoControlCode,
.
//BufferRet = Native.recv(socketPtr, BufferPtr, Buffer, Native.MsgFlags.MSG_PEEK);
.
//Native.ioctlsocket(this.socketPtr, Native.Command.FIONREAD, ref BufferRet);
.
this.socketPtr, Native.ControlCode.FIONREAD,
.
if (WSALookupServiceBegin(ref lpRestrictions, 4080, ref hLookup) != (int)SocketError.SocketError)
.
[Socket.IOControl] on MSDN
.
public int iSocketType;
.
Summary
The
WSASocket function creates a socket that is bound to a specific transport-service provider.
The WSASocket function creates a socket that is bound to a specific transport-service provider.
11/12/2009 9:03:55 PM - -128.226.120.121
.
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 SOCKET_TYPE : short
.
/// stream socket
.
/// datagram socket
.
public enum OPTION_FLAGS_PER_SOCKET : short
.
// socket has had listen()
.
// get socket type
.
// ID of a socket group
.
Summary
The
WSAStringToAddress function converts a network address in its standard text presentation form into its numeric binary form in a sockaddr structure, suitable for passing to Windows Sockets routines that take such a structure.
The WSAStringToAddress function converts a network address in its standard text presentation form into its numeric binary form in a sockaddr structure, suitable for passing to Windows Sockets routines that take such a structure.
7/29/2012 8:42:36 PM - -77.124.220.248
.
socketAddress: A pointer to a buffer that is filled with a sockaddr structure for the address string if the function succeeds.
.
socketAddressSize: A pointer to the length, in bytes, of the buffer pointed to by the lpAddress parameter.
.
System.Net.Sockets.AddressFamily.InterNetwork,
netapi32
.
public static extern int DsAddressToSiteNames(string computerName, int entryCount, SOCKET_ADDRESS[] socketAddresses, ref IntPtr siteNames);
.
entryCount: The number of elements in the socketAddresses array.
.
.
.
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.
The DsAddressToSiteNames function obtains the site names corresponding to the specified addresses.
11/3/2011 11:14:22 AM - -131.107.0.98
The SOCKET_ADDRESS structure stores protocol-specific address information.
11/3/2011 10:31:15 AM - anonymous
.
SOCKET_ADDRESS[] SocketAddresses = new SOCKET_ADDRESS[1];
.
// Call into WSAStringToAddress to build SOCKET_ADDRESS structure from the address string
.
System.Net.Sockets.AddressFamily.InterNetwork,
.
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.
The SOCKET_ADDRESS structure stores protocol-specific address information.
11/3/2011 10:31:15 AM - anonymous
.
using System.Net.Sockets;
wininet
.
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.
iphlpapi
.
using System.Net.Sockets;
.
public struct SOCKET_ADDRESS
.
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
.
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
httpapi
.
// 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();
dnsapi
.
using System.Net.Sockets;
.
using System.Net.Sockets;
Structures
.
internal SOCKET_TYPE_INT ai_socktype;
.
ai_socktype = SOCKET_TYPE_INT.SOCK_STREAM,
.
SOCKET_TYPE_INT
Click to read this page 5/16/2017 4:12:01 AM - anonymous
.
/// 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;
.
Summary
The SOCKET_ADDRESS structure stores protocol-specific address information.
.
public struct SOCKET_ADDRESS
.
Structure SOCKET_ADDRESS
.
Documentation
[SOCKET_ADDRESS] on MSDN
.
Summary
The WSADATA structure contains information about the Windows Sockets implementation
.
internal Int16 maxSockets;
.
Summary
The data returned from the Windows Socket API.
secur32
.
using System.Net.Sockets;
Interfaces
.
// hence the read goes directly to e.g. socket
Enums
.
internal enum SOCKET_TYPE_INT : int
.
/// stream socket
.
/// datagram socket
.
Enum SOCKET_TYPE_INT
.
Documentation
[SOCKET_TYPE_INT] on MSDN