Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You can learn a lot about the design of client-server protocols by reviewing the

ID: 3591721 • Letter: Y

Question

You can learn a lot about the design of client-server protocols by reviewing the implementation of one. And you can develop an even greater understanding by extending or updating that implementation.

For this Assignment, you will modify an existing client-server protocol to implement the Internet’s finger protocol.

To prepare:

Download the existing program contained in Week6_Echo.zip. This file unzips into a NetBeans project that includes the source code for two Java programs, a client program, and a server program. The two programs implement the Internet’s echo protocol.

Start up NetBeans. Open the Week6_Echo project you just downloaded and unzipped.

By Day 7, modify the client and the server so that they implement the Internet’s finger protocol. The response from the server does not need to reflect actual user data, but you must base the response on the name provided in the client’s request.

Save and submit your Assignment as a ".zip" file.

Retain your implementation, as you will be using it again in Week 7.

Please provide answer in a NetBeans project. Original project can be provided if needed.

echoclient.java:

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

echoserver.java:

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

}
}

Explanation / Answer

fingerclient.java:

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 FingerClient {

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

int b;

string compname = args.length > 0 ? args[0] : "localhost";

Socket socket = new Socket(compname, 79);

try {
InputStream input = socket.getInputStream();
final BufferedReader inputreader = new BufferedReader(new InputStreamReader (input));

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

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

if(args.length > 1)
printwriter.print(args[1].getBytes());

socket.shutdownOutput();
printwriter.flush();

while (true) {
b = inputreader.read();
if (b == null) {
break;
}
System.out.println((char) b);
}
System.out.println();

socket.close();
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}

-----------------------------------------------------------------------------------------------------------------

fingerserver.java:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set;

class FingerServer {
private static void validateandgeneratePlan(String userName, PrintWriter printwriter) throws Exception {
FileReader filereader = new FileReader(userName + ".plan");
BufferedReader buffferreader = new BufferedReader(filereader);
boolean eof = false;
printwriter.println(" User name: " + userName + " ");
while (!eof) {
String line = buffferreader.readLine();
if (line == null)
eof = true;
else
printwriter.println(line);
}
buffferreader.close();
}

public static void main(String[] arguments) throws Exception {
ServerSocketChannel socketChannel = ServerSocketChannel.open();
socketChannel.configureBlocking(false);

InetSocketAddress localserver = new InetSocketAddress("localhost", 79);
ServerSocket socket = socketChannel.socket();
socket.bind(localserver);

Selector selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_ACCEPT);

while (true) {
selector.select();
Set keys = selector.selectedKeys();
Iterator it = keys.iterator();
while (it.hasNext()) {
SelectionKey selKey = (SelectionKey) it.next();
it.remove();
if (selKey.isAcceptable()) {
ServerSocketChannel selChannel = (ServerSocketChannel) selKey.channel();
ServerSocket selSocket = selChannel.socket();
Socket connection = selSocket.accept();
  
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader is = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(new BufferedOutputStream(connection.getOutputStream()), false);
pw.println("NIO Finger Server");
pw.flush();
String outLine = null;
String inLine = is.readLine();
if (inLine.length() > 0) {
outLine = inLine;
}
validateandgeneratePlan(outLine, pw);
pw.flush();
pw.close();
is.close();
  
connection.close();
}
}
}
}
}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote