Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help needed for JAVA 21-3. Redo Exercise 21-2 but write the server and client program using a datagram socket. If possible, again use a group

Help needed for JAVA

21-3. Redo Exercise 21-2 but write the server and client program using a datagram socket. If possible, again use a group of 2 or more student. One student will run the server, the other students can be the clients.

package net21; /* * Exercise 21-2 client * This program will be a client to send messages to a server */ import java.io.*; import java.net.*;

public class ex212client { private int port; private InetAddress host;

public ex212client() throws IOException { try { port = 7777; // use port #7777 that server is waiting on // change name to printout of server's name - check server's printout

host = InetAddress.getByName("local host");// } catch(UnknownHostException u) { u.printStackTrace(); } }

public void run(String mes) { try { /* Create socket connected to host on port. Create PrintWriter connected to socket and print message over socket. Create BufferedReader connected to socket and print the string read over the socket to the screen. Close the socket. */

Socket connection; connection = new Socket( host, port ); PrintWriter out=new PrintWriter(new OutputStreamWriter (connection.getOutputStream())); out.println(mes); out.flush(); BufferedReader incoming = new BufferedReader( new InputStreamReader(connection.getInputStream()) );

String msg=incoming.readLine(); System.out.print(msg); connection.close(); } catch(IOException ex) { ex.printStackTrace(); } }

public static void main(String[] args) throws IOException { String line; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a message: "); line = br.readLine();

ex212client c = new ex212client(); c.run(line); // call run method with string line to send to server } }______________________________________________________________________________________

package net21; /* * Exercise 21-2 server * This program will be a server to receive messages from a client * using TCP/IP socket */ import java.io.*; import java.net.*;

public class ex212server { private InetAddress host; private int port;

public ex212server() { try { host = InetAddress.getLocalHost(); // use server computer as host port = 7777; // wait on port #7777 System.out.println("Server name is " + host.getHostName()); System.out.println("Server address is " + host); System.out.println("Port is " + port); } catch(UnknownHostException u) { u.printStackTrace(); } }

public void run() { try { /* Create ServerSocket and Socket. Then create BufferedReader connected to Socket. Print where message is coming from and print the message. Create a PrintWriter connected to the Socket, then send back a message over the Socket that the message was received. Close all the Sockets. */ ServerSocket listener; // Listens for incoming connections. Socket connection; // For communication with the connecting program. listener = new ServerSocket(7777);

connection = listener.accept();

BufferedReader incoming = new BufferedReader( new InputStreamReader(connection.getInputStream()) ); String msg=incoming.readLine(); System.out.println("message is coming from: "+connection.getInetAddress()); System.out.println("message : "+msg);

PrintWriter out=new PrintWriter(new OutputStreamWriter (connection.getOutputStream())); out.println("Server: Messages was received"); out.flush(); connection.close(); listener.close(); } catch(IOException e) { e.printStackTrace(); } }

public static void main(String[] args) { ex212server s = new ex212server(); s.run(); } }________________________________________________________________________________________

a. The server will run (list the host name and decide on a port number for the clients) and listen for a message from clients. It will accept that message as a part of a datagram packet and print the message to the screen. It will then send back a packet saying the message was received. b. The client(s) will run and send a message to the server as a part of a datagram packet. It will use the server's host name and port number. Again, use the getByName() with the server computer's name for the host. It will then receive a packet back saying the message was received. Here are the server and client. Fill in the missing part of the run() method for both files. Server: package net21; /* * Exercise 21-2 server * This program will be a server to receive messages from a client * using TCP/IP socket */ import java.io.*; import java.net.*; public class ex213server { private InetAddress host; private int port; public ex213server() { try { host = InetAddress.getLocalHost(); // host computer port = 7777; // wait on port 7777 System.out.println("Server address is " + host); System.out.println("Port is " + port); } catch(UnknownHostException u) { u.printStackTrace(); } } public void run() { try { /* Create DatagramSocket connected to port, create byte array, create DatagramPacket for byte array. Wait to receive packet, then get the message and sender address from the packet. Print to screen who the sender is, what the sender's name is, and what the message is. Close the socket. */ } catch(SocketException ss) { ss.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ex213server s = new ex213server(); s.run(); } }___________________________________________________________________________________________________ Client: package net21; /* * Exercise 21-2 client * This program will be a client to send messages to a server */ import java.io.*; import java.net.*; public class ex213client { private int port; private InetAddress host; public ex213client() { try { port = 7777; // server's port number // host = InetAddress.getByName("bt20402.hvcc.edu"); // if at school // fill in your server's computer name // host = InetAddress.getLocalHost(); // same computer as server, but use server's name instead } catch(UnknownHostException u) { u.printStackTrace(); } } public void run(String mes) { try { /* Create DatagramSocket, create DatagramPacket out of message and who the host and port are. Send the packet. Create byte array and the DatagramPacket for the byte array. Receive the incoming packet. Get the message out of the packet and print it to the screen. Close the socket */ } catch(IOException ex) { ex.printStackTrace(); } } public static void main(String[] args) throws IOException { String line; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a message: "); line = br.readLine(); ex213client c = new ex213client(); c.run(line); // send message to run() } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

Know the principles of effective service recovery systems.

Answered: 1 week ago

Question

Explain the service recovery paradox.

Answered: 1 week ago