2

I'm encountering a strange problem with my program.

I've been using UDP to transfer a file, with the standard sendto and recvfrom.

I have the server and client communicate back and forth many times during the program.

I've just added a new sendto call in the server's code, and it's giving me this error on execution.

Address family not supported by protocol. and that's coming from my sendto function.

Here is the code it's executing:

if ((numbytes = sendto(sockfd,sDropped,strlen(sDropped), 0,
         (struct sockaddr *)&their_addr, p->ai_addrlen)) == -1) {
    perror("talker: sendto");
    exit(1);
    }

As i said, this code works about 4 other times earlier in the program, so I'm sure i've got everything hooked up right.

Is anyone familiar with this issue, and could give me some insights on what to check for in my program that might produce this problem?

Thanks.

user974703
  • 1,653
  • 4
  • 20
  • 27
  • 1
    I suspect `their_addr` is not a valid `struct sockaddr_in` or whatever would be the right structure for the address family in use. Perhaps it's a raw IP address without `sockaddr` structure headers...? I think you should be using `p->ai_addr`. – R.. GitHub STOP HELPING ICE Oct 21 '11 at 01:09
  • As apparently something was not set up/defined properly despite appearances, you should add your code for sockfd, what structure p points to, and their_addr, plus prio relevant socket(), connect() (probably none as UDP) calls etc. It's hard to otherwise see what is going on. – gnometorule Oct 21 '11 at 01:10
  • Based on the error, you might be using a tcp socket with a UDP send(), or something like that. – gnometorule Oct 21 '11 at 01:13
  • What I don't understand is it has had no problem functioning before now... It goes through 4 or so calls no problem, sends the data and has no complaints. Wouldn't any of the problems mentioned effect every call? – user974703 Oct 21 '11 at 01:50
  • 1
    Then you probably have a memory corruption bug overwriting the address structure; perhaps a buffer overflow in an adjacent array variable... – R.. GitHub STOP HELPING ICE Oct 21 '11 at 02:14
  • See also https://stackoverflow.com/a/40615554/1979048 – Jonathan Ben-Avraham May 31 '18 at 14:52

2 Answers2

10

See if their_addr.sin_family is set, just wasted a couple of hours fighting with the same message just for that mistake.

Iskren
  • 1,301
  • 10
  • 15
0

Use of a cast means any old junk in their_addr will compile.

(struct sockaddr *)&their_addr

Is their_addr already a pointer? so &their_addr gives a pointer to a pointer?

teknopaul
  • 6,505
  • 2
  • 30
  • 24