Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Sockets - Can i only send characters?

I'm using synchronised sockets with a win32 window, and using the send() and recv() function to send data over internet TCP;

what i'm wondering, how would i send some integers or even my own class/structure over the tcp socket? because the send() function only lets me send characters.

Would i just have to send characters and then maybe convert them to integer with atoi()? or if i wanted to send a class structure, would i send many strings over and then put them in the variables.. one by one.

like image 593
Kaije Avatar asked Jan 22 '23 15:01

Kaije


1 Answers

It isn't sending characters in the textual sense - it's sending contiguous arrays of bytes, which it refers to using a char*. You can point to the bytes of any value type this way, so if you wanted to send an int,

int A = 5;
const char* pBytesOfA = (const char*)&A;
int lengthOfBytes = sizeof(A);

send(socket, pBytesOfA, lengthOfBytes, flags);
like image 168
Nicholas M T Elliott Avatar answered Jan 28 '23 03:01

Nicholas M T Elliott