isockad.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* Internet Socket Example using sockaddr_in.
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <netdb.h>
  19. void
  20. init_sockaddr (struct sockaddr_in *name,
  21. const char *hostname,
  22. uint16_t port)
  23. {
  24. struct hostent *hostinfo;
  25. name->sin_family = AF_INET;
  26. name->sin_port = htons (port);
  27. hostinfo = gethostbyname (hostname);
  28. if (hostinfo == NULL)
  29. {
  30. fprintf (stderr, "Unknown host %s.\n", hostname);
  31. exit (EXIT_FAILURE);
  32. }
  33. name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
  34. }