如何将C++中的SOCKADDR_IN*参数类型转换成C#中的参数类型
将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;
}
}
如何将C++中的SOCKADDR_IN*参数类型转换成C#中的参数类型的更多相关文章
- sqlserver搜索中怎么把varchar类型转换成numeric类型
sqlserver搜索中怎么把varchar类型转换成numeric类型 可以用cast来转换 如:列名叫grade,表名为A select cast(grade as numeric(y,x)) f ...
- ibtais中把clob数据类型转换成string并展示到前台
1,在xml中定义一个resultMap <resultMap class="com.aa.bb" id="clobToString"> <r ...
- ibtais中把clob数据类型转换成string并插入到数据库中
1,在xml中定义一个parameterMap <parameterMap id="stringToClob" class="com.a.b.c"> ...
- 缓存架构中的服务详解!SpringBoot中二级缓存服务的实现
创建缓存服务 创建缓存服务接口项目 创建myshop-service-redis-api项目,该项目只负责定义接口 创建项目的pom.xml: <?xml version="1.0&q ...
- [.net 面向对象编程基础] (4) 基础中的基础——数据类型转换
[.net面向对象编程基础] (4)基础中的基础——数据类型转换 1.为什么要进行数据转换? 首先,为什么要进行数据转换,拿值类型例子说明一下, 比如:我们要把23角零钱,换成2.30元,就需要把整形 ...
- 通过反射将request中的参数封装到对象中
import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.text.SimpleDateFo ...
- C++中的显式类型转换
一.学习总结 1.C++中额外提供的类型转换有 (1) reinterpret_cast: 格式:reinterpret_cast<type-id> (expression)①type-i ...
- c#中的里氏转换和Java中强制类型转换在多态中的应用
在c#中: 注意: 子类并没有继承父类的构造函数,而是会默认调用父类那个无参数的构造函数. 如果一个子类继承了一个父类,那么这个子类除了可以使用自己的成员外,还可以使用从父类那里继承过来的成员.但是父 ...
- 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数
在Web项目中,如果前端MVC框架使用的是SpringMVC,可以使用Map接收前端请求参数,比bean要方便很多. 尤其是SpringMVC和Mybatis一起用的时候,用Map大大减少了需要的be ...
随机推荐
- Unity3d 使用NPOI读写Excel 遇到的问题
开发环境:unity5.3 NPOI(.net 2.0版 http://npoi.codeplex.com/) 运行环境:PC版, 其他平台没有测试 先上效果图: 实现步骤: 1.新建一个Exce ...
- Apache Options Indexes FollowSymLinks具体解释
禁止显示Apache文件夹列表-Indexes FollowSymLinks 怎样改动文件夹的配置以禁止显示 Apache 文件夹列表. 缺省情况下假设你在浏览器输入地址: http://loc ...
- Codeforces Round #200 (Div. 1) B. Alternating Current 栈
B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...
- BZOJ 1927: [Sdoi2010]星际竞速 费用流
1927: [Sdoi2010]星际竞速 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- linux netlink套接字学习资料
理论: http://blog.csdn.net/unbutun/article/details/3394061 进一步深入: http://edsionte.com/techblog/archive ...
- [AngularJS] Best Practise - Resolve promises in router, defer controllers
See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/ /** * Created by Answer12 ...
- iOS 音频拼接
工作中或许会遇到这样的需求,将两段不同的音频合成一个音频(暂且称之为音频拼接),实现起来相对来说不是很难,再介绍如何拼接之前,先了解下AVFoundation下的几个基本知识点. 基本知识 AVAss ...
- 学习笔记之SQL
--------参考网站-------- http://en.wikipedia.org/wiki/SQL SQL@Wiki http://en.wikipedia.org/wiki/Delete_% ...
- [Effective C++ --017]以独立语句将newed对象置入智能指针
这一节也比较简单,先假设我们有如下的函数: int foo(); void memFoo(shared_ptr<T> pw, int foo); 现在假设我们要调用memFoo函数: me ...
- CSS3 旋转的八卦图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...