Reputation: 1696
I'm continuously sending arrays of pixel values from LabVIEW to a C-program through TCP/IP connection. The C-program receives a stream of bytes, so I'm converting the bytes to uint32 using the following code to print all the pixel values in an array. Does anyone know how to switch the byte-order to little-endian in this case?:
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
int iResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
typedef unsigned int uint32_t;
unsigned int i;
size_t len;
uint32_t* p;
uint32_t* endp;
uint32_t value;
p = (uint32_t*)((void*)recvbuf);
do
{
iResult = recv(new_socket, recvbuf, recvbuflen, 0);
len = iResult/sizeof(uint32_t);
for(i=0; i < len; i++)
{
value = p[i];
}
printf("%d\n", value);
}
while ( iResult > 0 );
closesocket(new_socket);
WSACleanup();
return 0;
}
Upvotes: 1
Views: 1132
Reputation: 596307
Use htonl()
to convert a 32-bit integer from host byte order to network byte order before sending it, and use ntohl()
to convert a 32-bit integer from network byte order to host byte order after receiving it. There are also htons()
and ntohs()
for 16-bit integers. Let the functions decide how to convert the values for you. On little endian systems, the functions are implemented to perform actual conversions. On big endian systems, the functions are implemented as no-ops.
Upvotes: 1
Reputation: 60007
Use the family of macros/functions http://linux.die.net/man/3/ntohs to send data over the network. Then you will not have any problems at all.
Upvotes: 0