Question

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**

*** 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.

  

Add a comment
Know the answer?
Add Answer to:
Java(Eclipse). The task will create a server and client and send a message from the client...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q7 The following Client side Java code can send a message to the server side via...

    Q7 The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks...

  • The following Client side Java code can send a message to the server side via UDP...

    The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks with...

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of...

    implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of the classes exatcly the same as requested for testing purpose. please follow each instruction provided below Objective of this assignment o get you famililar with developing and implementing TCP or UDP sockets. What you need to do: I. Implement a simple TCP Client-Server application 2. and analyze round trip time measurements for each of the above applications 3. The...

  • You are to write a Java program using the following instructions to build a bank-ATM server...

    You are to write a Java program using the following instructions to build a bank-ATM server system with sockets. A bank holds accounts that can have deposits, withdrawals, or retrievals of balance. The bank server provides the account for transactions when a client uses and ATM machine. The ATM asks for the type of transaction and the amount (if needed), then creates a socket connection to the bank to send the transaction for processing. The bank performs the transaction on...

  • I have to modify a server program and chat program to work as the following instructions...

    I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java. Diffie-Hellman Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q)....

  • Error: Main method not found in class ServiceProvider, please define the main method as: public static...

    Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...

  • please use java Advance Java CSCI 3210 Section Name: Date: Final Project (100 pts.) Instructions: Submit...

    please use java Advance Java CSCI 3210 Section Name: Date: Final Project (100 pts.) Instructions: Submit an electronic copy of the assignment to the Dropbox (Ex: ClassName.java, ClassNameTest.java, etc.) CHECK CALENDAR FOR DUE DATE IMPORTANT: FOLLOW GOOD PROGRAMMING STYLE/CONVENTION FOR NAMING ALL YOUR OBJECTS/VARIABLES. ADD COMMENTS ON TOP OF EACH METHOD AND AT TOP OF THE FILE, Final Project Requirements (TCP): The final project assignment is to create a Messaging Chat GUI, based on client/server framework. The default behavior is...

  • Here’s your task: You’re going to create exactly two classes. One class will act as the...

    Here’s your task: You’re going to create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played. This is important: For each player’s turn they should start by displaying their current score to...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT