0% found this document useful (0 votes)
16 views3 pages

Array Sorting

The document contains Java code for a client-server application that sorts an array of integers. The server receives an array from the client, sorts it using the Arrays.sort() method, and sends the sorted array back to the client. The client connects to the server, sends the array, and then receives and displays the sorted data.

Uploaded by

Shitiz Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Array Sorting

The document contains Java code for a client-server application that sorts an array of integers. The server receives an array from the client, sorts it using the Arrays.sort() method, and sends the sorted array back to the client. The client connects to the server, sends the array, and then receives and displays the sorted data.

Uploaded by

Shitiz Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM

To implement sorting of data using a remote client


// SERVER SIDE CODE

import java.io.*;
import java.net.*;
import java.util.*;

class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(7777);
Socket s = ss.accept();
System.out.println("connected...");
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
int r, i = 0;
int n = din.readInt();
int a[] = new int[n];
System.out.println("data");
int count = 0;
System.out.println("Receiving Data...");
for(i=0;i<n;i++)
{
a[i] = din.readInt();
}
System.out.println("Data Received");
System.out.println("Sorting Data...");
Arrays.sort(a);
for(i=0; i<n; i++)
{
dout.writeInt(a[i]);
}
System.out.println("\nData Sent Successfully");
s.close();
ss.close();
}
}
// CLIENT SIDE CODE

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client


{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost", 7777);
if(s.isConnected())
{
System.out.println("Connected to Server");
}
System.out.println("Enter size of array:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a[] = new int[n];
System.out.println("Enter element to array:");
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeInt(n);
for(int i = 0; i<n; i++)
{
a[i] = scanner.nextInt();
dout.writeInt(a[i]);
}
System.out.println("Data Sent");
DataInputStream din = new DataInputStream(s.getInputStream());
int r;
System.out.println("Receiving Sorted Data...");
for(int i=0; i<n; i++)
{
a[i] = din.readInt();
System.out.print(" " + a[i] + " ");
}
s.close();
}
}

You might also like