Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /* compile using cc -o DIDSendSocket DIDSendSocket.c
  2.  
  3. /* sending an internet domain datagram */
  4. /* address on command line please */
  5.  
  6.  
  7. #include </sys/sys/types.h>
  8. #include </sys/sys/socket.h>
  9. #include </sys/netinet/in.h>
  10. #include <netdb.h>
  11. #include <stdio.h>
  12.  
  13.  
  14. #define DATA "The sea oh the sea, I am an Internet datagram..."
  15.  
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20. int sock;
  21. struct sockaddr_in name;
  22. struct hostent *hp, *gethostbyname();
  23.  
  24. sock=socket(AF_INET, SOCK_DGRAM, 0);
  25. if(sock<0)
  26. {
  27. perror("opening datagram socket");
  28. exit(1);
  29. }
  30.  
  31. /* construct name, of socket to send to, gethostbyname() returns struct including network address of host, port# taken from command line */
  32.  
  33. hp=gethostbyname(argv[1]);
  34. if (hp==0)
  35. {
  36. fprintf(stderr, "%s: unknown host", argv[1]);
  37. exit(2);
  38. }
  39. bcopy((char *)hp->h_addr, (char *)&name.sin_addr, hp->h_length);
  40. name.sin_family=AF_INET;
  41. name.sin_port=htons(atoi(argv[2]));
  42.  
  43. /* send message */
  44.  
  45. if(sendto(sock, DATA, sizeof DATA, 0, (struct sockaddr *)&name, sizeof name)<0)
  46. perror("sending datagram message");
  47. close(sock);
  48. exit(0);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement