Y
yawnb
I am using VC++ .Net 2003 and have a question about putting a byte
array returned from a socket receivefrom method in to a struct.
Here is what I have:
Socket* s = 0;
Byte RecvBytes[] = new Byte[1500];
try {
IPEndPoint* endPoint = new IPEndPoint(IPAddress::Any, 1646);
s = new Socket(endPoint->Address->AddressFamily,
SocketType:gram, ProtocolType::Udp);
s->Bind(endPoint);
Console::WriteLine (S"Waiting to receive datagrams from client...");
IPEndPoint* sender = new IPEndPoint(IPAddress::Any, 0);
EndPoint* senderRemote = __try_cast<EndPoint*>(sender);
Int32 LengthBytes = s->ReceiveFrom(RecvBytes, 0,
RecvBytes->Length, SocketFlags::None, &senderRemote);
Console::WriteLine(S"Datagram Received from -> {0} {1} Bytes",
senderRemote->ToString(), Convert::ToString(LengthBytes));
I get a the RecvBytes byte array back and I want to put it in a
structure.
__gc struct auth_hdr {
Byte code;
Byte id;
UInt16 length;
Byte vector[];
Byte data[];
};
What is the best way to do it? I have tried to cast it like auth_hdr*
authhdr = reinterpret_cast<auth_hdr*>(RecvBytes) but it does
not work. Would it be better to try something like:
[StructLayout(LayoutKind::Explicit)]
public __gc class Packet
{
public:
[FieldOffset(0)] Byte Code;
[FieldOffset(1)] Byte ID;
[FieldOffset(2)] UInt16 Length;
[FieldOffset(20)] Byte Vector;
[FieldOffset(38)] Byte Data;
};
array returned from a socket receivefrom method in to a struct.
Here is what I have:
Socket* s = 0;
Byte RecvBytes[] = new Byte[1500];
try {
IPEndPoint* endPoint = new IPEndPoint(IPAddress::Any, 1646);
s = new Socket(endPoint->Address->AddressFamily,
SocketType:gram, ProtocolType::Udp);
s->Bind(endPoint);
Console::WriteLine (S"Waiting to receive datagrams from client...");
IPEndPoint* sender = new IPEndPoint(IPAddress::Any, 0);
EndPoint* senderRemote = __try_cast<EndPoint*>(sender);
Int32 LengthBytes = s->ReceiveFrom(RecvBytes, 0,
RecvBytes->Length, SocketFlags::None, &senderRemote);
Console::WriteLine(S"Datagram Received from -> {0} {1} Bytes",
senderRemote->ToString(), Convert::ToString(LengthBytes));
I get a the RecvBytes byte array back and I want to put it in a
structure.
__gc struct auth_hdr {
Byte code;
Byte id;
UInt16 length;
Byte vector[];
Byte data[];
};
What is the best way to do it? I have tried to cast it like auth_hdr*
authhdr = reinterpret_cast<auth_hdr*>(RecvBytes) but it does
not work. Would it be better to try something like:
[StructLayout(LayoutKind::Explicit)]
public __gc class Packet
{
public:
[FieldOffset(0)] Byte Code;
[FieldOffset(1)] Byte ID;
[FieldOffset(2)] UInt16 Length;
[FieldOffset(20)] Byte Vector;
[FieldOffset(38)] Byte Data;
};