Why are the buffers in VxWorks sendto() and send() functions of different types, and how do I handle them in the same manner?

StackOverflow https://stackoverflow.com/questions/11401936

Question

In VxWorks, sendto() is defined as:

int sendto
(
  int               s,      /* socket to send data to */
  caddr_t           buf,    /* pointer to data buffer */
  int               bufLen, /* length of buffer */
  int               flags,  /* flags to underlying protocols */
  struct sockaddr * to,     /* recipient's address */
  int               tolen   /* length of to sockaddr */
)

and send() is defined as:

int send
(
  int          s,           /* socket to send to */
  const char * buf,         /* pointer to buffer to transmit */
  int          bufLen,      /* length of buffer */
  int          flags        /* flags to underlying protocols */
)

My issue is that the buffer in sendto() is not constant, while the buffer in send() is. This doesn't seem to comply with the POSIX standard. I'm also writing my code to cross-compile, so the buffer is passed in as a const void* rather than a const char*. I can get the functions to act similarly if not the same by calling the functions as below:

//assuming socket data is already populated

send(fileDescriptor, 
   reinterpret_cast<const char*>(pBuffer),
   bufferLength,
   flags);

sendto(fileDescriptor,
   const_cast<char*>(reinterpret_cast<const char*>(pBuffer)),
   bufferLength,
   flags,
   destinationAddr,
   destAddrlen);

It just seems to be that there should be a more elegant solution to use these two functions on the same piece of data.

It's also important to note that, while this may work, I may need to remove all typecasting from my code. Is there a way to tackle this without typecasting, implicit or explicit?

Was it helpful?

Solution

Other than changing the datatype of pBuffer itself, I didn't find a more elegant solution.

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