今天我们来学 socket 发送结构体
1. 先看要发送的结构体
代码
using
System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Lin.p2p.Mo
{
/// <summary>
/// 通信消息格式
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1 )]
public struct CP2PMessage
{
public sbyte nMessageTypes;
public int Value;
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Lin.p2p.Mo
{
/// <summary>
/// 通信消息格式
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1 )]
public struct CP2PMessage
{
public sbyte nMessageTypes;
public int Value;
}
}
2. 请看服务端
代码
using
System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using Lin.p2p;
using Lin.p2p.Mo;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
// 1.创建套节字
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// 2.填充IP
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 4321 );
// 3.绑定
socket.Bind(ipe);
// 等待客户机连接
Console.WriteLine( " This is a Server, host name is {0} " , Dns.GetHostName());
Console.WriteLine( " Waiting for a client... " );
// 4.得客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0 );
EndPoint remote = (EndPoint)sender;
// 5.接收客户机数据
byte [] buffer = new byte [ 1024 ];
socket.ReceiveFrom(buffer, ref remote);
CP2PMessage msg = new CP2PMessage();
msg = (CP2PMessage)Tool.BytesToStruct(buffer, msg.GetType());
Console.WriteLine( " 接收的值为:{0} " , msg.Value);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using Lin.p2p;
using Lin.p2p.Mo;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
// 1.创建套节字
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// 2.填充IP
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 4321 );
// 3.绑定
socket.Bind(ipe);
// 等待客户机连接
Console.WriteLine( " This is a Server, host name is {0} " , Dns.GetHostName());
Console.WriteLine( " Waiting for a client... " );
// 4.得客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0 );
EndPoint remote = (EndPoint)sender;
// 5.接收客户机数据
byte [] buffer = new byte [ 1024 ];
socket.ReceiveFrom(buffer, ref remote);
CP2PMessage msg = new CP2PMessage();
msg = (CP2PMessage)Tool.BytesToStruct(buffer, msg.GetType());
Console.WriteLine( " 接收的值为:{0} " , msg.Value);
Console.ReadKey();
}
}
}
2.其中重要的一段就是把 结构体转为 byte数组
代码
using
System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace Lin.p2p.Mo
{
public class Tool
{
public static byte [] StructToBytes( object obj)
{
int size = Marshal.SizeOf(obj);
byte [] bytes = new byte [size];
IntPtr structPtr = Marshal.AllocHGlobal(size); // 分配结构体大小的内存空间
Marshal.StructureToPtr(obj, structPtr, false ); // 将结构体拷到分配好的内存空间
Marshal.Copy(structPtr, bytes, 0 , size); // 从内存空间拷到byte数组
Marshal.FreeHGlobal(structPtr); // 释放内存空间
return bytes;
}
public static object BytesToStruct( byte [] bytes, Type type)
{
int size = Marshal.SizeOf(type);
if (size > bytes.Length)
return null ;
IntPtr structPtr = Marshal.AllocHGlobal(size); // 分配结构大小的内存空间
Marshal.Copy(bytes, 0 , structPtr, size); // 将byte数组拷到分配好的内存空间
object obj = Marshal.PtrToStructure(structPtr, type);
Marshal.FreeHGlobal(structPtr);
return obj;
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace Lin.p2p.Mo
{
public class Tool
{
public static byte [] StructToBytes( object obj)
{
int size = Marshal.SizeOf(obj);
byte [] bytes = new byte [size];
IntPtr structPtr = Marshal.AllocHGlobal(size); // 分配结构体大小的内存空间
Marshal.StructureToPtr(obj, structPtr, false ); // 将结构体拷到分配好的内存空间
Marshal.Copy(structPtr, bytes, 0 , size); // 从内存空间拷到byte数组
Marshal.FreeHGlobal(structPtr); // 释放内存空间
return bytes;
}
public static object BytesToStruct( byte [] bytes, Type type)
{
int size = Marshal.SizeOf(type);
if (size > bytes.Length)
return null ;
IntPtr structPtr = Marshal.AllocHGlobal(size); // 分配结构大小的内存空间
Marshal.Copy(bytes, 0 , structPtr, size); // 将byte数组拷到分配好的内存空间
object obj = Marshal.PtrToStructure(structPtr, type);
Marshal.FreeHGlobal(structPtr);
return obj;
}
}
}
3. 最后看客户端
代码
//
1.创建套节字
m_s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// 2.填写服务器IP
IPAddress ip = IPAddress.Parse( " 127.0.0.1 " );
IPEndPoint ipe = new IPEndPoint(ip, 4321 );
// 向服务器发送本用户信息
CP2PMessage msg = new CP2PMessage();
msg.nMessageTypes = ( sbyte )Cmd.UserLogin;
msg.Value = 10 ;
var buffer = Fun.StructToBytes(msg);
m_s.SendTo(buffer, buffer.Length, SocketFlags.None, ipe);
Console.ReadKey();
m_s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// 2.填写服务器IP
IPAddress ip = IPAddress.Parse( " 127.0.0.1 " );
IPEndPoint ipe = new IPEndPoint(ip, 4321 );
// 向服务器发送本用户信息
CP2PMessage msg = new CP2PMessage();
msg.nMessageTypes = ( sbyte )Cmd.UserLogin;
msg.Value = 10 ;
var buffer = Fun.StructToBytes(msg);
m_s.SendTo(buffer, buffer.Length, SocketFlags.None, ipe);
Console.ReadKey();
4. 哈哈,当然, 效果来也~~