Question

I have a client/server program I'm writing, I need to send information from client to server using the function send(SOCKET s, const char *buf, int len, int flags); but apparently this function is made to said a string, array of characters, but what I'm sending is encrypter message and the characters values large and character type isn't enough.

is there another function that allows me to do so ? I looked the reference in microsoft website but I didn't get the other functions.

if there's another function I hope you can show me how or give me hints, or if there's another way to do then so be it.

Notes: I'm working with C++ under Windows 8, using Borland 6

Was it helpful?

Solution

This might be tricky to explain.

Your issue isn't in the function you're using, but in the concept you're trying to apply.

First of all, if your data is intended to be transmitted through network, you must assume that the destination endpoint endianness may differ from the transmitting endpoint.

With that in mind, it's advisable to convert the eligible data types prone to endianness interpretation to network byte order before transmitting any data. Take a look at the htons(), htonl(), ntohs() and ntohl() functions.

As you must deal with known data sizes, instead of declaring your array as int[], you should declare it through a stdint.h type, such as int16_t, int32_t, uint16_t, etc.

So, lets assume you've the following:

uint32_t a[4] = { 1, 2, 3, 4 };

If you want to transmit this array in a portable way, you should first convert its contents to network byte order:

uint32_t a_converted[4];

for (int i = 0; i < sizeof(a); i ++)
   a_converted[i] = htonl(a[i]);

Now, if you want to transmit this array, you can do it using:

send(s, (char *) a_converted, sizeof(a_converted), flags);

Just remember that the code for receiving this data, should convert it from network byte order to host byte order, using, in this case, the ntohl() for each element received.

Hope this gives you some clues for further research.

OTHER TIPS

Well doodleboodle, guess what, if you read the TCP RFC, you might under stand that the TCP protocol only transfers OCTET STREAMS and, if you need to transfer anything more complex than one byte, you need a protocol on top of TCP that defines your Application Protocol Unit message type.

send(SOCKET s, const char *buf, int len, int flags); is basically the way to do it.

It uses binary data in bytes to send the data. So if you want to send a complex structure/object, you'll need to serialize it to a byte array first.

In your case with the integers it's quite simple: just convert the integer array to a byte array. (keep track of the length though).

Of course it's more appropriate to build an abstraction layer on top of your TCP layer so it's easier to send/receive different kinds of data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top