• Welcome to Valhalla Legends Archive.
 

Sockets

Started by Mangix, September 17, 2005, 07:04 PM

Previous topic - Next topic

MyndFyre

Mangix, you have to ask yourself, why are you trying to change a 4 into a 4?

Pretty much, the BitConverter class exists to provide a type- and memory-safe way of doing this:


uint val;
byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// this is BitConverter's magic:
fixed (byte *pBuf = &buffer) {
  pBuf += 2; // move the pointer to the third element in the array
  uint* pBufferAsUint = (uint*)pBuf;
  val = *pBufferAsUint;
}

This is the same as doing:

uint val;
byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
val = BitConverter.ToUInt32(buffer, 2);

In either scenario, the variable val holds the value 0x06050403.  But, by using the second type of code, you increase code safety, prevent memory leaks, and increase code verifiablity (this is what .NET was designed for).  Best of all, you can avoid pointer use.

For question 2.... you send the data you want to send to the other end of the connection?

I really don't know what you want me to say besides the obvious.  I've cited this buffer on numerous occasions -- you have to inherit from it, but if you made your own, this is a good model.  Example:

/// <summary>
/// The collection of bytes to send.
/// </summary>
private MemoryStream m_data;

The MemoryStream class is somewhat like a pointer reader/writer when combined with the BinaryReader and BinaryWriter classes (all from the System.IO namespace).  In fact, the buffer I provided doesn't even use BinaryReader/BinaryWriter -- it uses BitConverter to transform data into byte arrays (I think that could be improved upon).  But pretty much, it works like this:
1.) Create a MemoryStream
2.) Write to the MemoryStream.  The current position in the stream is advanced by as many bytes as were written.
3.) Repeat step 2 until all data is written.
4.) Obtain the data in the MemoryStream in a byte[].
5.) Send the data via the Send() method.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

in C#, every time i enter a number, it considers it to be of type int even though i set it to be something else. dunno why -_-. also thank's for the byte[] thing.

as for the socket.Send, nice explination but i still dont get something. the BitConverter class cannot make a byte array out of a string(obviously) so is there any way to do that?

MyndFyre

No.  Treat numbers as numbers, not as strings like you do in VB.  Treating them as strings is the only way to make a somewhat-efficient buffer.

If you need to, I know the C# compiler has suffixes for every primitive type (for example, to make a uint, use

uint someNum = 0xf8329fcdu;

-- note the "u" suffix at the end of the number).

For bytes, I don't know what the suffix is, but you can just cast:

byte someNum = (byte)0xcd;

Note that, using hex notation, you can't have a value greater than 0xff, or if using decimal, more than 255.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mangix

isnt it 256?

and i dont make em as strings. i do something like this byte g = 4; and it immediatly considers it to be an integer. also does (byte) even work? i've tried doing the same method of converting types to another type but it never works.

MyndFyre

Quote from: Mangix on September 19, 2005, 07:52 PM
isnt it 256?
No.  A byte can store 256 unique values from 0 to 0xff or 255.

Quote from: Mangix on September 19, 2005, 07:52 PM
and i dont make em as strings. i do something like this byte g = 4; and it immediatly considers it to be an integer. also does (byte) even work? i've tried doing the same method of converting types to another type but it never works.
So try byte g = (byte)4;.
Sometimes casting doesn't work, but that's if you're using nonprimitive or uncastable types.  For example:

uint f = 0x400583;
byte b = (byte)f;

That will raise a compiler error because you can lose information in the conversion.  You could:

uint f = 0x400583;
byte b = (byte)(f & 0xff);

which would yield the result you're looking for.

Also, conversion between signed/unsigned is often erroneous.  For example:

int f = -1; // -1 is 0xffffffff
uint g = (uint)f; // compiler exception

Proper way to do this is via unchecked in one of the following ways:

int f = -1;
uint g;
g = unchecked((uint)f);
// or
unchecked
{
    g = (uint)f;
}
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.