Advertisement
1fractal

Untitled

Sep 22nd, 2020
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.03 KB | None | 0 0
  1. ------ UDP sender --------
  2.  
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netinet/in.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <arpa/inet.h>
  13.  
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     int sockfd;
  18.     socklen_t addrlen;
  19.     struct sockaddr_in dest;
  20.  
  21.     // check the number of args on command line
  22.     if(argc != 4)
  23.     {
  24.         printf("USAGE: %s @dest port_num string\n", argv[0]);
  25.         exit(-1);
  26.     }
  27.  
  28.         // socket factory
  29.         if((sockfd = socket(...)) == -1)
  30.         {
  31.                 perror("socket");
  32.                 exit(EXIT_FAILURE);
  33.         }
  34.  
  35.         // init remote addr structure and other params
  36.         dest.sin_family = AF_INET;
  37.         dest.sin_port   = htons(atoi(argv[2]));
  38.         addrlen         = sizeof(struct sockaddr_in);
  39.  
  40.         // get addr from command line and convert it
  41.         if(inet_pton(...) != 1)
  42.         {
  43.                 perror("inet_pton");
  44.                 close(sockfd);
  45.                 exit(EXIT_FAILURE);
  46.         }
  47.  
  48.         // send string
  49.         if(sendto(...) == -1)
  50.         {
  51.                 perror("sendto");
  52.                 close(sockfd);
  53.                 exit(EXIT_FAILURE);
  54.         }
  55.  
  56.         // close the socket
  57.         close(sockfd);
  58.  
  59.         return 0;
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. ----- UDP receiver -------
  69. #include <sys/socket.h>
  70. #include <sys/types.h>
  71. #include <netinet/in.h>
  72. #include <sys/stat.h>
  73. #include <fcntl.h>
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76. #include <string.h>
  77. #include <unistd.h>
  78. #include <arpa/inet.h>
  79.  
  80. int main(int argc, char **argv)
  81. {
  82.         int sockfd;
  83.         char buf[1024];
  84.         socklen_t addrlen;
  85.  
  86.         struct sockaddr_in my_addr;
  87.         struct sockaddr_in client;
  88.  
  89.         // check the number of args on command line
  90.         if(argc != 2)
  91.         {
  92.                 printf("Usage: %s local_port\n", argv[0]);
  93.                 exit(-1);
  94.         }
  95.  
  96.         // socket factory
  97.         if((sockfd = socket(...)) == -1)
  98.         {
  99.                 perror("socket");
  100.                 exit(EXIT_FAILURE);
  101.         }
  102.  
  103.         // init local addr structure and other params
  104.         my_addr.sin_family      = AF_INET;
  105.         my_addr.sin_port        = htons(atoi(argv[1]));
  106.         my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  107.         addrlen                 = sizeof(struct sockaddr_in);
  108.         memset(buf,'\0',1024);
  109.  
  110.         // bind addr structure with socket
  111.         if(bind(...) == -1)
  112.         {
  113.                 perror("bind");
  114.                 close(sockfd);
  115.                 exit(EXIT_FAILURE);
  116.         }
  117.  
  118.         // reception de la chaine de caracteres
  119.         if(recvfrom(...) == -1)
  120.         {
  121.                 perror("recvfrom");
  122.                 close(sockfd);
  123.                 exit(EXIT_FAILURE);
  124.         }
  125.    
  126.     if(sendto(...) == -1)
  127.     {
  128.         perror("sendto");
  129.         close(sockfd);
  130.         exit(EXIT_FAILURE);
  131.     }
  132.  
  133.         // print the received char
  134.         printf("%s", buf);
  135.  
  136.         // close the socket
  137.         close(sockfd);
  138.  
  139.         return 0;
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. ----------- TCP Sender ---------
  150. #include <sys/socket.h>
  151. #include <sys/types.h>
  152. #include <netinet/in.h>
  153. #include <sys/stat.h>
  154. #include <fcntl.h>
  155. #include <stdlib.h>
  156. #include <stdio.h>
  157. #include <string.h>
  158. #include <unistd.h>
  159. #include <arpa/inet.h>
  160.  
  161. int main(int argc, char **argv)
  162. {
  163.         int sockfd;
  164.         struct sockaddr_in server;
  165.         socklen_t addrlen;
  166.  
  167.         // check the number of args on command line
  168.         if(argc != 4)
  169.         {
  170.                 printf("USAGE: %s @server port_num string\n", argv[0]);
  171.                 exit(-1);
  172.         }
  173.  
  174.         // socket factory
  175.         if((sockfd = socket(....)) == -1)
  176.         {
  177.                 perror("socket");
  178.                 exit(EXIT_FAILURE);
  179.         }
  180.  
  181.         // init remote addr structure and other params
  182.         server.sin_family = AF_INET;
  183.         server.sin_port   = htons(atoi(argv[2]));
  184.         addrlen           = sizeof(struct sockaddr_in);
  185.  
  186.         // get addr from command line and convert it
  187.         if(inet_pton(....) != 1)
  188.         {
  189.                 perror("inet_pton");
  190.                 close(sockfd);
  191.                 exit(EXIT_FAILURE);
  192.         }
  193.  
  194.         printf("Trying to connect to the remote host\n");
  195.         if(connect(....) == -1)
  196.         {
  197.                 perror("connect");
  198.                 exit(EXIT_FAILURE);
  199.         }
  200.  
  201.         printf("Connection OK\n");
  202.  
  203.         // send string
  204.         if(sendto(....) == -1)
  205.         {
  206.                 perror("sendto");
  207.                 close(sockfd);
  208.                 exit(EXIT_FAILURE);
  209.         }
  210.  
  211.         printf("Disconnection\n");
  212.  
  213.         // close the socket
  214.         close(sockfd);
  215.  
  216.         return 0;
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. ---------- TCP receiver ----------
  225. #include <sys/socket.h>
  226. #include <sys/types.h>
  227. #include <netinet/in.h>
  228. #include <sys/stat.h>
  229. #include <fcntl.h>
  230. #include <stdlib.h>
  231. #include <stdio.h>
  232. #include <string.h>
  233. #include <unistd.h>
  234. #include <arpa/inet.h>
  235.  
  236. int main(int argc, char **argv)
  237. {
  238.         int sockfd, sockfd2;
  239.         socklen_t addrlen;
  240.         char buf[1024];
  241.  
  242.         struct sockaddr_in my_addr;
  243.         struct sockaddr_in client;
  244.  
  245.         // check the number of args on command line
  246.         if(argc != 2)
  247.         {
  248.                 printf("USAGE: %s port_num\n", argv[0]);
  249.                 exit(-1);
  250.         }
  251.  
  252.         // socket factory
  253.         if((sockfd = socket(....)) == -1)
  254.         {
  255.                 perror("socket");
  256.                 exit(EXIT_FAILURE);
  257.         }
  258.  
  259.         // init local addr structure and other params
  260.         my_addr.sin_family      = AF_INET;
  261.         my_addr.sin_port        = htons(atoi(argv[1]));
  262.         my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  263.         addrlen                 = sizeof(struct sockaddr_in);
  264.         memset(buf,'\0',1024);
  265.  
  266.         // bind addr structure with socket
  267.         if(bind(....) == -1)
  268.         {
  269.                 perror("bind");
  270.                 close(sockfd);
  271.                 exit(EXIT_FAILURE);
  272.         }
  273.  
  274.         // set the socket in passive mode (only used for accept())
  275.         // and set the list size for pending connection
  276.         if(listen(....) == -1)
  277.         {
  278.                 perror("listen");
  279.                 close(sockfd);
  280.                 exit(EXIT_FAILURE);
  281.         }
  282.  
  283.         printf("Waiting for incomming connection\n");
  284.  
  285.         if((sockfd2 = accept(....)) == -1)
  286.         {
  287.                 perror("accept");
  288.                 close(sockfd);
  289.                 exit(EXIT_FAILURE);
  290.         }
  291.  
  292.         printf("Connection active\n");
  293.  
  294.         // reception de la chaine de caracteres
  295.         if(recv(....) == -1)
  296.         {
  297.                 perror("recv");
  298.                 close(sockfd);
  299.                 close(sockfd2);
  300.                 exit(EXIT_FAILURE);
  301.         }
  302.  
  303.         // print the received char
  304.         printf("%s", buf);
  305.  
  306.         // fermeture des sockets
  307.         close(sockfd);
  308.         close(sockfd2);
  309.  
  310.         return 0;
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement