Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Server side implementation of UDP client-server model
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #define PORT 8080
- #define MAXLINE 1024
- // Driver code
- int main() {
- int sockfd;
- char buffer[MAXLINE];
- char sorted[100];
- char *hello = "bhak bc!";
- struct sockaddr_in servaddr, cliaddr;
- // Creating socket file descriptor
- if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
- perror("socket creation failed");
- exit(EXIT_FAILURE);
- }
- memset(&servaddr, 0, sizeof(servaddr));
- memset(&cliaddr, 0, sizeof(cliaddr));
- // Filling server information
- servaddr.sin_family = AF_INET; // IPv4
- servaddr.sin_addr.s_addr = INADDR_ANY;
- servaddr.sin_port = htons(PORT);
- // Bind the socket with the server address
- if ( bind(sockfd, (const struct sockaddr *)&servaddr,
- sizeof(servaddr)) < 0 )
- {
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- int len, n;
- n = recvfrom(sockfd, (char *)buffer, MAXLINE,
- MSG_WAITALL, ( struct sockaddr *) &cliaddr,
- &len);
- buffer[n] = '\0';
- printf("Client : %s\n", buffer);
- /* sleep(100);
- //test
- sendto(sockfd, (const char *)hello, strlen(hello),
- MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
- len); */
- int i=0,j,count=0, numc=0;
- char num[10];
- int arr[100];
- while(buffer[i]!='\0'){
- j = i;
- numc=0;
- while(buffer[j]!= ' '){
- num[numc++] = buffer[j];
- j++;
- }
- if(numc!=0){
- num[numc+1] = '\0';
- arr[count++] = atoi(num);
- }
- i++;
- }
- i = 0;
- int tmp;
- while(i<count-1){
- j = i+1;
- int small=i;
- while(j<count-1){
- if(arr[j]<arr[small])
- small = j;
- j++;
- }
- tmp = arr[small];
- arr[small] = arr[i];
- arr[i] = tmp;
- i++;
- }
- i=0;
- memset(num, '\0', sizeof(num));
- memset(sorted, '\0', sizeof(sorted));
- while(i<count-1){
- sprintf(num, "%d ", arr[i++]);
- strcat(sorted, num);
- }
- //strcat(sorted, "\0");
- printf("Sorted:%s\n", sorted);
- //printf("Client : %s\n", buffer);
- sendto(sockfd, (const char *)hello, strlen(hello),
- MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
- len);
- printf("sorted message sent.\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment