variable Bytes question

G

Guest

hi, to get the bytes sent by a socket, in VB a simple local variable like
below was used

Dim recv as Byte()

how can i do that in C++...this is what i am trying now

Byte * rec[] = new Bytes;

but i get these errors:

x:\User\FinalProject\Battleship\NewGame.h(180): error C2691: 'unsigned char
__gc *' : invalid type for __gc array element
x:\User\FinalProject\Battleship\NewGame.h(180): error C2061: syntax error :
identifier 'Bytes'

how can i get the bytes variable so i can receive the text sent by the other
connected socket? thanks
 
B

Bruno van Dooren

this will give you a simple array of bytes.

Byte test[] = new Byte[1];
Byte test2 = 'A';

test[0] = test2;


kind regards,
Bruno.
 
G

Guest

but then wat if wats received is ghreater than 100 bytes? the vb declaration
didnt have a constraint....is there anywasy to do that or do i have to limit
how much i send?
 
D

doug mansell

iwdu15 said:
but then wat if wats received is ghreater than 100 bytes? the vb declaration
didnt have a constraint....is there anywasy to do that or do i have to limit
how much i send?

100 bytes? Bruno's example was a one byte array. But anyway, the trick
to not overflowing an array is to not read more bytes than can fit in it.

doug
 
G

Guest

if possible, always work with a fixed buffer size.
this will be more performant than always allocating and de-allocating buffers.

i don't know what you intend to send, but if it is not too much, just
allocate an array that will always be big enough. use a 1 MB buffer if that
solves you problem.
IMO that is a much better solution than having to manage variable length
buffers.

having to add xxx lines of memory management code to save a few bytes of
data is not worth it in most cases.
but in all cases, check the array size before you write data to it.

kind regards,
Bruno.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top