zero_shubham1

UDPServer-final.c

Oct 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 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. int * extract(char arr[]){
  21.     int *test = malloc(10* sizeof(int));
  22.    
  23.     int i=0,j,count=0, numc;
  24.     char num[10];
  25.  
  26.  
  27.      while(arr[i]!='\0'){
  28.         j = i;
  29.         numc=0;
  30.         while(arr[j]!= ' '){
  31.             num[numc++] = arr[j];
  32.             j++;
  33.         }
  34.         if(numc!=0){
  35.             num[numc] = '\0';
  36.             test[count++] = atoi(num);
  37.             i=j;
  38.         }
  39.  
  40.  
  41.         i++;
  42.     }
  43.  
  44.  
  45.     return test;
  46. }
  47.  
  48. void selectionSort(int arr[])
  49. {
  50.     int i, j, min_idx, tmp;
  51.  
  52.     // One by one move boundary of unsorted subarray
  53.     for (i = 0; i < 10-1; i++)
  54.     {
  55.         // Find the minimum element in unsorted array
  56.         min_idx = i;
  57.         for (j = i+1; j < 10; j++)
  58.           if (arr[j] < arr[min_idx])
  59.             min_idx = j;
  60.  
  61.         // Swap the found minimum element with the first element
  62.         tmp = arr[min_idx];
  63.         arr[min_idx] = arr[i];
  64.         arr[i] = tmp;
  65.     }
  66. }
  67.  
  68.  
  69. // Driver code
  70. int main() {
  71.     int sockfd;
  72.     char buffer[MAXLINE];
  73.     char unsorted[100];
  74.     struct sockaddr_in servaddr, cliaddr;
  75.  
  76.     // Creating socket file descriptor
  77.     if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  78.         perror("socket creation failed");
  79.         exit(EXIT_FAILURE);
  80.     }
  81.  
  82.     memset(&servaddr, 0, sizeof(servaddr));
  83.     memset(&cliaddr, 0, sizeof(cliaddr));
  84.  
  85.     // Filling server information
  86.     servaddr.sin_family    = AF_INET; // IPv4
  87.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  88.     servaddr.sin_port = htons(PORT);
  89.  
  90.     // Bind the socket with the server address
  91.     if ( bind(sockfd, (const struct sockaddr *)&servaddr,
  92.             sizeof(servaddr)) < 0 )
  93.     {
  94.         perror("bind failed");
  95.         exit(EXIT_FAILURE);
  96.     }
  97.  
  98.     int len, n;
  99.     n = recvfrom(sockfd, (char *)buffer, MAXLINE,
  100.                 MSG_WAITALL, ( struct sockaddr *) &cliaddr,
  101.                 &len);
  102.     buffer[n] = '\0';
  103.  
  104.     printf("Client : %s\n", buffer);
  105.  
  106.     memset(&unsorted, '\0', sizeof(unsorted));
  107.  
  108.     strcat(unsorted,buffer);
  109.  
  110.     int *c;
  111.     c = extract(unsorted);
  112.  
  113.     selectionSort(c);
  114.     char num[10], sorted[100];
  115.  
  116.     for (int i = 0; i < 10; i++)
  117.     {
  118.         sprintf(num, "%d ", c[i]);
  119.         strcat(sorted, num);
  120.     }
  121.     sendto(sockfd, (const char *)sorted, strlen(sorted),
  122.         MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
  123.             len);
  124.     printf("sorted message sent.\n");
  125.  
  126.  
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment