Question

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 with appropriate code to make them work.

===Client Side Java code below======

import java.io.*;

import java.net.*;

class UDPClient {

    public static void main(String args[]) throws Exception

    {

      DatagramSocket clientSocket = new __________;

      InetAddress IPAddress = InetAddress.getByName("________");

      byte[] sendData = new byte[1024];

      byte[] receiveData = new byte[1024];

      String sentence = “This is a Test for cpsc5157U class”;

________ = sentence.getBytes();    

    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.______, IPAddress, _____);

      clientSocket.______;

      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

      clientSocket.receive(________);

      String modifiedSentence = new String(receivePacket.getData());

      System.out.println("FROM SERVER:" + modifiedSentence);

      clientSocket.close();

      }

}

=======End of Client Side Code ===========

==== Server Side Java Code below===========

import java.io.*;

import java.net.*;

class UDPServer {

public static void main(String args[]) throws Exception

    {

      DatagramSocket serverSocket = new ____________(34926);

      byte[] receiveData = new byte[1024];

      byte[] sendData = new byte[1024];

      while(true) {

          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

          serverSocket.receive(receivePacket);

          String sentence = new String(receivePacket.getData());

          InetAddress MyAddress = receivePacket.getAddress();

          int port = receivePacket.getPort();

          String capitalizedSentence = sentence.toUpperCase();

          sendData = capitalizedSentence.getBytes();

          DatagramPacket sendPacket = new DatagramPacket(______, sendData.length, _______, port);

          serverSocket.send(sendPacket);

       }

     }

}

========= End of Server Side Java Code============

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

hostname and port number in client side is not given so I have passed them as hostname and portno respectively, which you can change accordingly.

Below is the code for client side with blanks filled :

import java.io.*;

import java.net.*;

class UDPClient {

    public static void main(String args[]) throws Exception

    {

      DatagramSocket clientSocket = new DatagramSocket( );

      InetAddress IPAddress = InetAddress.getByName(hostname);

      byte[] sendData = new byte[1024];

      byte[] receiveData = new byte[1024];

      String sentence = “This is a Test for cpsc5157U class”;

  sendData = sentence.getBytes();    

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, portno);

      clientSocket.send(request);

      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

      clientSocket.receive(receivePacket);

      String modifiedSentence = new String(receivePacket.getData());

      System.out.println("FROM SERVER:" + modifiedSentence);

      clientSocket.close();

      }

}

Below is the code for server side with blanks filled :

import java.io.*;

import java.net.*;

class UDPServer {

public static void main(String args[]) throws Exception

    {

      DatagramSocket serverSocket = new DatagramSocket(34926);

      byte[] receiveData = new byte[1024];

      byte[] sendData = new byte[1024];

      while(true) {

          DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

          serverSocket.receive(receivePacket);

          String sentence = new String(receivePacket.getData());

          InetAddress MyAddress = receivePacket.getAddress();

          int port = receivePacket.getPort();

          String capitalizedSentence = sentence.toUpperCase();

          sendData = capitalizedSentence.getBytes();

          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, MyAddress, port);

          serverSocket.send(sendPacket);

       }

     }

}

GOOD LUCK, PLEASE REVIEW AND UPVOTE!!!

Add a comment
Know the answer?
Add Answer to:
Q7 The following Client side Java code can send a message to the server side via...
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
  • 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...

  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

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

  • 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

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

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

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

  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

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