Question
below is 4 classes.java that i have done need help finishing what I have worked on so far. import java.util.*; public class Client { private
below is 4 classes.java that i have done need help finishing what I have worked on so far.
import java.util.*;
public class Client { private int ID; private int Port; private String Name; public Client(){} // public Client(int id){ // this.ID = id; // } public Client(int port){ this.Port = port; } public Client(int id, int port){ this.ID = id; this.Port = port; } public Client(int id, int port, String n){ this.ID = id; this.Port = port; this.Name = n; } public int getID(){return this.ID;} public void setID(int id){this.ID=id;} public int getPort(){return this.Port;} public void setPort(int port){this.Port = port; } public String getName(){return this.Name;} public void setName(String name){ this.Name = name;} public void PrintDate(){ Calendar ca = Calendar.getInstance(); System.out.println(ca.getTime().toString()); }
import java.util.*; import java.io.*; import java.net.*;
public class PrintClient extends Client{ private String host; private String Location; public String getLocation(){return this.Location;} public void setLocation(String location){ this.Location = location;} public PrintClient() { } public PrintClient(String h, int p) { super(p); this.host = h; } public void PrintDate() { if(Location == null || Location == "" ) { super.PrintDate(); } else { System.out.println(Location + " no printdate can be viewed"); } } public void connect() { try { Socket sock = new Socket(host,super.getPort()); InputStream in = sock.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); String line; line = bin.readLine(); System.out.println(line); PrintWriter pout = new PrintWriter(sock.getOutputStream(), true); pout.println("It is from Client "); sock.close(); } catch(Exception e) { System.err.println(e); } }
}
import java.io.*; import java.net.*;
public class PrintServer {
private int Port; public int getPort() { return this.Port; } public void setPort(int p) { this.Port = p; } public void listen() { try { ServerSocket sock = new ServerSocket(this.Port); System.out.println("The server is listening....."); Socket client = sock.accept(); PrintWriter pout = new PrintWriter(client.getOutputStream(), true); pout.println("It is from Server "); InputStream in = client.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); System.out.println("And this the message from the client: " + bin.readLine()); client.close(); } catch(Exception e) { System.err.println(e); } } public static void main(String args[]) { PrintServer ps = new PrintServer(); ps.setPort(8888); ps.listen(); } }
public class testClient { public static void main(String[] args) { //System.out.println("Hello world"); //Client cl = new Client(); //cl.PrintDate(); // PrintClient pc = new PrintClient(); // pc.PrintDate(); // // pc.setLocation("POWER130"); // pc.PrintDate(); PrintClient pc = new PrintClient("localhost", 8888); pc.connect(); } }
1. Create a PrintServer.java Class Hint: two useful packages that you can import to your code: i. java.io, ii. java.net 2. Two private member, int Port, and int MaxConnections which is the max number of clients that can connect to the server 3. Listen for socket connection requests server 4. Accept a client's connection 5. Send and receive message from each client (Bi-direction or duplex) Must Print message to show bi-direction responses something simple like server sent received etc. lient Part: 1. Based on three classes from lab1: a. Client.java b. PrintClient.java c. ClientTest.java Hint: three useful packages that you can import to your code i. java.io, ii. java.net, ii. java.util 2. 3. 4. 5. One private member in PrintClient class: String host A new constructor of PrintClient class to initiate the host, and the parent's variable: the port Connect to server Handle connections by sending and receiving messages
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started