Advertisement
Guest User

ELOOOEE

a guest
Oct 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <netinet/in.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <netdb.h>
  8. #include <unistd.h>
  9.  
  10.  
  11. char buf[512];
  12.  
  13. int main(int argc, char **argv)
  14. {
  15. struct in_addr adr2;
  16. int i;
  17. struct hostent *he;
  18.  
  19. struct sockaddr_in adr;
  20. int gniazdo, r;
  21. unsigned int port;
  22. char abcd[512];
  23.  
  24. printf("Podaj nazwe hosta: ");
  25. scanf("%s", abcd);
  26. he = gethostbyname((const char*) abcd);
  27. if (he == NULL) {
  28. printf("Nie ma hosta o takiej nazwie\n");
  29. printf("lub blad serwera DNS.\n");
  30. return 1;
  31. }
  32. i=0;
  33. while (he->h_addr_list[i]) {
  34. adr2.s_addr =
  35. *((unsigned long*) he->h_addr_list[i]);
  36. printf("Adres hosta: %s\n",
  37. inet_ntoa(adr2));
  38. i++;
  39. }
  40.  
  41.  
  42.  
  43. printf("Podaj numer portu odbiorcy: ");
  44. scanf("%u", &port);
  45. gniazdo = socket(AF_INET, SOCK_DGRAM, 0);
  46. adr.sin_family = AF_INET;
  47. adr.sin_port = htons(port);
  48. adr.sin_addr.s_addr = inet_addr(inet_ntoa(adr2));
  49.  
  50.  
  51. while(1){
  52. printf("Podaj wiadomosc: ");
  53. fflush(stdout);
  54. fgetc(stdin);
  55. fgets(buf, 512, stdin);
  56. r = sendto(gniazdo,
  57. buf,
  58. 512,
  59. 0,
  60. (struct sockaddr*) &adr,
  61. sizeof(adr));
  62. if (r != 512) printf("sendto() nie powiodl sie\n");
  63. else printf("Wiadomosc wyslana.\n");
  64. }
  65.  
  66. close(gniazdo);
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement