# Sockets tutorial, datagram sockets example works under Linux but not FreeBSD



## caesius (Dec 21, 2009)

I'm learning C net programming from  Beej's guide to network programming. Great guide.

But two the example programs listener.c and talker.c do not work as expected on my machines. These are simple programs to demonstrate datagram sockets; listener listens on a port, talker sends a packet to that port.

The code is from this section of the guide.

I've tried running the two programs on the same machine and on seperate machines, both times the listener never gets a packet and just hangs there.

I run the listener program first, then from another machine I run the talker program like this: 
	
	



```
talker 10.1.1.5 hello
```
 but the listener NEVER gets the packet. What could be going wrong?

*I've posted this in another forum and some linux people have tried it and it works fine for them. Why does it not work for me?*


----------



## mickey (Dec 21, 2009)

What's going wrong here is that the listener is listening on a udp6 socket, but the talker sends on a udp4 socket.

If you change this line in listener.c:

```
hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
```

to use AF_INET instead of AF_UNSPEC, then everything works fine.


----------



## caesius (Dec 21, 2009)

Thank you so much, this problem has been annoying me for a long time.

This brings up a question. Why does this code work fine under Linux?


----------



## SirDice (Dec 21, 2009)

caesius said:
			
		

> This brings up a question. Why does this code work fine under Linux?


Probably because IPv6 is turned off by default.


----------



## mickey (Dec 21, 2009)

I guess it would work, if your network interfaces were configured with IPv6 addresses.

As the listener does not listen on any particular address/interface and did not specify any particular protocol family (AF_UNSPEC), it is not a failure to receive a udp6 socket from getaddrinfo(), even if no interface in particular is configured with an IPv6 address.

The talker on the other hand has to use a specific interface, as it wants to send out the message on this interface. And as there probably are no interfaces configured with IPv6, it receives a udp4 socket.


----------



## SirDice (Dec 21, 2009)

It should work unless the hostname argument to talker doesn't resolve to an IPv6 address. If the listener listens on an IPv6 socket and the hostname resolves to an IPv6 address it should just work.


----------

