Server programs are rarely | Computer Science homework help


  

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. 

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.

FingerServer.java:

import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

public class FingerServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(79);

System.out.println(“The Finger 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());

BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter output = new PrintWriter(socket.getOutputStream());

String line; while (true) { line = input.readLine(); if (line == null) break;

System.out.println(“Client said: ” + line);

output.write(“Message from server: ” + line);

output.flush(); }

socket.close();

}

}

}

FingerClient.java:

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

public class FingerClient {

public static void main(String[] args) throws IOException {

int b;

if(args.length < 2) {

System.out.println(“Provide ip address and message through command line”);

return;

} Socket socket = new Socket(args[0], 79);

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 | Computer Science homework help


  

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. 

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.

FingerServer.java:

import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

public class FingerServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(79);

System.out.println(“The Finger 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());

BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter output = new PrintWriter(socket.getOutputStream());

String line; while (true) { line = input.readLine(); if (line == null) break;

System.out.println(“Client said: ” + line);

output.write(“Message from server: ” + line);

output.flush(); }

socket.close();

}

}

}

FingerClient.java:

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

public class FingerClient {

public static void main(String[] args) throws IOException {

int b;

if(args.length < 2) {

System.out.println(“Provide ip address and message through command line”);

return;

} Socket socket = new Socket(args[0], 79);

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();

}

}

Be the first to reply

Leave a Reply

Your email address will not be published.