Write a Java program to design a protocol where a server is responsible to match
ID: 3637014 • Letter: W
Question
Write a Java program to design a protocol where a server is responsible to match up two chatter clients. The server listens on a TCP port for upcoming connections.1. If no client is already connected to the server to be paired, the server accepts the connecting client, C1, and makes it wait for another client. To do that, it sends a “WAIT” command to C1. When receiving “WAIT”, C1 constructs a ServerSocket instance to listen on port p. Then it sends a message “CLIENT_PORT $p” to the server for future clients, where $p is the port number on which C1 listens for connections.
2. When another client, C2, seeks a connection with the server while C1 is waiting, the server informs C2 the existence of C1 by sending a message “PEER_LOC $h:$p” to C2, where $h is the host name (or IP address) of C1 and $p is the port number on which C1 is waiting. After C2 receives this message, it seeks a connection to C1 using the obtained information.
Clients get the messages from users. The two clients then exchange messages until either party, say C1, sends an end of stream” (Ctrl-D in Linux). In this case, C1 terminates the connection. Then C2 contacts the server. Depending upon the response it receives from the server, it either connects to another client that is currently waiting, or sends “CLIENT_PORT $p” to the server for future clients.
When you design clients, avoid deadlocks where the two clients wait for each other’s messages. This may happen, for example, if both clients invoke readLine() for the inputs from the other clients at the same time. A simple way to avoid deadlock is to require the clients to talk in a strict alternate fashion. Thus, when one client is waiting for message from its peer, the other client should be either sending, or waiting for inputs from the user. More sophisticated method may employ multiple threads, timeouts, etc., and is not required in this assignment.
Explanation / Answer
package bdn; /* The java.net package contains the basics needed for network operations. */ import java.net.*; /* The java.io package contains the basics needed for IO operations. */ import java.io.*; /** The SocketClient class is a simple example of a TCP/IP Socket Client. * */ public interface Process extends java.rmi.Remote { public void process(String arg) throws java.rmi.RemoteExcetion; } public class ProcessImpl extends UnicastRemoteServer implements Process { public ProcessImpl() { super(); } public void process(String arg) throws java.rmi.RemoteExcetion { // Implement the method here } } public class MyServer { public static void main(String args[]) { try { ProcessImpl processImpl = new ProcessImpl(); java.rmi.Naming.bind(“myProcess”, processImpl); } catch(Exception e) { // Exception handling } } }public class MyClient { public static void main(String args[]) { try { Process process = (Process)java.rmi.Naming.lookup(“myProcess”); process.process(“xyz”); } catch(Exception e) { // Exception handling } } } public class OrderManagerBean extends javax.ejb.SessionBean { public Process ejbCreate() throws java.rmi.RemoteExcetion { // Implementation } public void ejbRemove() { // Any cleanup } public void createOrder(...) throws java.rmi.RemoteExcetion { // Implementation } public void amendOrder(...) throws java.rmi.RemoteException { // Implementation } public void ejbActivate() { // After activation } public void ejbPassivate() { // Before activation } public void setSessionContext( javax.ejb.SessionContext ctx) { // Implementation } } public class OrderClient { public static void main(String[] arg) { // 1. Lookup for a proxy object for the home Context context = new InitialContext(); Object obj = context.lookup(“orderManager”); // 2. Cast the home object to OrderManagerHome OrderManagerHome omHome = javax.rmi.PortableRemoteObject.narrow(obj); // 3. Create a remote proxy OrderManager om = omHome.create(); // 4. Invoke methods on the remote proxy om.createOrder(...); // } } Order_Manager OrderManagerHome OrderManager OrderManagerBean Stateless ContainerRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.