我本来是使用vc++的,没问题。
现在改为c# .net ,需要使用网络来传送,传送的是一个结构体。
这个结构体在c是如下表示:
struct DataA
{
unsigned short type;
unsigned short len
char UserName[32];
unsigned long info1;
};
现在要在c# 中 使用tcpclient给发送出去。
另外UserName 在c#传送时是 string username,如何转换成为
char UserName[32]; ?
现在改为c# .net ,需要使用网络来传送,传送的是一个结构体。
这个结构体在c是如下表示:
struct DataA
{
unsigned short type;
unsigned short len
char UserName[32];
unsigned long info1;
};
现在要在c# 中 使用tcpclient给发送出去。
另外UserName 在c#传送时是 string username,如何转换成为
char UserName[32]; ?
解决方案 »
- 机房线路管理系统
- 做3个程序,如何控制这3个程序的同步
- 看似简单的问题:MyClass myClass = new MyClass(); 如何让myClass.Name等于"myClass"?
- rdlc报表上的图像显示不出来,为红叉.
- 各位大侠帮帮忙,指点一二!怎样用C#连接MY SQL数据库并编写一个删除程序!
- 请问这样一个问题,怎么获得一个表中字段的所有列存放在datacolumn或者泛型中的stack
- 紧急求救!!-一个查询问题
- 高分求难题```高手请进```
- c#调用c++ dll,开发电脑运行正常,其他电脑提示"找不到webcamlib.dll文件
- Wpf的Combobox绑定dataTable
- 为什么线程会自已阻塞?
- 三层结构部署问题
用序列化来做,先把struct变量序列化,然后形成xml数据,接收方接收后反序列化成struct变量。方法二:
自己来定义byte流格式,即把struct的每个成员分别转成byte,然后组成byte流;接收方接到后,按照格式然后反向形成struct变量。
struct DataA
{
unsigned short type;
unsigned short len;
char UserName[32];
unsigned long info1;
};
type=1
len=36;
info1=123456;而UserName 应该来自于一个 string username,
请问该怎么写这个byte 流格式 ?能否告诉我怎么写,我真是新手啊...
Byte[] DataSend = new Byte[1024] ;
Sample code as follows:
private byte[] ShortToBytes( short nValue )
{
byte[] bytShort = new byte[2];
bytShort[0] = (byte) ( nValue >> 8 );//High 8 bits
bytShort[1] = (byte) ( nValue & 0x00FF );//Low 8 bits
return nValue;
}bytes => short
private short BytesToShort( byte[] bytData )
{
if( bytData == null || bytData.Length != 2 ) throw new Exception( "Invalid parameters" ); short nReturn = 0;
nReturn = bytData[0];
nReturn = ( nReturn << 8 );
nReturn ^= byteData[1];
return nReturn;
}
char[32]可以强转成byte[32],一一对应即可。