Question

Create a very simple TCP Application in JAVA. You will create a client and a server....

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.

Homework Answers

Answer #1

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);
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The goal of this assignment is to implement a simple client-server system. You have to use...
The goal of this assignment is to implement a simple client-server system. You have to use Python. The basic functionality of the system is a remote calculator. You first will be sending expressions through the client to the server. The server will then calculate the result of the expression and send it back to the client where it is displayed. The server should also send back to the client the string "Socket Programming" as many times as the absolute value...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
***Programming language is Java. After looking at this scenario please look over the requirements at the...
***Programming language is Java. After looking at this scenario please look over the requirements at the bottom (in bold) THIS IS ALL THAT WAS PROVIDED. PLEASE SPECIFY ANY QUESTIONS IF THIS IS NOT CLEAR (don't just say more info, be specific)*** GMU in partnership with a local sports camp is offering a swimming camp for ages 10-18. GMU plans to make it a regular event, possibly once a quarter. You have been tasked to create an object-oriented solution to register,...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...