4

I don't understand my warning in gcc compiler. Warning is: warning: pointer targets in passing argument 6 of ‘recvfrom’ differ in signedness I don't know, where is a problem, I am not using signed and unsigned value.

Problem is on line:

recvfrom(server_socket, inputbuffer, maxLenght, 0, (struct sockaddr*) remote_addr, &server_addr_len);

I tried this:

recvfrom(server_socket, inputbuffer, maxLenght, 0, (unsigned int) remote_addr, &server_addr_len);

But it didn't help me. Thank you for your advice and explanation.

alk
  • 66,653
  • 10
  • 83
  • 219
staigoun
  • 65
  • 1
  • 7

3 Answers3

5

From man recvfrom():

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
                    struct sockaddr *src_addr, socklen_t *addrlen);

recvfrom() expects a socklen_t as 6th parameter. You probably pass an int.

So define server_addr_len like so:

socklen_t server_addr_len;
alk
  • 66,653
  • 10
  • 83
  • 219
  • `socklen_t server_addr_len;` is both declaration **and** definition. `extern socklen_t server_addr_len;` would be a declaration only. `socklen_t server_addr_len = 0;` is declaration **and** definition **and** initialisation. @iharob For your reference: http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – alk Dec 23 '14 at 13:16
  • Thank you sir for proving the link. One question though, is there anything to do with the storage class? I mean to say, can I phrase like this "_in case of `auto` storage, `socklen_t server_addr_len;` is always a valid definition, however, for `extern`, this might not be always the case_", keeping in mind, `-fno-common` is missing at compile time? – Sourav Ghosh Dec 23 '14 at 13:31
  • 1
    A "declaration" is a promise, a "definition" is a fact. @SouravGhosh – alk Dec 23 '14 at 13:34
2

See the man page of recvfrom().

It says, the function prototype is

 ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
             struct sockaddr *src_addr, socklen_t *addrlen);

The 6th argument is socklen_t *addrlen. So, while calling recvfrom() from your application, you have to use it like

socklen_t server_addr_len = 0;
struct sockaddr * remote_addr = NULL;
ssize_t retval = 0;
.
. 
retval = recvfrom(server_socket, inputbuffer, maxLenght, 0, remote_addr, &server_addr_len);

SideNotes:

1. Define the variables in a way so that they do not need casting. Good practice.

2. check the return value of recvfrom() [or for that case, any library call] for success.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
  • @iharob yeah difference is there. but why you say to _declare_, why not use _define_ term here? can you elaborate please? thank you. – Sourav Ghosh Dec 23 '14 at 12:35
  • Only `usigned int` make my compiler happy. – staigoun Dec 23 '14 at 12:38
  • @iharob well, right, but usually, it's a better practice to initialize the variables. Combined, it makes a definition. :-) – Sourav Ghosh Dec 23 '14 at 12:39
  • @staigoun maybe, in your case, `socklen_t` is a typedef of `unsigned int`... why take chances? – Sourav Ghosh Dec 23 '14 at 12:40
  • @iharob: "*`socklen_t server_addr_len;`*" is a perfect valid definition (and an implicit declaration). Whereas "*`server_addr_len = someValue;`*" is not a definition but an initialisation! – alk Dec 23 '14 at 13:18
1

Just declare server_addr_len as socklen_t

socklen_t server_addr_len;

since the recvfrom function signature is

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
         struct sockaddr *src_addr, socklen_t *addrlen);
Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
  • Thank you for your reply, but `gcc`compiler cannot compile this. – staigoun Dec 23 '14 at 12:23
  • Do not cast a pointer to a pointer of a different type. This could lead to desaster in case the two types pointed to are of different size. – alk Dec 23 '14 at 12:27