c - Write n bytes and read n bytes: sending number of bytes to read using uint16_t -


i've been using these read , write functions (provided @alk).
problem don't know how correctly send uint16_t data_size;.
here actual code send general example buffer:

uint16_t data_size; int retry_on_interrupt = 0; char buffer[] = "hello world!"; data_size = (uint16_t) sizeof(buffer);      /* sending data_size */ writen(socket, data_size, 2, retry_on_interrupt);  /* sending buffer */ writen(socket, buffer, sizeof(buffer);   

and here actual code receive general example buffer:

/* receiving data_size */ readn(socket, &data_size, 2);  /* receiving buffer */ readn(socket, buffer, data_size); 

but not working, think because writen requires const char *, instead i'm using uint16_t...
how should these calls be? thanks.

replace

writen(socket, data_size, 2, retry_on_interrupt); 

by

if (-1 == writen(socket, &data_size, sizeof data_size, 1)) {    perror("writen() failed writing size");    exit(exit_failure); } 

and replace line

writen(socket, buffer, sizeof(buffer);   

by

if (-1 == writen(socket, buffer, data_size, 1)) {    perror("writen() failed writing pay-load");    exit(exit_failure); } 

you want add error-checking reading functions well!


also want take care of possible endianness (byte-order) issue, when sending/receiving between different hardware-platforms.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -