zero_shubham1

weirdo.c

Oct 14th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 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 unsorted[100],sorted[100];
  25.     //char *hello;
  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.     strcat(unsorted, buffer);
  59.  
  60.     int i=0,j,count=0, numc;
  61.     char num[10];
  62.     int arr[100];
  63.  
  64.     while(unsorted[i]!='\0'){
  65.         j = i;
  66.         numc=0;
  67.         while(unsorted[j]!= ' '){
  68.             num[numc++] = unsorted[j];
  69.             j++;
  70.         }
  71.         if(numc!=0){
  72.             num[numc] = '\0';
  73.             arr[count++] = atoi(num);
  74.         }
  75.        
  76.  
  77.         i++;
  78.     }
  79.  
  80.     memset(num, '\0', sizeof(num));
  81.     memset(sorted, '\0', sizeof(sorted));
  82.  
  83.     while(i<count){
  84.         sprintf(num, "%d ", arr[i++]);
  85.         strcat(sorted, num);
  86.     }
  87.  
  88.     //strcat(sorted,buffer);
  89.     i=0;
  90.  
  91.     while(sorted[i]!='\0'){
  92.         printf("%d:%c\n",i, sorted[i++] );
  93.     }
  94.  
  95.  
  96.     sendto(sockfd, (const char *)sorted, strlen(sorted),  
  97.         MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
  98.             len);
  99.     printf("sorted message sent.\n");  
  100.  
  101.      
  102.     return 0;
  103. }
Add Comment
Please, Sign In to add comment