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

Java(Eclipse). The task will create a server and client and send a message from

ID: 3860456 • Letter: J

Question

Java(Eclipse).

The task will create a server and client and send a message from the client to the server. We will have two classes

NetworkClient and NetworkServerListener classes.

These will each have a main to run from the command line

The NetworkServerListener class will listen for a client connection from NetworkClient , receive a message, display the message to the console (stdout) and exit.

The NetworkClient class will create a connection to the NetworkServerListener and send a message to the server and exit.

We must use try and catch blocks as appropriate. there should be one or more try/catch blocks in that method if it throws an exception. It may be appropriate to "pass" the exception to the calling method but some method in the call stack must handle the exception. We cannot let it bubble up to the virtual machine and have our application abnormally terminated. The code should be simple(mostly from the java introductory book).

We must compile using the standard Java 1.8 or later (javac.exe) or later compiler. The application must execute using the standard Java 1.8 or later virtual machine (JVM). This means we cannot use any Java extensions not part of the standard libraries. We must use the Eclipse IDE.

Thanks

Explanation / Answer

/**

*** code for NetworkServerListener

**/

import java.io.*;

import java.net.*;

public class NetworkServerListener

{

public static void main(String[] args)

{

try{

ServerSocket serverSocket = new ServerSocket(3000);

System.out.println("Server ready for chatting");

Socket sock = serverSocket.accept( );

// reading from keyboard (keyRead object)

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

// sending to client (pwrite object is send to the client)

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object is recived from the server sent by the client)

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;   

if((receiveMessage = receiveRead.readLine()) != null)

{

System.out.println(receiveMessage);   

}   

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);   

pwrite.flush();

}

catch(IOException e) {

e.printStackTrace();

}

}

}

/***

**

*** code for NetworkClient.java

**

***/

import java.io.*;

import java.net.*;

public class NetworkClient

{

public static void main(String[] args)

{

try{

Socket sock = new Socket("127.0.0.1", 3000); // "127.0.0.1" is the name of server(IP Address)

// reading from keyboard (keyRead object)

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

// sending to client (pwrite object is send to the client)

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object is recived from the server sent by the client)

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start chat, type and press Enter key");

String receiveMessage, sendMessage;   

sendMessage = keyRead.readLine(); // keyboard reading

pwrite.println(sendMessage); // sending to server

pwrite.flush(); // flush the data

if((receiveMessage = receiveRead.readLine()) != null) //receive from server

{

System.out.println(receiveMessage); // displaying at DOS prompt

}   

}

catch(IOException e) {

e.printStackTrace();

}

}

}

Note:

First execute the NetworkServerListener.java program on the server then executes the NetworkClient.java program on client.

  

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