Application: Implementing a Client-Server Protocol You can learn a lot about the
ID: 3815235 • Letter: A
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 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.
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.
***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
Finger.java
// Connect to a machine (first arg) using finger protocol
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Finger {
final static int port = 7000;
public static void main (String [] args) {
final String hostname = args.length>0?args[0]:"localhost";
try {
final Socket s = new Socket (hostname, port);
final PrintWriter pw = new PrintWriter (s.getOutputStream());
final BufferedReader in = new BufferedReader (
new InputStreamReader (s.getInputStream()));
for (int i=1; i<args.length; i++)
{
pw.print (args[i]+ " ");
}
pw.print (" ");
pw.flush();
for (;;) {
final String line = in.readLine();
if (line==null) break;
System.out.println (line);
}
s.close();
} catch (IOException e) {
e.printStackTrace (System.err);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.