Java Program to Implement sorting algorithm using TCP on Server application

Simple Java program to Implement sorting algorithm using TCP on Server application

Input: Give input on client side and client should receive sorted output from server and display sorted list on input(client) side.
Note: You can implement any sorting algorithm by changing the sorting algorithm code on server side

Code:

Server:

package aj.pkg7;
import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(1111);
Socket s=ss.accept();
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;
for(i=0;i<n;i++)
{
a[i]=din.readInt();
}
int number = a.length;
for (int i1 = 0; i1 < number-1; i1++)
for (int j1 = 0; j1 < number-i1-1; j1++)
if (a[j1] > a[j1+1])
{
int temp = a[j1];
a[j1] = a[j1+1];
a[j1+1] = temp;
}
for(i=0;i<n;i++)
{
dout.writeInt(a[i]);
}
s.close();
ss.close();
}
}

Client:

package aj.pkg7;
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",1111);
System.out.println("Enter the size of array:");
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int a[]=new int[n];
System.out.println("Enter elements of array:");
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeInt(n);
for(int i=0;i<n;i++)
{
int r=scanner.nextInt();;
dout.writeInt(r);
}
DataInputStream din=new DataInputStream(s.getInputStream());
int r;
System.out.println("Sorted Array:");
for(int i=0;i<n;i++)
{
r=din.readInt();
System.out.print(r+" ");
}
s.close();
}
}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

C++ program to perform data transformation Min-max and Z score Normalization