473,508 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.Net Sockets

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::Dgram, 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;
};

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 17 '05 #1
1 1235
Yawnb,

You can simply extract the header fields like so:

Byte code = RecvBytes[0];
Byte id = RecvBytes[1];

Extracting the length depends on whether it is encoded as little-endian
or big-endian.

Little-endian:

UInt16 length = BitConverter::ToUInt16(RecvBytes, 2);

Big-endian:

Byte lengthHigh = RecvBytes[2];
Byte lengthLow = RecvBytes[3];
UInt16 length = (lengthHigh << 8) | lengthLow;

Greetings

Bart Jacobs

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3880
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
1
3757
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
0
1786
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
3
10379
by: Logan McKinley | last post by:
I have a C# program that uses blocking sockets and want to allow the user to stop the server. The problem I am having is the socket blocks on...
1
20175
by: Adam Clauss | last post by:
I am (attempting) to move an existing socket application to use raw sockets. Right now, my application is essentially a port forwarder. Upon receiving a connection, it will open a connection to...
4
6297
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
4335
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
3
11567
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
7
6703
by: Adam01 | last post by:
Im using cygwin to test the code of a server I am writing. I've included sys/types.h, sys/socket.h, netdb.h, and arpa/inet.h. And this is the output.. ../../../sockets.cpp: In constructor...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.