简体   繁体   中英

UDP recvfrom warning with gcc compiler

I am receiving the following warning when compiling my client - server UDP socket simulation:

warning: passing 'int *' to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with different sign [-Wpointer-sign]

This is the concerned code:

#define BUFLEN 2048
struct sockaddr_in myaddr, remaddr;
int fd, recvlen, slen=sizeof(remaddr);
...
char response[BUFLEN];
recvlen = recvfrom(fd, response, BUFLEN - 1, 0, (struct sockaddr *)&remaddr, &slen);

I have been referencing a tutorial from Rutgers university. I have a fair idea as to why this warning is occuring (it wants me to use socklen_t * ), but I wanted to ask the SO community.

  1. Why is this warning occuring?
  2. How can I get rid of it?

it wants me to use socklen_t *

Correct.

Why is this warning occuring?

Because you aren't using socklen_t .

How can I get rid of it?

Declare slen as socklen_t .

Why is this warning occuring?

The warning is occurring because socklen_t is an unsigned type, and int is a signed type (as the diagnostic says). In principle, therefore, the pointed-to value may be interpreted differently by recvfrom() than it is by the caller. In practice, such a difference won't happen with GCC-compiled code as long as only correct values of the length of the address struct are used, but GCC can't check that at compile time.

How can I get rid of it?

Pass an argument of the correct type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM