Write a socket server program named "RockPaperScissorsServer", which communicates with players (clients). Server firstly receives a name for the player. Let’s assume 1 is Rock, 2 is Paper and 3 is Scissors. Next Server receives any of the 3 number from the user, generate a random number between 1 to 3 itself and then apply the Rock Paper Scissors game logic (which is paper beats rock, rock beats scissors, scissors beats paper), Then it will tell the player if the player or server has won. Write another client program to communicate with the server.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Topic: Data Communication & Network

Write a socket server program named "RockPaperScissorsServer", which communicates with players (clients). Server firstly receives a name for the player. Let’s assume 1 is Rock, 2 is Paper and 3 is Scissors. Next Server receives any of the 3 number from the user, generate a random number between 1 to 3 itself and then apply the Rock Paper Scissors game logic (which is paper beats rock, rock beats scissors, scissors beats paper), Then it will tell the player if the player or server has won. Write another client program to communicate with the server.

Kindly make the server in such a way the any time any player can leave and another player can join.

Use Java to write the code.

*** Reference code: Creating a simple Server ***

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket;
import java.util.Scanner;

public class Server1 {

private static ServerSocket server = null; private static
Socket socket = null; private static final int port =
8080;
public static void main(String[] args) {

//Create IO Objects BufferedReader in = null; PrintWriter out = null; Scanner consoleInput = new Scanner(System.in);

//Start Server
try { System.out.println("Server is starting ..."); server = new ServerSocket(port); System.out.println("Server has started");
} catch (IOException e) { System.out.println("Can not listen to port: " + port); System.exit(-1);

}
while(true) { //Create Socket
try { socket = server.accept(); System.out.println("Client has been connected\n"); } catch
(IOException e) {
System.out.println("Communication Error with client"); System.exit(-1); }


try { in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );

out = new PrintWriter(socket.getOutputStream(), true);

out.println("NSU CSE338 LAB Server"); System.out.println("Client Name: " + in.readLine());

while(socket.isConnected()) { System.out.print("Server: "); out.println(consoleInput.nextLine()); System.out.print("Client: "); System.out.println(in.readLine()); }

} catch (IOException e) { System.out.print("Client Left"); consoleInput.close(); } }

}


}

 

*** Reference code: Creating a Client ***


import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket;
import java.util.Scanner;

public class Client1 {

public static Socket socket = null;

public static void main(String[] args) {

try { socket = new Socket("localhost", 8080);
System.out.println("Connected to Server\n" + "Socket: " + socket.getInetAddress() + ":" + socket.getPort() + "\n" ); } catch (IOException e) { System.out.print("Connection to network can not be stablished"); }

BufferedReader in = null; PrintWriter out = null; Scanner consoleInput = new Scanner(System.in);

try {

in = new BufferedReader( new InputStreamReader(
socket.getInputStream() ));
out = new PrintWriter(socket.getOutputStream(), true);

System.out.println("Server: " + in.readLine()); System.out.print("Client: your name_ ");
out.println(consoleInput.nextLine());

while(true) { System.out.print("Server: "); System.out.println(in.readLine()); System.out.print("Client: "); out.println(consoleInput.nextLine()); }


} catch (IOException e) {
e.printStackTrace();
} } }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY