Application: Implementing a Client-Server Protocol You can learn a lot about the
ID: 3818511 • 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.
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.
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)
}
Explanation / Answer
server side
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
open class Server
{
private static Socket attachment;
open static void main(String[] args)
{
attempt
{
int port = 25000;
serverSocket = new ServerSocket(port);
System.out.println("Server Started and tuning in to the port 25000");
/Server is running dependably. This is done utilizing this while(true) circle
while(true)
{
/Reading the message from the customer
attachment = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message got from customer is "+number);
/Multiplying the number by 2 and framing the arrival message
String returnMessage;
attempt
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + " ";
}
catch(NumberFormatException e)
{
/Input was not a number. Sending appropriate message back to customer.
returnMessage = "Please send an appropriate number ";
}
/Sending the reaction back to the customer.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the customer is "+returnMessage);
bw.flush();
}
}
get (Exception e)
{
e.printStackTrace();
}
at last
{
attempt
{
socket.close();
}
catch(Exception e){}
}
}
}
clent .java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
open class Client
{
private static Socket attachment;
open static void main(String args[])
{
attempt
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
attachment = new Socket(address, port);
/Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String number = "2";
String sendMessage = number + " ";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
/Get the arrival message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message got from the server : " +message);
}
get (Exception exemption)
{
exception.printStackTrace();
}
at long last
{
/Closing the attachment
attempt
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.