0% found this document useful (0 votes)
6 views

Lab8 (DC2021BTE0133)

Uploaded by

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

Lab8 (DC2021BTE0133)

Uploaded by

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

Q1.

Server
import java.io.*;
import java.net.*;
import java.util.ArrayList; import java.util.Collections;
public class server{
public static void main(String[] args) throws IOException{
ServerSocket sc = new ServerSocket(8000);
System.out.println("Name: Arijt Roy");
System.out.println("ID: DC2021BTE0133");
System.out.println("Wait until connection is established!");
Socket s = sc.accept();
System.out.println("Connection done at port" +sc.getLocalPort());
DataInputStream dis = new DataInputStream(s.getInputStream());
ArrayList<Integer> arr = new ArrayList<>(); int num;
while((num = dis.readInt()) != -1){
arr.add(num);
}
System.out.println("Unsorted number: ");
for(int nums: arr){
System.out.print(nums +" ");
}
System.out.println(); Collections.sort(arr);
System.out.println("Sorted number: "); for(int nums: arr){
System.out.print(nums +" "); }
System.out.println(); sc.close();
s.close();
dis.close();
}
}
Client
import java.io.*;
import java.net.*; import java.util.Scanner;
public class client {
public static void main(String[] args) throws IOException{
Socket sc = new Socket("localhost",8000);
DataOutputStream dos = new DataOutputStream(sc.getOutputStream());
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the array: "); int n = input.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: "); for(int i=0; i<n; i++){
arr[i] = input.nextInt(); }
for(int number: arr){ dos.writeInt(number);
} dos.writeInt(-1);
sc.close();
dos.close();
input.close();
}
}
Output

Q2.
Server
import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class server {


public static void main(String[] args) throws IOException {
ServerSocket sc = new ServerSocket(8000);
System.out.println("Name: Arijt Roy");
System.out.println("ID: DC2021BTE0133");
System.out.println("Wait until connection is established!");
Socket s = sc.accept();
System.out.println("Connection done at port" + sc.getLocalPort());
DataInputStream dis = new DataInputStream(s.getInputStream());
ArrayList<Integer> arr = new ArrayList<>();
int num, sum = 0;
while ((num = dis.readInt()) != -1) {
arr.add(num);
}
for (int nums : arr) {
sum += nums;
}
System.out.println("Sum of the number recieved by the client: " + sum);
sc.close();
s.close();
dis.close();
}
}
Client
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class client {


public static void main(String[] args) throws IOException {
Socket sc = new Socket("localhost", 8000);
DataOutputStream dos = new DataOutputStream(sc.getOutputStream());
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n = input.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: ");

for (int i = 0; i < n; i++) {


arr[i] = input.nextInt();
}

for (int number : arr) {


dos.writeInt(number);
}
dos.writeInt(-1);

sc.close();
dos.close();
input.close();
}
}
Output

You might also like