Sort an array using socket programming in C Last Updated : 28 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Given an array of unsorted positive integer, sort the given array using the Socket programming . Examples: Input : 4 5 6 1 8 2 7 9 3 0 Output :0 1 2 3 4 5 6 7 8 9 Input : 9 8 1 4 0 Output : 0 1 4 8 9 Compile these files using gcc command (gcc client.c -o client and gcc server.c -o server). Run the program using ./server and ./client (Please note : First you should run server program which will be waiting for client's response and then client code). In this program, client will take the input and send it to server and the server will sort the array using the bubble sort. C // Client code in C to sort the array #include <arpa/inet.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> // Driver code int main(int argc, char* argv[]) { int sock; struct sockaddr_in server; int server_reply[10]; int number[10] = { 5, 4, 3, 8, 9, 1, 2, 0, 6 }, i, temp; // Create socket sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { printf("Could not create socket"); } puts("Socket created"); server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = AF_INET; server.sin_port = htons(8880); // Connect to remote server if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { perror("connect failed. Error"); return 1; } puts("Connected\n"); if (send(sock, &number, 10 * sizeof(int), 0) < 0) { puts("Send failed"); return 1; } // Receive a reply from the server if (recv(sock, &server_reply, 10 * sizeof(int), 0) < 0) { puts("recv failed"); return 0; } puts("Server reply :\n"); for (i = 0; i < 10; i++) { printf("%d\n", server_reply[i]); } // close the socket close(sock); return 0; } Note : Save above file as client.c C // Server code in C to sort the array #include <arpa/inet.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> void bubble_sort(int[], int); // Driver code int main(int argc, char* argv[]) { int socket_desc, client_sock, c, read_size; struct sockaddr_in server, client; int message[10], i; // Create socket socket_desc = socket(AF_INET, SOCK_STREAM, 0); if (socket_desc == -1) { printf("Could not create socket"); } puts("Socket created"); // Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(8880); // Bind the socket if (bind(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0) { // print the error message perror("bind failed. Error"); return 1; } puts("bind done"); // listen to the socket listen(socket_desc, 3); puts("Waiting for incoming connections..."); c = sizeof(struct sockaddr_in); // accept connection from an incoming client client_sock = accept(socket_desc, (struct sockaddr*)&client, (socklen_t*)&c); if (client_sock < 0) { perror("accept failed"); return 1; } puts("Connection accepted"); // Receive a message from client while ((read_size = recv(client_sock, &message, 10 * sizeof(int), 0)) > 0) { bubble_sort(message, 10); write(client_sock, &message, 10 * sizeof(int)); } if (read_size == 0) { puts("Client disconnected"); } else if (read_size == -1) { perror("recv failed"); } return 0; } // Function to sort the array void bubble_sort(int list[], int n) { int c, d, t; for (c = 0; c < (n - 1); c++) { for (d = 0; d < n - c - 1; d++) { if (list[d] > list[d + 1]) { /* Swapping */ t = list[d]; list[d] = list[d + 1]; list[d + 1] = t; } } } } Note : Save above file as server.c Output: 0 1 2 3 4 5 6 7 8 9 Comment More infoAdvertise with us Next Article Sort an array using socket programming in C A ajay0007 Follow Improve Article Tags : Sorting Technical Scripter C Language DSA Practice Tags : Sorting Similar Reads Sorting an Array in Bash using Insertion Sort Given an array, arr[] of size N, the task is to sort the array in ascending order using Insertion Sort in bash scripting. Examples: Input: arr[] = {9, 7, 2, 5}Output: 2 5 7 9Explanation: The array in sorted order is {2, 5, 7, 9} Input: arr[] = {3, 2, 1}Output: 1 2 3Explanation: The array in sorted o 2 min read Socket Programming in C Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the serv 8 min read Program to sort an array of strings using Selection Sort Given an array of strings, sort the array using Selection Sort. Examples: Input : paper true soap floppy flower Output : floppy, flower, paper, soap, true Prerequisite : Selection Sort. C++ // C++ program to implement selection sort for // array of strings. #include <bits/stdc++.h> #include 7 min read C Program to Sort an array of names or strings Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings. C #include <stdio.h> #include <stdlib.h> #include <string. 2 min read Sorting an array in Bash using Bubble sort Prerequisite: Bubble Sort Given an array arr sort the array in ascending order using bash scripting. Examples: Input : 9 7 2 5 Output : Array in sorted order : 2 5 7 9 Approach : For sorting the array bubble sort is the simplest technique. Bubble sort works by swapping the adjacent elements if they 2 min read Like