Write an algorithm each for TCP client and TCP server based on the following specifications: a. TCP client will send a list of 10 numbers to the TCP server. b. TCP server can handle maximum 4 client at a time. TCP server will find the smallest number from the given list and return to the respective client.
//Client
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client {
private Socket socket = null;
private ObjectInputStream input = null;
private ObjectOutputStream out = null;
public Client(String address, int port) {
try {
Data data = new Data();
data.input = new int[10];
Scanner scanner = new Scanner(System.in);
socket = new Socket(address, port);
System.out.println("Connected to the server, enter 10 numbers:");
for (int i = 0; i < 10; i++) {
data.input[i] = scanner.nextInt();
}
out = new ObjectOutputStream(socket.getOutputStream());
input = new ObjectInputStream(socket.getInputStream());
out.writeObject(data);
System.out.println("Minimum value received from server: " + (Integer)input.readObject());
input.close();
out.close();
socket.close();
} catch (UnknownHostException u) {
System.out.println(u);
} catch (IOException i) {
System.out.println(i);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Client client = new Client("127.0.0.1", 1234);
}
}
//Server
import java.net.*;
import java.io.*;
public class Server {
private Socket socket = null;
private ServerSocket server = null;
static int NumberOfClients = 0;
final static int MaximumClients = 4;
public Server(int port) {
try {
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for clients");
while(true) {
socket = server.accept();
if(NumberOfClients < MaximumClients) {
new ClientManager(socket).start();
System.out.println((NumberOfClients+1) + " Clients connected");
NumberOfClients++;
} else {
System.out.println("Maximum clients connected");
socket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Server server = new Server(1234);
}
}
//ClientManager to manage single client
import java.io.*;
import java.net.Socket;
public class ClientManager extends Thread {
private Socket socket = null;
private ObjectInputStream in = null;
private ObjectOutputStream out = null;
public ClientManager(Socket socket) {
this.socket = socket;
}
public void run() {
try {
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
Data data = (Data)in.readObject();
int min = Integer.MAX_VALUE;
for(int i = 0; i < 10; i++) {
if(min > data.input[i]) {
min = data.input[i];
}
}
out.writeObject(min);
out.close();
in.close();
socket.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Server.NumberOfClients--;
e.printStackTrace();
}
}
}
//Data that is sent by the client to server
import java.io.Serializable;
public class Data implements Serializable {
public int input[];
}
Get Answers For Free
Most questions answered within 1 hours.