Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SOCKADDR (Structures)
 
.
Summary
TODO - a short description

C# Definition:

    [StructLayout(LayoutKind.Explicit, Size = 24)]
    internal struct sockaddr_in6_old
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct sockaddr
    {
        [FieldOffset(0)] internal ADDRESS_FAMILIES sin6_family;
        internal EnumLib.ADDRESS_FAMILIES sa_family;

        [FieldOffset(2)] internal ushort sin6_port;

        [FieldOffset(4)] internal uint sin6_flowinfo;

        [FieldOffset(8)] internal in6_addr sin6_addr;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)] internal String sa_data;
    }

VB Definition:

Structure sockaddr_in6_old
   Public TODO
End Structure

User-Defined Field Types:

in6_addr ADDRESS_FAMILIES

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal unsafe struct sockaddr_UNSAFE
    {
        internal EnumLib.ADDRESS_FAMILIES sa_family;
        internal fixed char sa_data [14];

        internal string Name
        {
        get
        {
            var result = string.Empty;
            fixed (char* str = sa_data)
            {
            result = new string(str);
            }
            return result;
        }
        }
    }

Notes:

None.

Documentation

Converting an IntPtr pointing to a sockaddr structure, to an IPAddress (source: http://stackoverflow.com/questions/17426939/c-sharp-converting-intptr-pointing-to-sockaddr-structure-to-ipaddress):

    public enum SockAddrFamily
    {
      Inet = 2,
      Inet6 = 23
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SockAddr
    {
      public ushort Family;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
      public byte[] Data;
    };

    [StructLayout(LayoutKind.Sequential)]
    public struct SockAddrIn
    {
      public ushort Family;
      public ushort Port;
      public uint Addr;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
      public byte[] Zero;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SockAddrIn6
    {
      public ushort Family;
      public ushort Port;
      public uint FlowInfo;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
      public byte[] Addr;
      public uint ScopeId;
    };

    public IPAddress ConvertSockAddrPtrToIPAddress(IntPtr sockAddrPtr)
    {
      SockAddr sockAddr = (SockAddr)Marshal.PtrToStructure(sockAddrPtr, typeof(SockAddr));
      switch ((SockAddrFamily)sockAddr.Family)
      {
        case SockAddrFamily.Inet:
        {
          SockAddrIn sockAddrIn = (SockAddrIn)Marshal.PtrToStructure(sockAddrPtr, typeof(SockAddrIn));
          return new IPAddress(sockAddrIn.Addr);
        }
        case SockAddrFamily.Inet6:
        {
          SockAddrIn6 sockAddrIn6 = (SockAddrIn6)Marshal.PtrToStructure(sockAddrPtr, typeof(SockAddrIn6));
          return new IPAddress(sockAddrIn6.Addr);
        }
        default:
          throw new Exception(string.Format("Non-IP address family: {0}", sockAddr.Family));
      }
    }


Documentation
SOCKADDR on MSDN

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions