问题
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);
}
回答1:
Just cast the pointer to the correct type. So use:
(char *) (&buffer[++index])
Also, why are you creating a blocking scheme by repeating a non-blocking operation in a sleep loop? Either use blocking sockets or use non-blocking sockets, but don't create some fake in-between thing. (If, for example, a malicious or slow client sends you only one byte, you'll spin on recv
.)
Lastly, why are you reading only one byte in the first call to recv
? You need the rest of the data anyway, so why make the kernel give it to you in tiny drops? Why not read as many bytes as you can and, with luck, avoid the need to call recv
a second time?
来源:https://stackoverflow.com/questions/32386158/using-unsigned-char-instead-of-char-because-of-its-range