How do I send an array of integers over TCP in C?

后端 未结 7 489
小蘑菇
小蘑菇 2020-12-17 06:39

I\'m lead to believe that write() can only send data buffers of byte (i.e. signed char), so how do I send an array of long integers using the C write()

相关标签:
7条回答
  • 2020-12-17 07:25

    Yes, you can just cast a pointer to your buffer to a pointer to char, and call write() with that. Casting a pointer to a different type in C doesn't affect the contents of the memory being pointed to -- all it does is indicate the programmer's intention that the contents of memory at that address be interpreted in a different way.

    Just make sure that you supply write() with the correct size in bytes of your array -- that would be the number of elements times sizeof (long) in your case.

    0 讨论(0)
  • 2020-12-17 07:29

    Declare a character array. In each location of the array, store integer numbers, not characters. Then you just send that.

    For example:

    char tcp[100];
    
    tcp[0] = 0;
    tcp[1] = 0xA;
    tcp[2] = 0xB;
    tcp[3] = 0xC;
    .
    .
    
    // Send the character array
    write(sock, tcp, sizeof(tcp));
    
    0 讨论(0)
  • 2020-12-17 07:29

    I think what you need to come up with here is a protocol.

    Suppose your integer array is:

    100, 99, 98, 97
    

    Instead of writing the ints directly to the buffer, I would "serialize" the array by turning it into a string representation. The string might be:

    "100,99,98,97"
    

    That's what would be sent over the wire. On the receiving end, you'd split the string by the commas and build the array back up.

    This is more standardised, is human readable, and means people don't have to think about hi/lo byte orders and other silly things.

    // Sarcasm

    If you were working in .NET or Java, you'd probably encode it in XML, like this:

    <ArrayOfInt><Int>100</Int><Int>99</Int><Int>98</Int><Int>97</Int></ArrayOfInt>
    

    :)

    0 讨论(0)
  • 2020-12-17 07:34

    The simplest way to send a single int (assuming 4-byte ints) is :

    int tmp = htonl(myInt);
    write(socket, &tmp, 4);
    

    where htonl is a function that converts the int to network byte order. (Similarly,. when you read from the socket, the function ntohl can be used to convert back to host byte order.)

    For an array of ints, you would first want to send the count of array members as an int (in network byte order), then send the int values.

    0 讨论(0)
  • 2020-12-17 07:38

    It would be better to have serialize/de-serialize functionality in your client /server program.

    Whenever you want to send data, serialize the data into a byte buffer and send it over TCP with byte count.

    When receiving data, de-serialize the data from buffer to your own interpretation .

    You can interpret byte buffer in any form as you like. It can contain basic data type, objects etc.

    Just make sure to take care of endianess and also alignment stuff.

    0 讨论(0)
  • 2020-12-17 07:42

    the prototype for write is:

    ssize_t write(int fd, const void *buf, size_t count);
    

    so while it writes in units of bytes, it can take a pointer of any type. Passing an int* will be no problem at all.

    EDIT:

    I would however, recomend that you also send the amount of integers you plan to send first so the reciever knows how much to read. Something like this (error checking omitted for brevity):

    int x[10] = { ... };
    int count = 10;
    write(sock, &count, sizeof(count));
    write(sock, x, sizeof(x));
    

    NOTE: if the array is from dynamic memory (like you malloced it), you cannot use sizeof on it. In this case count would be equal to: sizeof(int) * element_count

    EDIT:

    As Brian Mitchell noted, you will likely need to be careful of endian issues as well. This is the case when sending any multibyte value (as in the count I recommended as well as each element of the array). This is done with the: htons/htonl and ntohs/ntohl functions.

    0 讨论(0)
提交回复
热议问题