In this assignment, you’ll write a server and client program. There are two clie
ID: 3802914 • Letter: I
Question
In this assignment, you’ll write a server and client program. There are two clients, X and Y (both essentially identical) that will communicate with a server. Clients X and Y will each open a TCP socket to your server and send a message to your server. The message contains the name of the client followed by your name (e.g., “Client X: Alice”, “Client Y: Bob”).
The server will accept connections from both clients and after it has received messages from both X and Y will print their messages and then send an acknowledgment back to your clients. The acknowledgment from the server should contain the sequence in which the client messages were received (“X: Alice received before Y: Bob”, or “Y: Bob received before X: Alice”). After the server sends out this message it should output a message saying - “Sent acknowledgment to both X and Y”. Your server can then terminate. Once your clients receive the message from the server, they should print the message that they sent to the server, followed by the reply received from the server. The following figure explains the problem.
Your program should print out the messages sent by the client at both the client and server and vice versa for messages sent by the server.
Explanation / Answer
import java.net.*;
import java.io.*;
import java.util.Date;
import java.util.Random;
public class Server {
static Random random = new Random();
static int id1=897;
static int id2=1002;
public static final int LISTENING_PORT = 32007;
public static void main(String[] args) {
ServerSocket listener; // Listens for incoming connections.
Socket connection1; // For communication with the connecting program.
Socket connection2;
try {
listener = new ServerSocket(LISTENING_PORT);
System.out.println("Listening on port " + LISTENING_PORT);
// Accept next connection request and handle it.
connection1 = listener.accept();
BufferedReader incoming1 = new BufferedReader(
new InputStreamReader(connection1.getInputStream()) );
String lineFromClient1 = incoming1.readLine();
System.out.println(lineFromClient1);
connection2 = listener.accept();
BufferedReader incoming2 = new BufferedReader(
new InputStreamReader(connection2.getInputStream()) );
String lineFromClient2 = incoming2.readLine();
System.out.println(lineFromClient2);
if(lineFromClient1.contains("Alice"))
{
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( connection1.getOutputStream() );
outgoing.println("X: Alice received before Y: Bob" );
outgoing.flush(); // Make sure the data is actually sent!
outgoing = new PrintWriter( connection2.getOutputStream() );
outgoing.println("X: Alice received before Y: Bob" );
outgoing.flush(); // Make sure the data is actually sent!
}
else
{
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( connection1.getOutputStream() );
outgoing.println("Y: Bob received before X: Alice" );
outgoing.flush(); // Make sure the data is actually sent!
outgoing = new PrintWriter( connection2.getOutputStream() );
outgoing.println("Y: Bob received before X: Alice" );
outgoing.flush(); // Make sure the data is actually sent!
}
System.out.println("Sent acknowledgment to both X and Y");
connection1.close();//closing everything
connection2.close();
listener.close();
}
catch (Exception e) {
System.out.println("Sorry, the server has shut down.");
System.out.println("Error: " + e);
return;
}
} // end main()
} //end class
----------------------
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class ClientAlice {
public static final int LISTENING_PORT = 32007;
public static void main(String[] args) {
String hostName="localhost"; // Name of the server computer to connect to.
Socket connection; // A socket for communicating with server.
BufferedReader incoming; // For reading data from the connection.
String msg="Client X:Alice";
/* Make the connection, then read and display a line of text. */
try {
connection = new Socket( hostName, LISTENING_PORT );
// The current date and time.
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( connection.getOutputStream() );
outgoing.println(msg);
outgoing.flush(); // Make sure the data is actually sent!
incoming = new BufferedReader(
new InputStreamReader(connection.getInputStream()) );
String lineFromServer = incoming.readLine();
if (lineFromServer == null) {
// A null from incoming.readLine() indicates that
// end-of-stream was encountered.
throw new IOException("Connection was opened, " +
"but server did not send any data.");
}
System.out.println();
System.out.println("Sent: "+msg);
System.out.println("Receieved: "+lineFromServer);
incoming.close();
}
catch (Exception e) {
System.out.println("Error: " + e);
}
} // end main()
}
------------------------
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class ClientBob {
public static final int LISTENING_PORT = 32007;
public static void main(String[] args) {
String hostName="localhost"; // Name of the server computer to connect to.
Socket connection; // A socket for communicating with server.
BufferedReader incoming; // For reading data from the connection.
String msg="Client Y:Bob";
/* Make the connection, then read and display a line of text. */
try {
connection = new Socket( hostName, LISTENING_PORT );
// The current date and time.
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( connection.getOutputStream() );
outgoing.println(msg);
outgoing.flush(); // Make sure the data is actually sent!
incoming = new BufferedReader(
new InputStreamReader(connection.getInputStream()) );
String lineFromServer = incoming.readLine();
if (lineFromServer == null) {
// A null from incoming.readLine() indicates that
// end-of-stream was encountered.
throw new IOException("Connection was opened, " +
"but server did not send any data.");
}
System.out.println("Sent: "+msg);
System.out.println("Receieved: "+lineFromServer);
incoming.close();
}
catch (Exception e) {
System.out.println("Error: " + e);
}
} // end main()
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.