zero_shubham1

UDPServer.c

Oct 14th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1.  
  2. // Server side implementation of UDP client-server model
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10. #include <netinet/in.h>
  11.  
  12.  
  13. #include <unistd.h>
  14.  
  15.  
  16.  
  17. #define PORT     8080
  18. #define MAXLINE 1024
  19.  
  20. // Driver code
  21. int main() {
  22.     int sockfd;
  23.     char buffer[MAXLINE];
  24.     char sorted[100];
  25.     char *hello = "bhak bc!";
  26.     struct sockaddr_in servaddr, cliaddr;
  27.      
  28.     // Creating socket file descriptor
  29.     if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  30.         perror("socket creation failed");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.      
  34.     memset(&servaddr, 0, sizeof(servaddr));
  35.     memset(&cliaddr, 0, sizeof(cliaddr));
  36.      
  37.     // Filling server information
  38.     servaddr.sin_family    = AF_INET; // IPv4
  39.     servaddr.sin_addr.s_addr = INADDR_ANY;
  40.     servaddr.sin_port = htons(PORT);
  41.      
  42.     // Bind the socket with the server address
  43.     if ( bind(sockfd, (const struct sockaddr *)&servaddr,  
  44.             sizeof(servaddr)) < 0 )
  45.     {
  46.         perror("bind failed");
  47.         exit(EXIT_FAILURE);
  48.     }
  49.      
  50.     int len, n;
  51.     n = recvfrom(sockfd, (char *)buffer, MAXLINE,  
  52.                 MSG_WAITALL, ( struct sockaddr *) &cliaddr,
  53.                 &len);
  54.     buffer[n] = '\0';
  55.  
  56.     printf("Client : %s\n", buffer);
  57.  
  58.  
  59.    /* sleep(100);
  60.     //test
  61.     sendto(sockfd, (const char *)hello, strlen(hello),  
  62.         MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
  63.             len); */
  64.  
  65.  
  66.  
  67.  
  68.     int i=0,j,count=0, numc=0;
  69.     char num[10];
  70.     int arr[100];
  71.  
  72.     while(buffer[i]!='\0'){
  73.         j = i;
  74.         numc=0;
  75.         while(buffer[j]!= ' '){
  76.             num[numc++] = buffer[j];
  77.             j++;
  78.         }
  79.         if(numc!=0){
  80.             num[numc+1] = '\0';
  81.             arr[count++] = atoi(num);
  82.         }
  83.        
  84.  
  85.         i++;
  86.     }
  87.    
  88.     i = 0;
  89.     int tmp;
  90.     while(i<count-1){
  91.         j = i+1;
  92.         int small=i;
  93.         while(j<count-1){
  94.             if(arr[j]<arr[small])
  95.                 small = j;
  96.  
  97.             j++;
  98.         }
  99.  
  100.         tmp = arr[small];
  101.         arr[small] = arr[i];
  102.         arr[i] = tmp;
  103.  
  104.         i++;
  105.     }
  106.  
  107.     i=0;
  108.  
  109.     memset(num, '\0', sizeof(num));
  110.     memset(sorted, '\0', sizeof(sorted));
  111.  
  112.     while(i<count-1){
  113.         sprintf(num, "%d ", arr[i++]);
  114.         strcat(sorted, num);
  115.     }
  116.     //strcat(sorted, "\0");
  117.  
  118.     printf("Sorted:%s\n", sorted);
  119.     //printf("Client : %s\n", buffer);
  120.  
  121.  
  122.  
  123.     sendto(sockfd, (const char *)hello, strlen(hello),  
  124.         MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
  125.             len);
  126.     printf("sorted message sent.\n");  
  127.  
  128.      
  129.     return 0;
  130. }
Add Comment
Please, Sign In to add comment