import java.io.IOException; import java.io.InputStream; import java.io.OutputStr
ID: 666687 • Letter: I
Question
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(7000);
System.out.println("The ITEC 6120/8120 Echo Server is now ready!");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Accepted an echo request");
System.out.println("... local socket address " + socket.getLocalSocketAddress());
System.out.println("... remote socket address " + socket.getRemoteSocketAddress());
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
while (true) {
int b = input.read();
if (b == -1) break;
output.write(b);
}
socket.close();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class EchoClient {
public static void main(String[] args) throws IOException {
int b;
Socket socket = new Socket(args[0], 7000);
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
System.out.println("The socket is connected the server.");
System.out.println("... local socket address is " + socket.getLocalSocketAddress());
System.out.println("... remote socket address is " + socket.getRemoteSocketAddress());
output.write(args[1].getBytes());
socket.shutdownOutput();
while (true) {
b = input.read();
if (b == -1) {
break;
}
System.out.print((char) b);
}
System.out.println();
socket.close();
}
}
Server programs are rarely implemented as non-threaded applications, even though that is how you implemented your previous server application. Servers would not be able to provide the necessary throughput unless they used threading to allow for I/O to occur concurrently with servicing client requests.
For this Assignment, you will modify your finger server program from Week 6 to use threads. The server should activate a new thread to process each incoming client request, thus allowing client requests to be processed concurrently.
To prepare:
Start up NetBeans.
Open your implementation of the finger protocol from Week 6.
By Day 7, modify your finger server program to use threads. The server should activate a new thread to process each incoming client request, thus allowing client requests to be processed concurrently.
In addition, write a 5- to 7-page paper documenting the changes that you made and analyzing the impact the changes will have on the overall throughput and latency of the server.
Include in your paper responses to these questions:
What other solutions might you consider in order to increase throughput and reduce latency?
Which solution do you consider to have the better trade-off between performance and complexity?
Which solution do you consider to have the better application of modularity, abstraction, hierarchy, and layering?
Remember to properly cite your sources according to APA guidelines.
Save your implementation of the threaded finger server in a ".zip" file. You will also submit your paper documenting the implementation and analysis of your server separately.
Explanation / Answer
Modified code to implement threaded finger server:-
Class EchoServer:-
import java.io.*;
import java.net.*;
public class EchoServer{
public static void main(String args[]){
int port=6789;
EchoServer server=new EchoServer(port);
server.startServer();
}
//declare the server socket and the client socket for the server
//declare the no. of connections
ServerSocket echoServer=null;
Socket clientSocket=null;
int numConnections=0;
int port;
public EchoServer(int port){
this.port=port;
}
public void stopServer(){
System.out.println("Server is cleaning up");
System.exit(0);
}
public void startServer(){
//Try to open the server socket on given port
try {
echoServer=new ServerSocket(port);
}
catch (IOException e) {
System.out.println(e);
}
//Whenever we receive a connection start a new thread to //process it and wait for the next connection.
while(true){
try {
clientSocket=echoServer.accept();
numConnections++;
EchoServerConnection EchoServerConnection(clientSocket,numConnections,this);
new Thread(oneconnection).start();
}
catch (IOException e){
System.out.println(e);
}
}
}
}
class EchoServerConnection implements Runnable{
BufferedReader is;
PrintStream os;
Socket clientSocket;
int id;
EchoServer server;
public EchoServerConnection(Socket clientSocket,int id, EchoServer server){
this.clientSocket=clientSocket;
this.id=id;
this.server=server;
System.out.println("The connection "+id+" established with: "+clientSocket);
try {
is=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os=new PrintStream(clientSocket.getOutputStream());
}catch(IOException e){
System.out.println(e);
}
}
public void run(){
String line;
try {
boolean serverStop=false;
while(true){
line=is.readLine();
System.out.println("Received the line "+line+" from the connection "+id);
int n=Integer.parseInt(line);
if(n==-1){
serverStop=true;
break;
}
if(n==0)break;
os.println(" "+n*n);
}
System.out.println("The connection "+id+" is closed." );
is.close();
os.close();
clientSocket.close();
if(serverStop)server.stopServer();
} catch(IOException e){
System.out.println(e);
}
}
}
Class EchoClient:-
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) {
String hostname="localhost";
int port=6789;
Socket clientSocket=null;
DataOutputStream os=null;
BufferedReader is=null;
// Try to open the socket on given port
// Try to open the input and output streams
try {
clientSocket=new Socket(hostname,port);
os=new DataOutputStream(clientSocket.getOutputStream());
is=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch(UnknownHostException e){
System.err.println("Unknwon host: "+hostname);
} catch(IOException e){
System.err.println("Unable to get I/O for the connection to: "+hostname);
}
//If everything is initialized then we need to write some data
//to the socket which is opened a connection to the given port
if(clientSocket==null||os==null||is==null){
System.err.println("Something is wrong,1 variable is null" );
return;
}
try{
while(true){
System.out.print("Enter integer(0 to stop the connection,-1 to stop the server):" );
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String keyboardInput=br.readLine();
os.writeBytes(keyboardInput+" " );
int n=Integer.parseInt(keyboardInput);
if(n==0||n==-1){
break;
}
String responseLine=is.readLine();
System.out.println("Server has returned its square as "+ responseLine);
}
os.close();
is.close();
clientSocket.close();
}catch(UnknownHostException e){
System.err.println("Trying to connect to the unknown host "+e);
} catch(IOException e){
System.err.println("IOException "+e);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.