Practical : 3
Subject : Advanced Java
Aim : Implement any one sorting algorithm using TCP/UDP
on Server application and Give Input On Client side and client should sorted
output from server and display sorted on input side.
Server Code:
--------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
public
static void main(String args[]) throws Exception
{
ServerSocketss=new
ServerSocket(7777);
Socket
s=ss.accept();
System.out.println("connected..........");
DataInputStream
din=new DataInputStream(s.getInputStream());
DataOutputStreamdout=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);
System.out.println("Data
Sorted");
System.out.println("Sending
Data........");
for(i=0;i<n;i++)
{
dout.writeInt(a[i]);
}
System.out.println("\nData
Sent Successfully");
s.close();
ss.close();
}
}
--------------------------------------------------------------------------------
Client 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("127.0.0.1",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:");
DataOutputStreamdout=new
DataOutputStream(s.getOutputStream());
dout.writeInt(n);
for(int
i=0;i<n;i++)
{
int
r=scanner.nextInt();;
dout.writeInt(r);
}
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++)
{
r=din.readInt();
System.out.print(r+"
");
}
s.close();
}
}
--------------------------------------------------------------------------------
Output:
Server
Client
Sign up here with your email
3 comments
Write commentsI really appreciate the information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in WINDCHILL PLM, kindly Contact MaxMunus
ReplyMaxMunus Offer World Class Virtual Instructor-led training on WINDCHILL PLM. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 1,00,000 + training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain, and UAE etc.
Avishek Priyadarshi
MaxMunus
E-mail: avishek@maxmunus.com
Skype id: avishek_2.
Ph:(0) 8553177744 / 080 - 41103383
www.MaxMunus.com
very helpfull man thankyou for uploading practical if possible can you please upload advance java 7 no gtu practical
ReplyBhau tum bahut achha kaam karte ho.....
ReplyConversionConversion EmoticonEmoticon