Question

Using Eclipse I am trying to pass a string of numbers from the client class to...

Using Eclipse I am trying to pass a string of numbers from the client class to the server. The server will then compute the sum, average max and min from the numbers and then pass it back to the client. The client will then display the answers in a text field. This is what i have so far for the class client and not sure how to proceed or if it's correct. i also am not sure how to complete the server class but if i could know if the client class is correct i can proceed to work on the server class.

Here is what i have so far:

butSend.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) {

                                       try {
                                          
                                           DatagramSocket clientSocket = new DatagramSocket();
                         
                                          
                                           byte[] data = new byte[1024];
                                           data = message.getBytes();
                                          
                                           InetAddress IPAddress = InetAddress.getByName("localhost");
                                           int portNumber = 9877;
                                          
                                           DatagramPacket dp = new DatagramPacket(data, data.length, IPAddress, portNumber);
                                          
                                           clientSocket.send(dp);

                                           byte[] sum = new byte[1024];
                                           byte[] average = new byte[1024];
                                           byte[] max = new byte[1024];
                                           byte[] min = new byte[1024];
                  
                                           DatagramPacket sumdp = new DatagramPacket(sum, sum.length);
                                           DatagramPacket averagedp = new DatagramPacket(average, average.length);
                                           DatagramPacket maxdp = new DatagramPacket(max, max.length);
                                           DatagramPacket mindp = new DatagramPacket(min, min.length);
                                          
                                           clientSocket.receive(sumdp);
                                           clientSocket.receive(averagedp);
                                           clientSocket.receive(maxdp);
                                           clientSocket.receive(mindp);
                                          
                                           String sumMessage = new String(sumdp.getData());
                                           String averageMessage = new String(averagedp.getData());
                                           String maxMessage = new String(maxdp.getData());
                                           String minMessage = new String(mindp.getData());
                                          
                                       JTextArea textCal = new JTextArea();
                                           textCal.setFont(new Font("Times", Font.BOLD, 14));
                                       textCal.setBounds(20, 400, 400, 400);
                                       textCal.setEditable(false);
                                       frame.getContentPane().add(textCal);
                                      
                                       answer = "Sum= \n Average= \n Max= \n Min=";
                                       textCal.setText(answer);
  
                                       } catch (Exception ex) {
                                           System.out.println(ex.toString());
                                       }      
                                   }
                               });
                       }
                       }

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

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

CODE:

//ChatServer.java
import java.net.*;
import java.io.*;
class ChatServer
{
public static void main(String args[])
{
try
{
ServerSocket ss = new ServerSocket(8000);
System.out.println("Waiting for client to connect..");
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out = new DataOutputStream(s.getOutputStream());
DataInputStream in = new DataInputStream(s.getInputStream());
String receive, send;
while((receive = in.readLine()) != null)
{
if(receive.equals("STOP"))
break;
System.out.println("Client Says : "+receive);
System.out.print("Server Says : ");
send = br.readLine();
out.writeBytes(send+"\n");
}
br.close();
in.close();
out.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

//ChatClient.java
import java.net.*;
import java.io.*;
class ChatClient
{
public static void main(String args[])
{
try
{
Socket s = new Socket("Localhost",8000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out = new DataOutputStream(s.getOutputStream());
DataInputStream in = new DataInputStream(s.getInputStream());
String msg;
System.out.println("To stop chatting with server type STOP");
System.out.print("Client Says: ");
while((msg = br.readLine()) != null)
{
out.writeBytes(msg+"\n");
if(msg.equals("STOP"))
break;
System.out.println("Server Says : "+in.readLine());
System.out.print("Client Says : ");
}
br.close();
in.close();
out.close();
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

RATE THUMBSUP PLEASE

Add a comment
Know the answer?
Add Answer to:
Using Eclipse I am trying to pass a string of numbers from the client class to...
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...

  • Overview UDP is a transport layer protocol that provides no reliability. This project aims to show...

    Overview UDP is a transport layer protocol that provides no reliability. This project aims to show how to implement extra features for the protocol in the application layer. The project will develop a new version of UDP protocol called UDPplus which provides reliable connection oriented between a client and a UDPplus sever. The UDPplus is implemented at application layer using UDP protocol only. Basic Scenario The UPDplus is a simple bank inquiry application where the client sends full name and...

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

  • My client java a simple client program java .net import java.io*i public class MyClient (public static...

    My client java a simple client program java .net import java.io*i public class MyClient (public static void main(String args() thrown IO Exception (part I: initialize rocket and stream BufferedReader inFromUser - new BufferedReader(new inputStreamReader(System in)); Socket clientSocket - new Socket(___. ___); DataOutputStream oldToServer - new DataOutputStream(clientSocket.getOutputStrea, ())); part 2: interact with server

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

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