Practical - 4: Aim: Servers Can Be Designed To Limit The Number of Open Connections. For Example

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Name : Romil.P.Vora Roll.

no:90

Subject : Advanced operating systems Prof:Diksha Wakode

Practical -4
Aim : Servers can be designed to limit the number of open connections. For example,
a server may wish to have only N socket connections open at any point in time. After
N connections have been made, the server will not accept another incoming
connection until an existing connection is released. Write Java programs to
demonstrate the scenario

Code :

Client4.java

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

class client4 {

public static void main(String args[]) {


try {

Socket s = new Socket("localhost", 4040);


//socket input
InputStream in = s.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
//socket output
OutputStream out = s.getOutputStream();
PrintWriter pw = new PrintWriter(out, true);

//input

InputStreamReader isr1 = new InputStreamReader(System.in);


BufferedReader br1 = new BufferedReader(isr1);

while (true) {
System.out.println("CAndroid/CIOS/CQuit");
String line = br1.readLine();
if(line.equalsIgnoreCase("CAndroid")){
System.out.println("Romil");
} else if(line.equalsIgnoreCase("CIOS")){
System.out.println("vora");
} else if(line.equalsIgnoreCase("CQuit")){
System.out.println("Let's Go Home");
System.exit(0);
}

}
} catch (Exception e) {
System.out.println(e);
}
}

Server4.java

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

class server4 {

public static void main(String[] args) {


try {
ServerSocket ss = new ServerSocket(4040, 50);
Socket[] client = new Socket[10];

for (int i = 0; i < client.length; i++) {


client[i] = new Socket("localhost", 4040);
client[i] = ss.accept();
System.out.printf("Client %2d: " + client[i] + "%n", i);

InputStream in = client[i].getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
//socket output
OutputStream out = client[i].getOutputStream();
PrintWriter pw = new PrintWriter(out, true);

//input
InputStreamReader isr1 = new InputStreamReader(System.in);
BufferedReader br1 = new BufferedReader(isr1);

while (true) {
System.out.println("SAndroid/SIOS/SQuit");
String line = br1.readLine();

if(line.equalsIgnoreCase("SAndroid")){
System.out.println("Romil");
} else if(line.equalsIgnoreCase("SIOS")){
System.out.println("vora");
} else if(line.equalsIgnoreCase("SQuit")){
System.out.println("Let's Go Home");
client[i].close();
System.exit(0);
}

/* switch (line.charAt(0)) {

case 'r':
case 'R':
System.out.println("here");
String input = br.readLine();
System.out.println("ROutput : " + input);
break;

case 's':
case 'S':
String output = br1.readLine();
pw.println(output);
pw.flush();
System.out.println("SOutput : " + output);
break;

case 'q':
case 'Q':
System.out.println("Fin");
client[i].close();
System.exit(0);
}*/
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Output :

You might also like