We’ve looked at how to create a Client/Server connection, how to send data, and
ID: 3812136 • Letter: W
Question
We’ve looked at how to create a Client/Server connection, how to send data, and how to host multiple Clients. Now we’re going to put everything together by creating a program in which two users can interact with each other.
First, the setup. Create a Server and a Client. The Server will accept two Clients when it starts. Then, we’re going to assign an ID to both Clients. Once we have this setup, the Server is going to play a guessing game with the Clients. The Server will randomly pick a number between 1 and 10, and then it will get a guess from each Client. Each Client will accept a numeric entry between 1 and 10 from the user and send their entry to the Server. Don’t forget to use Data Validation and ask for a new entry without terminating the program if invalid data is entered. When the Server has received both entries, it will determine a winner and send a confirmation to both Clients. Everything will then finish and disconnect.
While this may sound complex, there are several ways to keep this program simple. Once both IDs have been sent, get an integer from each Client and have the Server receive them one at a time. Then, compare both numbers to the chosen number and decide which player won or if it is a tie (in the case that both players’ choices are the same distance from the server’s chosen number – i.e. if the Server chose 5 and the users chose 3 and 7). Create a String that describes the outcome of the game like shown below and send that to both Clients. This will be enough to finish execution.
“Player 1 chose 2. Player 2 chose 7. The Server chose 3. Player 1 wins!”
To summarize, your program should do the following:
• Create a Server class and a Client class. Have two Clients connect to the Server.
• Have the Server send an ID to both Clients.
• Have the Server choose a random number from 1 to 10 for a guessing game.
• Allow both Clients to get user-input in the form of a number.
• Perform entry validation to ensure that each entry is an integer between 1 and 10, prompting for a re-entry without terminating the program if the input is invalid.
• Send both user entries back to the Server.
• Determine which user chose a number closest to the Server’s target number.
• Send a String with an outcome message to both Clients.
• Disconnect and close everything.
Explanation / Answer
Answer:
Server side code:
package com.Ser.GuessSocket1;
import java.io.IOException;import java.i.ObjectInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Server
{
private ObjectInputSteam input;
private ObjectOutputStream output;
private Socket connection;
private ServerSocket server;
private int cunter=0;
public vid startRunning()
{
try
{
server=new ServerSocket(53);
while(true)
{
waitForConnection();
}
}
catch(Exception e)
{
}
}
public void waitForCnnection() throws IOException
{
System.out.println("waiting for cnnection.... ");
connection=server.accept();
new Thread(new Worked()).start();
counter++;
System.out.println("cnnectin"+counter+"recieved from:"+connection.getInetAddress().getHostName());
}
public class Worker implements Runnable
{
private int guess;
private final int target;
private final Random ran;
public Worker()
{
ran=new Random();
target=ran.nextInt(10)+1;
}
public void processConnection()
{
try
{
guess=input.readInt();
System.out.println("client guessed-"+guess);
}
catch(Exception e)
{
}
}
public void setUpStreams() throws IOException
{
output=new objectOutputStream(connectin.getOOutputStream());
output.flush();
input=new ObjectInputStream(connectin.getInputStream());
}
public void askForGuess() throws IOException
{
sendMessage("please type in a guess ");
System.out.println("client guessed:"+guess);
}
public void checkGuess() throws IOException
{
while(guess!=target)
{
sendMessage("you guesses wrong,plese try again ");
askForGuess();
}
sendMessage("you wn! ");
}
public void sendMessage(string message) throws IEception
{
output.writeObject(message);
output.flush();
}
@Override
public void run()
{
try
{
setUpStreams();
processConnection();
askForGuess();
checkGuess();
}
catch(IOException e)
{
//todo Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])
{
Server s=new Server();
s.startRunning();
}
}
client java code:
package com.Ser.guessSocket1;
import java.io.IException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client
{
private int guess;
Scanner scan;
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
public Client()
{
scan=new Scanner(System.in);
}
public void startRunning()
{
try
{
connectToServer();
setUpStreams();
processConnection();
}
catch(Exception e)
{
}
}
public void ConnectToServer()
{
try
{
System.out.println("Attempting connection...");
connection=new Socket(InetAddress.getByName("localhost"),53);
}
catch(Exception e)
{
System.out.println("connot find server");
}
}
public void setUpStreams() throws IOException
{
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
System.ut.println("Got streams");
}
public void processConnection()
{
String message="you are now connected";
System.out.println(message);
}
catch(Exception e)
{
}
}
public void sendGuess(int guess1) throws IOException
{
output.writeInt(guess1);
output.flush();
}
public static void main(String args[])
{
Client c=new Client();
c.startRunning();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.