将C++中的参数类型SOCKADDR_IN*映射为C#中的IntPtr参数类型的示例代码如下:
IntPtr ptrSockaddr = new IntPtr();
//ip地址
sockaddr_in sockaddr = new sockaddr_in();
sockaddr.sin_family = ADDRESS_FAMILIES.AF_INET;
sockaddr.sin_port = Ws232Util.htons(port);
sockaddr.sin_addr.s_addr = Ws232Util.inet_addr(host);
sockaddr.sin_zero = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
ptrSockaddr = Marshal.AllocHGlobal(Marshal.SizeOf(sockaddr));
Marshal.StructureToPtr(sockaddr, ptrSockaddr, false);
然后将ptrSockaddr参数传入相应的方法中即可。
sockaddr_in在C#中的完整定义如下:
1、ADDRESS_FAMILIES.cs
using System;
namespace DiancaiBll
{
public enum ADDRESS_FAMILIES : short
{
/// <summary>
/// Unspecified [value = 0].
/// </summary>
AF_UNSPEC = 0,
/// <summary>
/// Local to host (pipes, portals) [value = 1].
/// </summary>
AF_UNIX = 1,
/// <summary>
/// Internetwork: UDP, TCP, etc [value = 2].
/// </summary>
AF_INET = 2,
/// <summary>
/// Arpanet imp addresses [value = 3].
/// </summary>
AF_IMPLINK = 3,
/// <summary>
/// Pup protocols: e.g. BSP [value = 4].
/// </summary>
AF_PUP = 4,
/// <summary>
/// Mit CHAOS protocols [value = 5].
/// </summary>
AF_CHAOS = 5,
/// <summary>
/// XEROX NS protocols [value = 6].
/// </summary>
AF_NS = 6,
/// <summary>
/// IPX protocols: IPX, SPX, etc [value = 6].
/// </summary>
AF_IPX = 6,
/// <summary>
/// ISO protocols [value = 7].
/// </summary>
AF_ISO = 7,
/// <summary>
/// OSI is ISO [value = 7].
/// </summary>
AF_OSI = 7,
/// <summary>
/// european computer manufacturers [value = 8].
/// </summary>
AF_ECMA = 8,
/// <summary>
/// datakit protocols [value = 9].
/// </summary>
AF_DATAKIT = 9,
/// <summary>
/// CCITT protocols, X.25 etc [value = 10].
/// </summary>
AF_CCITT = 10,
/// <summary>
/// IBM SNA [value = 11].
/// </summary>
AF_SNA = 11,
/// <summary>
/// DECnet [value = 12].
/// </summary>
AF_DECnet = 12,
/// <summary>
/// Direct data link interface [value = 13].
/// </summary>
AF_DLI = 13,
/// <summary>
/// LAT [value = 14].
/// </summary>
AF_LAT = 14,
/// <summary>
/// NSC Hyperchannel [value = 15].
/// </summary>
AF_HYLINK = 15,
/// <summary>
/// AppleTalk [value = 16].
/// </summary>
AF_APPLETALK = 16,
/// <summary>
/// NetBios-style addresses [value = 17].
/// </summary>
AF_NETBIOS = 17,
/// <summary>
/// VoiceView [value = 18].
/// </summary>
AF_VOICEVIEW = 18,
/// <summary>
/// Protocols from Firefox [value = 19].
/// </summary>
AF_FIREFOX = 19,
/// <summary>
/// Somebody is using this! [value = 20].
/// </summary>
AF_UNKNOWN1 = 20,
/// <summary>
/// Banyan [value = 21].
/// </summary>
AF_BAN = 21,
/// <summary>
/// Native ATM Services [value = 22].
/// </summary>
AF_ATM = 22,
/// <summary>
/// Internetwork Version 6 [value = 23].
/// </summary>
AF_INET6 = 23,
/// <summary>
/// Microsoft Wolfpack [value = 24].
/// </summary>
AF_CLUSTER = 24,
/// <summary>
/// IEEE 1284.4 WG AF [value = 25].
/// </summary>
AF_12844 = 25,
/// <summary>
/// IrDA [value = 26].
/// </summary>
AF_IRDA = 26,
/// <summary>
/// Network Designers OSI & gateway enabled protocols [value = 28].
/// </summary>
AF_NETDES = 28,
/// <summary>
/// [value = 29].
/// </summary>
AF_TCNPROCESS = 29,
/// <summary>
/// [value = 30].
/// </summary>
AF_TCNMESSAGE = 30,
/// <summary>
/// [value = 31].
/// </summary>
AF_ICLFXBM = 31
}
}
2、in_addr.cs
using System;
using System.Runtime.InteropServices;
namespace DiancaiBll
{
[StructLayout(LayoutKind.Explicit, Size = 4)]
public struct in_addr
{
[FieldOffset(0)]
internal byte s_b1;
[FieldOffset(1)]
internal byte s_b2;
[FieldOffset(2)]
internal byte s_b3;
[FieldOffset(3)]
internal byte s_b4;
[FieldOffset(0)]
internal ushort s_w1;
[FieldOffset(2)]
internal ushort s_w2;
[FieldOffset(0)]
internal uint S_addr;
/// <summary>
/// can be used for most tcp & ip code
/// </summary>
public uint s_addr
{
get { return S_addr; }
set { S_addr = value; }
}
/// <summary>
/// host on imp
/// </summary>
public byte s_host
{
get { return s_b2; }
set { s_b2 = value; }
}
/// <summary>
/// network
/// </summary>
public byte s_net
{
get { return s_b1; }
set { s_b1 = value; }
}
/// <summary>
/// imp
/// </summary>
public ushort s_imp
{
get { return s_w2; }
set { s_w2 = value; }
}
/// <summary>
/// imp #
/// </summary>
public byte s_impno
{
get { return s_b4; }
set { s_b4 = value; }
}
/// <summary>
/// logical host
/// </summary>
public byte s_lh
{
get { return s_b3; }
set { s_b3 = value; }
}
}
}
3、in6_addr.cs
using System;
using System.Runtime.InteropServices;
namespace DiancaiBll
{
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct in6_addr
{
[FieldOffset(0)]
public byte Byte_0;
[FieldOffset(1)]
public byte Byte_1;
[FieldOffset(2)]
public byte Byte_2;
[FieldOffset(3)]
public byte Byte_3;
[FieldOffset(4)]
public byte Byte_4;
[FieldOffset(5)]
public byte Byte_5;
[FieldOffset(6)]
public byte Byte_6;
[FieldOffset(7)]
public byte Byte_7;
[FieldOffset(8)]
public byte Byte_8;
[FieldOffset(9)]
public byte Byte_9;
[FieldOffset(10)]
public byte Byte_10;
[FieldOffset(11)]
public byte Byte_11;
[FieldOffset(12)]
public byte Byte_12;
[FieldOffset(13)]
public byte Byte_13;
[FieldOffset(14)]
public byte Byte_14;
[FieldOffset(15)]
public byte Byte_16;
[FieldOffset(0)]
public short Word_0;
[FieldOffset(2)]
public short Word_1;
[FieldOffset(4)]
public short Word_2;
[FieldOffset(6)]
public short Word_3;
[FieldOffset(8)]
public short Word_4;
[FieldOffset(10)]
public short Word_5;
[FieldOffset(12)]
public short Word_6;
[FieldOffset(14)]
public short Word_7;
}
}
4、sockaddr_in.cs
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace DiancaiBll
{
[StructLayout(LayoutKind.Sequential)]
public struct sockaddr_in
{
public ADDRESS_FAMILIES sin_family;
public ushort sin_port;
public in_addr sin_addr;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] sin_zero;
public string Host
{
get
{
sockaddr_in local = this;
uint length = 256;
StringBuilder builder = new StringBuilder((int)length);
WSAData data = new WSAData();
Ws232Util.WSAStartup(2, ref data);
Ws232Util.WSAAddressToString(ref local, (uint)Marshal.SizeOf(local), IntPtr.Zero, builder, ref length);
Ws232Util.WSACleanup();
if (builder.ToString().Split(':').Length > 0)
return builder.ToString().Split(':')[0];
else
return "";
}
}
public string Port
{
get
{
sockaddr_in local = this;
uint length = 256;
StringBuilder builder = new StringBuilder((int)length);
WSAData data = new WSAData();
Ws232Util.WSAStartup(2, ref data);
Ws232Util.WSAAddressToString(ref local, (uint)Marshal.SizeOf(local), IntPtr.Zero, builder, ref length);
Ws232Util.WSACleanup();
if (builder.ToString().Split(':').Length > 1)
return builder.ToString().Split(':')[1];
else
return "";
}
}
public static sockaddr_in FromString(string host, int port)
{
sockaddr_in sockaddr = new sockaddr_in();
int lpAddressLength = Marshal.SizeOf(sockaddr);
Ws232Util.WSAStringToAddress(host + ":" + port.ToString(), ADDRESS_FAMILIES.AF_INET, IntPtr.Zero, ref sockaddr, ref lpAddressLength);
return sockaddr;
}
}
}
5、sockaddr_in6.cs
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace DiancaiBll
{
[StructLayout(LayoutKind.Explicit, Size = 28)]
public struct sockaddr_in6
{
[FieldOffset(0)]
public ADDRESS_FAMILIES sin6_family;
[FieldOffset(2)]
public ushort sin6_port;
[FieldOffset(4)]
public uint sin6_flowinfo;
[FieldOffset(8)]
public in6_addr sin6_addr;
[FieldOffset(24)]
public uint sin6_scope_id;
internal string Host
{
get
{
sockaddr_in6 local = this;
uint length = 256;
StringBuilder builder = new StringBuilder((int)length);
WSAData data = new WSAData();
Ws232Util.WSAStartup(2, ref data);
Ws232Util.WSAAddressToString(ref local, (uint)Marshal.SizeOf(local), IntPtr.Zero, builder, ref length);
Ws232Util.WSACleanup();
if (builder.ToString().Split(':').Length > 0)
return builder.ToString().Split(':')[0];
else
return "";
}
}
internal string Port
{
get
{
sockaddr_in6 local = this;
uint length = 256;
StringBuilder builder = new StringBuilder((int)length);
WSAData data = new WSAData();
Ws232Util.WSAStartup(2, ref data);
Ws232Util.WSAAddressToString(ref local, (uint)Marshal.SizeOf(local), IntPtr.Zero, builder, ref length);
Ws232Util.WSACleanup();
if (builder.ToString().Split(':').Length > 1)
return builder.ToString().Split(':')[1];
else
return "";
}
}
public static sockaddr_in6 FromString(string host, int port)
{
sockaddr_in6 sockaddr = new sockaddr_in6();
int lpAddressLength = Marshal.SizeOf(sockaddr);
Ws232Util.WSAStringToAddress(host + ":" + port.ToString(), ADDRESS_FAMILIES.AF_INET6, IntPtr.Zero, ref sockaddr, ref lpAddressLength);
return sockaddr;
}
}
}
6、WSAData.cs
using System;
using System.Runtime.InteropServices;
namespace DiancaiBll
{
[StructLayout(LayoutKind.Sequential)]
public struct WSAData
{
public Int16 version;
public Int16 highVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
public String description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
public String systemStatus;
public Int16 maxSockets;
public Int16 maxUdpDg;
public IntPtr vendorInfo;
}
}