Skip to content
Snippets Groups Projects
Commit 89eab05e authored by tjsus's avatar tjsus
Browse files

Assignment 2

parent cd71ac9c
No related branches found
No related tags found
No related merge requests found
package MV3500Cohort2024JulySeptember.homework2.Smith;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public Client() {
// default constructor
}
public final static String LOCALHOST = "0:0:0:0:0:0:0:1";
public static void main(String[] args) {
Socket socket = null;
InputStream is;
Reader isr;
BufferedReader br;
PrintWriter pw;
String serverMessage;
int clientLoopCount = 0;
Scanner scanner = new Scanner(System.in);
try {
while (true) {
clientLoopCount++;
System.out.println(Client.class.getName() + " creating socket...");
socket = new Socket(LOCALHOST, 2317);
System.out.println("Enter your choice (rock, paper, or scissors):");
String choice = scanner.nextLine().trim().toLowerCase();
// Send the choice to the server
pw = new PrintWriter(socket.getOutputStream(), true);
pw.println(choice);
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("The message the server sent was: '" + serverMessage + "'");
Thread.sleep(500L);
}
} catch (IOException | InterruptedException e) {
System.err.println("Problem with " + Client.class.getName() + " networking:");
System.err.println("Error: " + e);
if (e instanceof java.net.BindException) {
System.err.println("*** Be sure to stop any other running instances of programs using this port!");
}
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
System.err.println("Error closing socket: " + e);
}
System.out.println();
System.out.println(Client.class.getName() + " exit");
}
}
}
package MV3500Cohort2024JulySeptember.homework2.Smith;
import java.io.*;
import java.net.*;
import java.util.Random;
public class Server {
private static int runningTotal = 0; // Initialize running total
public Server() {
// default constructor
}
public static void main(String[] args) {
try {
System.out.println(Server.class.getName() + " has started...");
ServerSocket serverSocket = new ServerSocket(2317);
OutputStream os;
PrintStream ps;
InetAddress localAddress, remoteAddress;
int localPort, remotePort;
int wins = 0;
int losses = 0;
while (true) {
try (Socket clientConnectionSocket = serverSocket.accept()) {
os = clientConnectionSocket.getOutputStream();
ps = new PrintStream(os);
// Read the number sent by the client
BufferedReader br = new BufferedReader(new InputStreamReader(clientConnectionSocket.getInputStream()));
String clientChoice = br.readLine();
Random rand = new Random();
int serverChoiceInt = rand.nextInt(0,3);
String serverChoiceStr;
serverChoiceStr = switch (serverChoiceInt) {
case 0 -> "rock";
case 1 -> "paper";
case 2 -> "scissors";
default -> "Error";
};
if ("rock".equals(serverChoiceStr) & "paper".equals(clientChoice)){
losses++;
}
if ("paper".equals(serverChoiceStr) & "scissors".equals(clientChoice)){
losses++;
}
if ("scissors".equals(serverChoiceStr) & "rock".equals(clientChoice)){
losses++;
}
if ("rock".equals(serverChoiceStr) & "scissors".equals(clientChoice)){
wins++;
}
if ("scissors".equals(serverChoiceStr) & "paper".equals(clientChoice)){
wins++;
}
if ("paper".equals(serverChoiceStr) & "rock".equals(clientChoice)){
wins++;
}
// Send back the updated total
ps.println("Client chose: " + clientChoice + "/ Server Chose: "+ serverChoiceStr + "/ Wins: "+wins+"/ loses: "+ losses);
ps.println("Please wait for server to choose");
System.out.println("Client chose: " + clientChoice);
localAddress = clientConnectionSocket.getLocalAddress();
remoteAddress = clientConnectionSocket.getInetAddress();
localPort = clientConnectionSocket.getLocalPort();
remotePort = clientConnectionSocket.getPort();
ps.flush();
}
}
} catch (IOException e) {
System.err.println("Problem with " + Server.class.getName() + " networking: " + e);
if (e instanceof java.net.BindException) {
System.err.println("*** Be sure to stop any other running instances of programs using this port!");
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment