Java Rock Paper Scissors Client Server Program I need to create a client server
ID: 3758337 • Letter: J
Question
Java Rock Paper Scissors Client Server Program
I need to create a client server program of Rock Paper Scissors. There needs to be TWO Java classes - one to run the server, one to run the client. The client will connect to the server (local connection, "localhost") then the server will play a game of Rock, Paper, Scissors with the client.
Program Guideline:
- The server should establish a connection with the client, then randomly pick Rock, Paper, or Scissors and send their choice to the client.
- The client should ask the player to select Rock, Paper, or Scissors, then receive the choice from the server and find out who’s the winner.
- The client and the server should print out the results from the server’s choice and the client’s choice, as well as who won.
- The client should validate the input of the user, as in if the user inputs anything else other than rock, paper, or scissors, the program should state that the user inputted a incorrect choice and should ask to try again.
- After displaying the winner on both the client’s end and the server’s end, the connection should be terminated, any sockets used should be closed, and both programs should finish running.
- This should be a simple console-based application – no more than words on the screen.
- Please make sure that the program closes any sockets before finishing.
Explanation / Answer
Server.java
import java.io.*;
import java.net.*;
import java.util.logging.*;
public class SocketServer {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inStream = null;
OutputStream outStream = null;
String rcvdMsg = null;
public void serverStart() {
Thread lstnthread = new Thread(){
public void run(){
try {
serverSocket = new ServerSocket(5001);
System.out.println("Server Running");
}
catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
while(true){
try {
socket = serverSocket.accept();
System.out.println("Server connected");
outStream = socket.getOutputStream();
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
while (socket.isConnected()) {
String s = null;
try {
inStream = socket.getInputStream();
byte[] readBuffer = new byte[500];
int num = inStream.read(readBuffer);
if (num > 0) {
byte[] arrayBytes = new byte[num];
System.arraycopy(readBuffer, 0, arrayBytes, 0, num);
rcvdMsg = new String(arrayBytes, "UTF-8");
if (rcvdMsg.contains("QUERY") == true){
// Code for paper scissor here
}
if(s!=null){
outStream.write(s.getBytes("UTF-8"));
break;
}
}
} catch (SocketException se) {
} catch (UnsupportedEncodingException ex) {
8 Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("socket disconnected");
}
}
};
lstnthread.start();
}
public static void main(String[] args){
new SocketServer().serverStart();
}
}
client.java
import java.io.*;
import java.net.*;
public class TCPClient {
public String socketclient(String s,String ip,int port) throws Exception {
OutputStream outStream = null;
InputStream inStream = null;
Socket clientSocket = new Socket(ip, port);
String rcvdMsg = null;
while(clientSocket.isConnected()){
outStream = clientSocket.getOutputStream();
outStream.write(s.getBytes("UTF-8"));
s = null;
inStream = clientSocket.getInputStream();
byte[] readbuffer = new byte[2000];
int num = inStream.read(readbuffer);
if (num > 0)
{
byte[] arrayBytes = new byte[num];
System.arraycopy(readbuffer, 0, arrayBytes, 0, num);
rcvdMsg = new String(arrayBytes, "UTF-8");
if(rcvdMsg!=null)
break;
}
}
outStream.close();
inStream.close();
clientSocket.close();
return rcvdMsg;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.