I've been working on a small pure C client application (my first :/) which uses TCP socket for communication with the server. The Server sends me a packet (C structure) in which the first byte contains the size of the packet.
The problem is that server is using unsigned char to represent the size of the packet because char is signed (from -128 to +127) and +127 is not enough to represent size that can be up to 255 in some packets. => I need a unsigned char buffer;
In Linux, the second parameter of recv() function is void * which means I can declare a void *buffer and there is no problem. But recv() in Windows (MinGW) has char * instead of void *. Which give me warning "Parameter type mismatch: Incompatible pointer types 'char *' and 'unsigned char *'"
Is it possible to solve this problem? Here is the code. Thanks.
PS: I'm using NON BLOCKING sockets.
int recvsize = 0;
unsigned char tmpsize;
int index = 0;
unsigned char *buffer;
while (1) {
recvsize = recv(server, &tmpsize, sizeof(unsigned char), 0); // every packet starts with one byte where is its length
if (recvsize > 0 ) {
buffer = malloc(tmpsize * sizeof(unsigned char)); //memory allocation according to the size of packet
buffer[0] = tmpsize--; //get back the size value to the buffer
recvsize = 0;
do { //loop over and over until you do not have all bytes of the packet
recvsize = recv(server, &buffer[++index], tmpsize, 0);
if (recvsize == 0)
break;
tmpsize -=recvsize;
index += recvsize;
} while (tmpsize != 0);
}
sleep(50);
}
charmight be signed on your platform. The standard leaves this to the implementation, In general, when transfering data between different systems (and most times also in the same system), usingstdint.htypes and proper serialization is the prefered way. Otherwise a tarpit is waiting. - too honest for this sitesizeof(unsigned char)is always 1, so... * sizeof(unsigned char)is not needed. Should code want to note the scaling by the size of the variable, suggestbuffer = malloc(tmpsize * sizeof *buffer)- chux - Reinstate Monica