Skip to content
Related Articles

Related Articles

Sort an array using socket programming in C
  • Difficulty Level : Expert
  • Last Updated : 24 Jan, 2018

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.






// 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




// 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");
  
    // lsiten 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 

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :