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 unsorted[100],sorted[100];
- //char *hello;
- 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);
- strcat(unsorted, buffer);
- int i=0,j,count=0, numc;
- char num[10];
- int arr[100];
- while(unsorted[i]!='\0'){
- j = i;
- numc=0;
- while(unsorted[j]!= ' '){
- num[numc++] = unsorted[j];
- j++;
- }
- if(numc!=0){
- num[numc] = '\0';
- arr[count++] = atoi(num);
- }
- i++;
- }
- memset(num, '\0', sizeof(num));
- memset(sorted, '\0', sizeof(sorted));
- while(i<count){
- sprintf(num, "%d ", arr[i++]);
- strcat(sorted, num);
- }
- //strcat(sorted,buffer);
- i=0;
- while(sorted[i]!='\0'){
- printf("%d:%c\n",i, sorted[i++] );
- }
- sendto(sockfd, (const char *)sorted, strlen(sorted),
- MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
- len);
- printf("sorted message sent.\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment