Help needed for JAVA 21-3. Redo Exercise 21-2 but write the server and client pr
ID: 3819427 • Letter: H
Question
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()
}
}
Explanation / Answer
ex212client.java
import java.io.*;
import java.net.*;
public class ex212client
{
private int port;
private InetAddress host;
public ex212client()
{
try
{
port = 7777; // use port #7777 that server is waiting on
host = InetAddress.getByName("Cartographer");
System.out.println(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 client = new Socket(host, port);
PrintWriter pw = new PrintWriter(client.getOutputStream(), true);
BufferedReader br = new BufferedReader
(new InputStreamReader(client.getInputStream()));
pw.println(mes);
String lineread = br.readLine();
System.out.println(lineread);
client.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
}
}
ex212server.java
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 ss = new ServerSocket(port);
Socket incoming = ss.accept();
BufferedReader br = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter pw = new PrintWriter(incoming.getOutputStream(), true);
String lineread = br.readLine();
System.out.println(lineread);
pw.println("Message Received");
ss.close();
incoming.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ex212server s = new ex212server();
s.run();
}
}
ex213client.java
import java.io.*;
import java.net.*;
public class ex213client
{
private int port;
private InetAddress host;
public ex213client()
{
try
{
port = 7777; // use port #7777 that server is waiting on
host = InetAddress.getByName("Cartographer");
System.out.println(host);
}
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*/
byte[] buffer = new byte[256];
DatagramSocket client = new DatagramSocket();
DatagramPacket outdgp = new DatagramPacket(mes.getBytes(), mes.length(), host, port);
client.send(outdgp);
DatagramPacket indgp = new DatagramPacket(buffer, buffer.length);
client.receive(indgp);
String rsp = new String(indgp.getData(), 0, indgp.getLength());
System.out.println(rsp);
client.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();
ex213client c = new ex213client();
c.run(line); // call run method with string line to send to server
}
}
ex213server.java
import java.io.*;
import java.net.*;
public class ex213server
{
private InetAddress host;
private int port;
public ex213server()
{
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 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.
*/
DatagramSocket ss = new DatagramSocket(port);
byte[] buffer = new byte[256];
DatagramPacket indgp = new DatagramPacket(buffer, buffer.length);
ss.receive(indgp);
InetAddress senderaddress = indgp.getAddress();
String rsp = new String(indgp.getData(), 0, indgp.getLength());
System.out.println(senderaddress);
System.out.println(senderaddress.getHostName());
System.out.println(rsp);
String mes = "Message Received";
DatagramPacket outdgp = new DatagramPacket(mes.getBytes(), mes.length(), senderaddress, port);
ss.send(outdgp);
ss.close();
}
catch(SocketException ss)
{
ss.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ex213server s = new ex213server();
s.run();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.