由于其范围,所以使用unsigned char而不是char [英] Using unsigned char instead of char because of its range

查看:65
本文介绍了由于其范围,所以使用unsigned char而不是char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个小型的纯C客户端应用程序(我的第一个:/),该应用程序使用TCP套接字与服务器进行通信.服务器向我发送一个数据包(C结构),其中第一个字节包含该数据包的大小.

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.

问题在于服务器正在使用无符号char来表示数据包的大小,因为char是已签名的(从-128到+127),而+127不足以表示某些数据包中的最大大小为255的大小.=>我需要一个未签名的char缓冲区;

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;

在Linux中,recv()函数的第二个参数是void *,这意味着我可以声明一个void * buffer,没有问题.但是Windows(MinGW)中的recv()具有char *而不是void *.给我警告参数类型不匹配:指针类型'char *'和'unsigned char *'不兼容"

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:我正在使用NON BLOCKING套接字.

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);
}

推荐答案

只需将指针转换为正确的类型.因此使用:

Just cast the pointer to the correct type. So use:

(char *) (&buffer[++index])

此外,为什么还要通过在睡眠循环中重复非阻塞操作来创建阻塞方案?使用阻塞套接字或使用非阻塞套接字,但不要在中间创建任何虚假的东西.(例如,如果恶意或缓慢的客户端仅向您发送一个字节,则您将打开 recv .)

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.)

最后,为什么在第一次调用 recv 时只读取一个字节?无论如何,您仍然需要其余的数据,那么为什么要让内核以极小的液滴将其提供给您呢?为什么不读尽可能多的字节,如果幸运的话,又避免了第二次调用 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?

这篇关于由于其范围,所以使用unsigned char而不是char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆