Question: Application: Implementing a Client-Server Protocol You can learn a lot about the design of client-server protocols by reviewing the implementation of one. And you can

Application: Implementing a Client-Server Protocol

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 Internets 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 Internets 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 Internets 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 clients request.

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

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

Here is what I have....can you tell me what is wrong with my code:

______________EchoClient.Java_____________________

import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket;

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______________________

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

} }

________________FingerClient.java______________

// TCP finger client - Greg Taylor... // Usage: Finger username@host.com import java.io.*; import java.net.*; public class FingerClient { public static void main ( String args[] ) { // Check command line paramaters

if (args.length != 1) { System.err.println ("Invalid number of paramaters:"); System.err.println ("Usage: Finger username@host"); System.exit(1); } // Check for existence of @ in paramater else if (!args[0].contains("@")) { System.err.println ("Invalid paramater : syntax user@host"); System.exit(1); } // Split command line paramater at the @ character String username = args[0].substring(0, args[0].indexOf("@") ); String hostname = args[0].substring(args[0].indexOf("@") +1, args[0].length()); try { System.out.println ("Connecting to " + hostname); // Create a connection to server Socket s = new Socket(hostname, 79); // Create input and output streams to socket PrintStream out = new PrintStream( s.getOutputStream()) ; BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream())); // Write username to socket output out.println( username ); // Read response from socket String line = in.readLine(); while (line != null) { System.out.println ( line );

// Read next line line = in.readLine(); }

// Terminate connection s.close(); } catch (SocketException e ) { System.err.println ("Socket error : " + e); } catch (UnknownHostException e ) { System.err.println ("Invalid host!"); } catch (IOException e ) { System.err.println ("I/O error : " + e); } } }

____________________FingerServer.java_________________

import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date;

public class FingerServer extends Thread { private ServerSocket sock;

public FingerServer() { super("Finger Server"); try { sock = new ServerSocket(79); System.out.println("Finger Server up and running ..."); } catch (IOException e) { System.err.println("Error: couldn't create socket."); System.exit(1); } }

@Override public void run() { Socket client = null;

// Look for clients while (true) { // Wait for a client if (sock == null) return; try { client = sock.accept(); } catch (IOException e) { System.err.println("Error: couldn't connect to client."); System.exit(1); }

// Process finger requests try { InputStreamReader isr = new InputStreamReader( client.getInputStream()); BufferedReader is = new BufferedReader(isr); PrintWriter os = new PrintWriter(new BufferedOutputStream(client.getOutputStream()), false); String outLine = null;

// Output server greeting os.println("*** Finger Server"); os.flush();

// Process and output user input String inLine = is.readLine(); if (inLine.length() > 0) outLine = inLine; readPlan(outLine, os); os.flush();

// Clean up os.close(); is.close(); client.close(); } catch (Exception e) { System.err.println("Error: " + e); e.printStackTrace(); } } }

public static void main(String[] arguments) { FingerServer server = new FingerServer(); server.start(); }

void readPlan(String userName, PrintWriter os) throws IOException { /* FileReader file = new FileReader(userName + ".plan"); BufferedReader buff = new BufferedReader(file); boolean eof = false;*/ os.println(" Login: " + userName + " "); os.println(" NAME: " + "XYZ " +userName + " "); Date d = new Date(); os.println(" TIME: "+d.toString()); /*while (!eof) { String line = buff.readLine(); if (line == null) eof = true; else os.println(line); } buff.close();*/ }

This is all I get....just says up and running...unit I stop it

run: Finger Server up and running ... BUILD STOPPED (total time: 10 seconds) }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!