0

why is it that when we make a client progam, we pass the ip of the host we want to connect to like this:

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

but this does not work:

their_addr.sin_addr.s_addr = inet_addr("192.168.1.3");

but when we make a server program this works:

local_addr.sin_addr.s_addr=INADDR_ANY;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ioannis
  • 1
  • 1
  • Welcome to StackOverflow. The `h_addr` example is when you use `gethostbyname()` (deprecated! use `getaddrinfo()` instead) to lookup a hostname's IP address dynamically. The `inet_addr()` example works fine as well. `sin_addr.s_addr` is declared as a 32bit `in_addr_t` or `ulong` (depending on platform), expressed in network byte order, and `inet_addr()` returns such a value, provided its input string is expressed as a valid dotted IPv4 address (which your example is). If you are having problems, please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Remy Lebeau Aug 04 '16 at 22:43

1 Answers1

0

When you create a client you want to connect to a specific server.

When you make a server you (usually) want to accept connections via all local IP addresses.

The situations are not comparable.

user207421
  • 305,947
  • 44
  • 307
  • 483