Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- char buf[512];
- int main(int argc, char **argv)
- {
- struct in_addr adr2;
- int i;
- struct hostent *he;
- struct sockaddr_in adr;
- int gniazdo, r;
- unsigned int port;
- char abcd[512];
- printf("Podaj nazwe hosta: ");
- scanf("%s", abcd);
- he = gethostbyname((const char*) abcd);
- if (he == NULL) {
- printf("Nie ma hosta o takiej nazwie\n");
- printf("lub blad serwera DNS.\n");
- return 1;
- }
- i=0;
- while (he->h_addr_list[i]) {
- adr2.s_addr =
- *((unsigned long*) he->h_addr_list[i]);
- printf("Adres hosta: %s\n",
- inet_ntoa(adr2));
- i++;
- }
- printf("Podaj numer portu odbiorcy: ");
- scanf("%u", &port);
- gniazdo = socket(AF_INET, SOCK_DGRAM, 0);
- adr.sin_family = AF_INET;
- adr.sin_port = htons(port);
- adr.sin_addr.s_addr = inet_addr(inet_ntoa(adr2));
- while(1){
- printf("Podaj wiadomosc: ");
- fflush(stdout);
- fgetc(stdin);
- fgets(buf, 512, stdin);
- r = sendto(gniazdo,
- buf,
- 512,
- 0,
- (struct sockaddr*) &adr,
- sizeof(adr));
- if (r != 512) printf("sendto() nie powiodl sie\n");
- else printf("Wiadomosc wyslana.\n");
- }
- close(gniazdo);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement