This articleArticleIt was written on Friday evening, because I went on a tour on the 6 th and didn't come soon.
1. Similarly, let's take a look at this simple struct.
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. runtime. interopservices;
Namespace Csharp_socket_5
{
/// <Summary>
/// Communication Message format
/// </Summary>
[Serializable]
[Structlayout (layoutkind. Sequential, pack = 1 )]
Public Struct Cp2pmessage
{
Public Ushort ID;
}
}
C ++ version
Struct Cp2pmessage
{
Word ID;
};
2. C # server, just as before ~~ Is it very kind ~
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. net. Sockets;
Using System. net;
Using System. runtime. interopservices;
Namespace Csharp_socket_5
{
Class Program
{
/// <Summary>
/// Server
/// </Summary>
/// <Param name = "ARGs"> </param>
Static Void Main ( String [] ARGs)
{
// 1. Create a set of characters
Socket socket = New Socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP );
//2. Fill in the IP address
Ipendpoint IPE= NewIpendpoint (IPaddress. Any,4321);
//3. Bind
Socket. BIND (IPE );
// waiting for client connection
console. writeline ( " this server, host Name is {0} " , DNS. gethostname ();
console. writeline ( " wait for the client to connect .... " );
// 4. obtain the Client IP address
ipendpoint sender = New ipendpoint (IPaddress. any, 0 );
endpoint remote = (endpoint) sender;
// 5. receive client data
byte [] buffer = New byte [ 1024 ];
socket. receivefrom (buffer, ref remote );
Cp2pmessage msg= NewCp2pmessage ();
MSG=(Cp2pmessage) bytestostruct (buffer, MSG. GetType ());
Console. writeline ("Received by C ++: {0}", MSG. ID );
Console. readkey ();
}
Public Static Byte [] Structtobytes ( Object OBJ)
{
Int Size = Marshal. sizeof (OBJ );
Byte [] Bytes = New Byte [Size];
Intptr structptr = Marshal. allochglobal (size ); // Allocate memory space of the struct size
Marshal. structuretoptr (OBJ, structptr, False ); // Copy the struct to the allocated memory.
Marshal. Copy (structptr, bytes, 0 , Size ); // Copy From memory space to byte array
Marshal. freehglobal (structptr ); // Release memory space
Return Bytes;
}
Public Static Object Bytestostruct ( Byte [] Bytes, type)
{
Int Size = Marshal. sizeof (type );
If (Size > Bytes. length)
Return Null ;
Intptr structptr = Marshal. allochglobal (size ); // Allocate memory space of the structure size
Marshal. Copy (bytes, 0 , Structptr, size ); // Copy the byte array to the allocated memory space
Object OBJ = Marshal. ptrtostructure (structptr, type );
Marshal. freehglobal (structptr );
Return OBJ;
}
}
}
3. Focus, C ++ Client
Code
# Include < Winsock2.h >
# Include < Stdio. h >
# Pragma Comment (Lib, "ws2_32.lib ")
StructCp2pmessage
{
Word ID;
};
VoidMain ()
{
//Initialize ws2_32.dll
Wsadata;
: Wsastartup (makeword (2,2),&Wsadata );
// 1. create a set of characters
socket S = :: socket (af_inet, sock_dgram, ipproto_udp);
// 2. enter the Server IP address
sockaddr_in sin;
sin. sin_family = af_inet;
sin. sin_port = :: htons ( 4321 );
sin. sin_addr.s_un.s_addr = :: inet_addr ( " 127.0.0.1 " );
// 3. send a message to the server
cp2pmessage MSG;
MSG. ID = 10 ;
: Sendto (S ,(Char*)&MSG,Sizeof(MSG ),0, (Sockaddr*)&Sin,Sizeof(SIN ));
}
4. Finished., so, please see !!