Create a very simple TCP Application in JAVA. You will create a client and a server. The client must execute after you start the server. Here is what they will do.
The client will read from the console a string that the users enters. This string will be the name of the user, let's say Bob. The client then sends the name, Bob, in an Object Stream, to the server. The server will receive this Object Stream and send to the client a nice Object Stream greeting, let's say "Hello Bob, time to play the GUESSING GAME. Bob, please enter a number between 1 and 50." (the server has also kept a secret number to play the game, let's say it is 4).
The client receives this Object Stream, and the user enters a numerical guess, say 5, and the client sends an Object Stream that includes the guessed number, i this case 5. The server receives this Object Stream, and sends an Object Stream, with the string "Too Big, Guess Again", remember, in this example the secret number on the server side is 4. The client receives this Object Stream, and the user enters another number, say 2. The client sends this number as an Object Stream, to the server. The server receives this Object Stream, and realizing that the number is too small, sends a new Object Stream, with the following. "Too small, Guess Again". The client receives this Object Stream, and the user enters a 4. This is sent as an Object Stream to the server, which receives it and sends the Object Stream, "YOU WIN!!!".
At that point, the server and the client terminate. Of course, when the "YOU WIN!!!" Object is sent, you should also include the number of guesses (this running total MUST be stored ONLY on the SERVER, NOT ON THE CLIENT. Please refer to the example code in modules for TCP demos. You should send an object between the client and server which may have the string message, an int for the guess and anything else you think is important (such as the number of guesses).
Part A:
Implement everything as described above, BUT terminate the server and the client when the client finally guesses the secret number.
Example:
Server:
Enter port number: 5555 (from user)
Server is waiting for client to play guessing game ......
Client Name: bob Handling client at /127.0.0.1:53492 with port#
53492
target is: 43
Name: null, data: null guess 30
guess is: 30
Name: null, data: null guess 40
guess is: 40
Name: null, data: null guess 50
guess is: 50
Name: null, data: null guess 45
guess is: 45
Name: null, data: null guess 44
guess is: 44
Name: null, data: null guess 43
guess is: 43
Client (this is interactive with user):
Enter computer name or IP address: 127.0.0.1 (from user)
Enter port number:5555 (from user)
Please Enter your name: bob (from user)
bob, Welcome to the guessing game, please enter a number between 1
and 50
30 (from user)
Too Low! You've guessed 1 times. Please try again.
40 (from user)
Too Low! You've guessed 2 times. Please try again.
50 (from user)
Too High! You've guessed 3 times. Please try again.
45 (from user)
Too High! You've guessed 4 times. Please try again.
44 (from user)
Too High! You've guessed 5 times. Please try again.
43 (from user)
You Win! It took you 6 tries. Game over!
Part B:
Implement everything as described above. Have the server ask the client if the client wishes to play again. If the client wants to play again, repeat the game. If not, terminate both the server and the client. Repeat this until the client does not want to play anymore.
Greetings!!
Please find the solution below.
Happy Learning!!
Part A:
Server.java
import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Server
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
private DataOutputStream out = null;
// constructor with port
public Server(int port)
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Server is waiting for client to play guessing
game .....");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
Random rand = new Random();
boolean over = false;
int num, j;
String msg = "";
String guess = "";
// reads message from client until "Over" is sent
String name = in.readUTF();
System.out.println("Client name "+name+" handleling at
/127.0.0.1:"+port+" with port# "+port);
try
{
j = 0;
num = rand.nextInt(50) + 1;
System.out.println("Target is "+num);
do {
guess = in.readUTF();
if(Integer.parseInt(guess) > num) {
j++;
msg = "To High!, You guessed "+j+" times. Please try agian!";
out.writeUTF(msg);
} else if(Integer.parseInt(guess) < num) {
j++;
msg = "To Low!, You guessed "+j+" times. Please try agian!";
out.writeUTF(msg);
} else {
j++;
msg = "You win!, It took you "+j+" tries. Game over!";
out.writeUTF(msg);
}
System.out.println("Name: "+name+" Data: null Guess:
"+guess);
System.out.println("Guess is "+guess);
}
while(Integer.parseInt(guess) != num);
}
catch(IOException i)
{
System.out.println(i);
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
out.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter port number: ");
int PORT = sc.nextInt();
Server server = new Server(PORT);
}
}
Client.java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataInputStream in = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String num = "";
String msg = "";
// keep reading until "Over" is input
try {
System.out.println("Please enter your name: ");
num = input.readLine();
out.writeUTF(num);
System.out.println("Guess the number between 1 - 50: ");
} catch(IOException i) {
System.out.println(i);
}
while (true)
{
try
{
num = input.readLine();
out.writeUTF(num);
msg = in.readUTF();
System.out.println(msg);
if(msg.contains("win"))
break;
}
catch(IOException i)
{
System.out.println(i);
}
}
// close the connection
try
{
input.close();
in.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter computer name or IP address: ");
String IP = sc.nextLine();
System.out.println("Enter port number: ");
int PORT = sc.nextInt();
Client client = new Client(IP, PORT);
}
}
Part B:
Server.java
import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Server
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
private DataOutputStream out = null;
// constructor with port
public Server(int port)
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Server is waiting for client to play guessing
game .....");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
Random rand = new Random();
boolean over = false;
int num, j;
String msg = "";
String guess = "";
// reads message from client until "Over" is sent
String name = in.readUTF();
System.out.println("Client name "+name+" handleling at
/127.0.0.1:"+port+" with port# "+port);
try
{
while (true) {
j = 0;
num = rand.nextInt(50) + 1;
System.out.println("Target is "+num);
do {
guess = in.readUTF();
if(Integer.parseInt(guess) > num) {
j++;
msg = "To High!, You guessed "+j+" times. Please try agian!";
out.writeUTF(msg);
} else if(Integer.parseInt(guess) < num) {
j++;
msg = "To Low!, You guessed "+j+" times. Please try agian!";
out.writeUTF(msg);
} else {
j++;
msg = "You win!, It took you "+j+" tries. Game over!";
out.writeUTF(msg);
}
System.out.println("Name: "+name+" Data: null Guess:
"+guess);
System.out.println("Guess is "+guess);
}
while(Integer.parseInt(guess) != num);
if(in.readUTF().equals("n"))
break;
}
}
catch(IOException i)
{
System.out.println(i);
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
out.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter port number: ");
int PORT = sc.nextInt();
Server server = new Server(PORT);
}
}
Client.java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataInputStream in = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
Scanner sc = new Scanner(System.in);
String num = "";
String msg = "";
// keep reading until "Over" is input
try {
System.out.println("Please enter your name: ");
num = input.readLine();
out.writeUTF(num);
} catch(IOException i) {
System.out.println(i);
}
while(true) {
System.out.println("Guess the number between 1 - 50: ");
while (true)
{
try
{
num = input.readLine();
out.writeUTF(num);
msg = in.readUTF();
System.out.println(msg);
if(msg.contains("win"))
break;
}
catch(IOException i)
{
System.out.println(i);
}
}
try {
System.out.println("Do you want to play again? (y/n): ");
num = input.readLine();
out.writeUTF(num);
if(num.equals("n")) {
System.out.println("Thank you for playing!");
break;
}
} catch(IOException i) {
System.out.println(i);
}
}
// close the connection
try
{
input.close();
in.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter computer name or IP address: ");
String IP = sc.nextLine();
System.out.println("Enter port number: ");
int PORT = sc.nextInt();
Client client = new Client(IP, PORT);
}
}
Get Answers For Free
Most questions answered within 1 hours.