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

Could you please explain and solve by Java programming with details code of the

ID: 3595610 • Letter: C

Question

Could you please explain and solve by Java programming with details code of the programming problem?

An echo server echoes back whatever it receives from a client. Forexample, if a client sends the server the string Hello there!, the serverwill respond with Hello there! Write an echo server using the Java networking API described in Section 3.6.1. This server will wait for a client connection using the accept() method. When a client connection is received, the server will loop, performing the following steps:

• Read data from the socket into a buffer.

• Write the contents of the buffer back to the client.

The server will break out of the loop only when it has determined that the client has closed the connection.

The date server shown in Figure 3.21(the figure is included in next page) uses the java.io.BufferedReader class. BufferedReader extends the java.io.Reader class, which is used for reading character streams.

However, the echo server cannot guarantee that it will read characters from clients; it may receive binary data as well. The

class java.io.InputStream deals with data at the byte level rather than the character level. Thus, your echo server must use an object the client has closed its end of the socket connection.

The figure 3.21 of text:

import java.net.*;

import java.io.*;

public class DateServer

{

public static void main(String[] args) {

try {

ServerSocket sock = new ServerSocket(6013);

/* now listen for connections */

while (true) {

Socket client = sock.accept();

PrintWriter pout = new

PrintWriter(client.getOutputStream(), true);

/* write the Date to the socket */

pout.println(new java.util.Date().toString());

/* close the socket and resume */

/* listening for connections */

client.close();

}

}

catch (IOException ioe) {

System.err.println(ioe);

    }

}

}

Figure 3.21 Date server.

Explanation / Answer

EchoClient.java

import java.io.*;
import java.net.*;

public class EchoClient
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("localhost", 2000);
BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter w = new PrintWriter(s.getOutputStream(), true);
BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
String line;
do
{
line = r.readLine();
if ( line != null )
System.out.println(line);
line = con.readLine();
w.println(line);
}
while ( !line.trim().equalsIgnoreCase(("bye")));
}
catch (Exception e)
{
System.out.println(e);
}
}
}

EchoServer.java


import java.io.*;
import java.net.*;

public class EchoServer
{
  
public static void main(String[] args) throws IOException
{
  
ServerSocket server = new ServerSocket(1000);
while (true)
{
Socket client = server.accept();
BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter w = new PrintWriter(client.getOutputStream(), true);
w.println("Welcome to Echo Server. Enter 'bye' to quit.");
String line;
do
{
line = r.readLine();
System.out.println(line);
if ( line != null )
w.println(line);
}
while ( !line.trim().equalsIgnoreCase("bye") );
client.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