Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. /* SERVER */
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11.  
  12. #define BUF_SIZE 1024
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     struct addrinfo hints;
  17.     struct addrinfo *result, *rp;
  18.     int sfd, s;
  19.     struct sockaddr_storage peer_addr;
  20.     socklen_t peer_addr_len;
  21.     ssize_t nread;
  22.     char buf[BUF_SIZE];
  23.     char input[BUF_SIZE];
  24.     int fd;
  25.     int len;
  26.     if (argc != 2) {
  27.         fprintf(stderr, "Usage: %s port\n", argv[0]);
  28.         exit(EXIT_FAILURE);
  29.     }
  30.  
  31.     memset(&hints, 0, sizeof(struct addrinfo));
  32.     hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  33.     hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
  34.     hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
  35.     hints.ai_protocol = 0; /* Any protocol */
  36.     hints.ai_canonname = NULL;
  37.     hints.ai_addr = NULL;
  38.     hints.ai_next = NULL;
  39.  
  40.     s = getaddrinfo(NULL, argv[1], &hints, &result);
  41.     if (s != 0) {
  42.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  43.         exit(EXIT_FAILURE);
  44.     }
  45.  
  46.     /* getaddrinfo() returns a list of address structures.
  47.               Try each address until we successfully bind(2).
  48.               If socket(2) (or bind(2)) fails, we (close the socket
  49.               and) try the next address. */
  50.  
  51.     for (rp = result; rp != NULL; rp = rp->ai_next) {
  52.         sfd = socket(rp->ai_family, rp->ai_socktype,
  53.             rp->ai_protocol);
  54.         if (sfd == -1)
  55.             continue;
  56.  
  57.         if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
  58.             break; /* Success */
  59.  
  60.         close(sfd);
  61.     }
  62.  
  63.     if (rp == NULL) { /* No address succeeded */
  64.         fprintf(stderr, "Could not bind\n");
  65.         exit(EXIT_FAILURE);
  66.     }
  67.  
  68.     freeaddrinfo(result); /* No longer needed */
  69.  
  70.     /* Read datagrams and echo them back to sender */
  71.         peer_addr_len = sizeof(struct sockaddr_storage);
  72.  
  73.     for (;;) {
  74.         nread = recvfrom(sfd, buf, BUF_SIZE, 0,
  75.             (struct sockaddr*)&peer_addr, &peer_addr_len);
  76.         if (nread == -1)
  77.             continue; /* Ignore failed request */
  78.  
  79.         if((fd = open(buf,O_RDONLY)) == -1)
  80.         {
  81.             perror("wrong path");
  82.             continue;
  83.         }
  84.  
  85.         while ((len = read(fd,input,BUF_SIZE-1)) > 0)
  86.         {
  87.             if(write(STDOUT_FILENO,input,len) == -1)
  88.                 perror("error");
  89.             if (sendto(sfd, input, len, 0,
  90.                 (struct sockaddr*)&peer_addr,
  91.                 peer_addr_len)
  92.             != len)
  93.             fprintf(stderr, "Error sending response\n");
  94.         }
  95.         printf("came here\n");
  96.         close(fd);
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. /* CLIENT */
  103.  
  104. #include <sys/types.h>
  105. #include <sys/socket.h>
  106. #include <netdb.h>
  107. #include <stdio.h>
  108. #include <stdlib.h>
  109. #include <unistd.h>
  110. #include <string.h>
  111. #include <sys/stat.h>
  112. #include <fcntl.h>
  113.  
  114. #define BUF_SIZE 1024
  115.  
  116. int main(int argc, char* argv[])
  117. {
  118.     struct addrinfo hints;
  119.     struct addrinfo *result, *rp;
  120.     int sfd, s, j;
  121.     size_t len;
  122.     ssize_t nread;
  123.     char buf[BUF_SIZE];
  124.     char input[BUF_SIZE];
  125.     if (argc < 2) {
  126.         fprintf(stderr, "Usage: %s host port...\n", argv[0]);
  127.         exit(EXIT_FAILURE);
  128.     }
  129.  
  130.     /* Obtain address(es) matching host/port */
  131.  
  132.     memset(&hints, 0, sizeof(struct addrinfo));
  133.     hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
  134.     hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
  135.     hints.ai_flags = 0;
  136.     hints.ai_protocol = 0; /* Any protocol */
  137.  
  138.     s = getaddrinfo(argv[1], argv[2], &hints, &result);
  139.     if (s != 0) {
  140.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  141.         exit(EXIT_FAILURE);
  142.     }
  143.  
  144.     /* getaddrinfo() returns a list of address structures.
  145.               Try each address until we successfully connect(2).
  146.               If socket(2) (or connect(2)) fails, we (close the socket
  147.               and) try the next address. */
  148.  
  149.     for (rp = result; rp != NULL; rp = rp->ai_next) {
  150.         sfd = socket(rp->ai_family, rp->ai_socktype,
  151.             rp->ai_protocol);
  152.         if (sfd == -1)
  153.             continue;
  154.  
  155.         if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
  156.             break; /* Success */
  157.  
  158.         close(sfd);
  159.     }
  160.  
  161.     if (rp == NULL) { /* No address succeeded */
  162.         fprintf(stderr, "Could not connect\n");
  163.         exit(EXIT_FAILURE);
  164.     }
  165.  
  166.     freeaddrinfo(result); /* No longer needed */
  167.  
  168.     /* Send remaining command-line arguments as separate
  169.               datagrams, and read responses from server */
  170.     int n;
  171.     while(scanf("%s",input) != EOF)
  172.     {
  173.         write(sfd,input,BUF_SIZE-1);
  174.         while((n=read(sfd,input,BUF_SIZE-1)) > 0)
  175.         {
  176.             if(write(STDOUT_FILENO,input,n) == -1)
  177.                 perror("error");
  178.         }
  179.  
  180.  
  181.     }
  182.  
  183. /**
  184.     for (j = 3; j < argc; j++) {
  185.         len = strlen(argv[j]) + 1;
  186.         /* +1 for terminating null byte
  187.  
  188.         if (len > BUF_SIZE) {
  189.             fprintf(stderr,
  190.                 "Ignoring long message in argument %d\n", j);
  191.             continue;
  192.         }
  193.  
  194.         if (write(sfd, argv[j], len) != len) {
  195.             fprintf(stderr, "partial/failed write\n");
  196.             exit(EXIT_FAILURE);
  197.         }
  198.  
  199.         nread = read(sfd, buf, BUF_SIZE);
  200.         if (nread == -1) {
  201.             perror("read");
  202.             exit(EXIT_FAILURE);
  203.         }
  204.  
  205.         printf("Received %zd bytes: %s\n", nread, buf);
  206.     }
  207.     **/
  208.  
  209.     exit(EXIT_SUCCESS);
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement