1

I get this error when i run the following code , on line 21

Error

error invalid conversion from int to const char* [fpermissive]

Code

receive(struct sockaddr_in sockad, struct message m){
  int rc;
  int i;
  int ibuf;
  i = sizeof(sockad);
  rc = recvfrom(sd,&m,strlen(ibuf),0,(struct sockaddr *)&sockad,(unsigned long)&i ); /* line 21*/ 
  if (rc < 0) {
    perror("recvfrom"); exit(1);
  }
}

I tried the solutions that in stackoverflow but no luck

  • 3
    There are several things wrong with this fragment, but without seeing the _complete program_ we can't tell you how to fix it. (For instance, I strongly suspect the prototype for `receive` is wrong, so I need to see the caller.) – zwol May 08 '17 at 21:19
  • 1
    You are passing `ibuf`, an `int`, to function `strlen()`, which expects its argument to be a `char *`, pointing to a null-terminated array of characters. Since `ibuf` is not even initialized, that cannot be what you actually want to do. – John Bollinger May 08 '17 at 21:19
  • 1
    Also: `int i;` --> `socklen_t i;` – chux - Reinstate Monica May 08 '17 at 21:20
  • In order to avoid a lot of guessing, please provide more code. Need code which calls `receive()`.. ALso need code for the function `recvfrom()`. – Nguai al May 08 '17 at 22:16

2 Answers2

3

The error is caused by strlen(ibuf)... when int ibuf...

You should pass a string to strlen, although you might have intended to use sizeof(m).

You might have also intended to use pointers, as in struct message *m, so that:

void receive(struct sockaddr_in * sockad, struct message * m){
  int rc;
  socklen_t i;
  i = sizeof(*sockad);
  rc = recvfrom(sd, m, sizeof(*m),0, sockad, &i ); /* line 21*/ 
  if (rc < 0) {
    perror("recvfrom"); exit(1);
  }
}
Myst
  • 18,516
  • 2
  • 45
  • 67
  • so what should i have to do? – Mariyam Mohammed Jalil May 08 '17 at 21:18
  • `(unsigned long)&i` with `i` an uninitialized `int` is also wrong, and the `struct sockaddr_in` argument also probably wants to be a pointer to a variable in the caller. – zwol May 08 '17 at 21:21
  • @MariyamMohammedJalil, I'm not sure what your initial intention was, but I updated the answer with more details. – Myst May 08 '17 at 21:22
  • @zwol - Yeah, I just noticed, nice catch. – Myst May 08 '17 at 21:22
  • @Myst One more thing, `socklen_t` is not the same thing as `unsigned long` - the whole point of its being different from `size_t` is that it needs to _not_ be `unsigned long` on 64-bit ABIs, for reasons which will not fit in this comment box. – zwol May 08 '17 at 21:26
  • ... and I suppose this ignores the possibility of a short message, but I don't actually know how to handle short UDP messages myself, so :shrug: – zwol May 08 '17 at 21:28
  • @zwol - Thanks :) I love answering here because I learn new things every time I'm exposed to other people approaches. I would probably avoid this sort of specialized encapsulation (as well as the `exit` on error), but I don't have much experience with UDP. – Myst May 08 '17 at 21:32
0

The issue is with the code:

strlen(ibuf)

ibuf is an integer, not string.

Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33