Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Application: Implementing Threading in a Client-Server Protocol Server programs are rarely implemented as non-threaded applications, even though that is how you implemented your previous server

Application: Implementing Threading in a Client-Server Protocol

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.

Verification that this source code is working in Netbeans. Not working on mine....may have to uninstall and reinstall program. If it is, if I could get a screenshot of it. If not, what is wrong with the source code.

***EchoServer.java***

public class EchoServer {

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

ServerSocket serverSocket = new ServerSocket(79);

while (true) {

final Socket socket = serverSocket.accept();

new Thread(new Runnable() {

public void run() {

System.out.println("Accepted an echo request");

System.out.println("... local socket address " + socket.getLocalSocketAddress());

System.out.println("... remote socket address " + socket.getRemoteSocketAddress());

try {

InputStream input = socket.getInputStream();

OutputStream output = socket.getOutputStream();

while (true) {

int b = input.read();

if (b == -1) break;

output.write(b);

}

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

}

}

}

__________________________________________________________________________________________________________

complicated matters we can use ThreadPool. An easy way is to use an ExecutorService for thread handling.

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

// Replace with suitable executor

ExecutorService executor = Executors.newCachedThreadPool();

ServerSocket serverSocket = new ServerSocket(79);

while (true) {

final Socket socket = serverSocket.accept();

executor.execute(new Runnable() {

public void run() {

try {

handleSocket(socket);

} catch (IOException e) {

// Handle exception

}

}

private void handleSocket(final Socket socket) throws IOException {

// Do stuff with your socket, same as original code

}

});

}

}

__________________________________________________________________________________________________________

***EchoClient.java***

public class EchoClient {

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

int b;

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

//Socket socket = new Socket("localhost", 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();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions