Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.*;
- class GameGlobals{
- static boolean isPlayable = true;
- static int playerLeft, playerCenter, playerRight;
- static int clientCount = 0;
- }
- class Server{
- public static void main (String[] args) {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(8080);
- } catch (IOException e) {
- System.out.println("Could not listen on port 8080, " + e);
- System.exit(1);
- }
- Socket clientSocket1 = null;
- Socket clientSocket2 = null;
- try {
- System.out.println("Waiting for players (0/2).");
- clientSocket1 = serverSocket.accept();
- System.out.println("Waiting for players (1/2).");
- clientSocket2 = serverSocket.accept();
- System.out.println("All players connected (2/2).");
- } catch (IOException e) {
- System.out.println("Accept failed, " + e);
- System.exit(1);
- }
- Serving serving1 = new Serving(clientSocket1);
- Serving serving2 = new Serving(clientSocket2);
- GameSetup game = new GameSetup();
- GameSetup.Enemy currEnemy = null;
- currEnemy = game.new Enemy();
- serving1.start();
- serving2.start();
- boolean isPlayable = true;
- while (GameGlobals.isPlayable) {
- try {
- Thread.sleep(game.SPAWN_TIME);
- currEnemy = game.new Enemy();
- for(int i = 0; i < GameGlobals.clientCount; i++) {
- System.out.println("ENEMY " + currEnemy);
- serving1.os[i].println("ENEMY " + currEnemy);
- }
- } catch (InterruptedException e) {}
- }
- try {
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class Serving extends Thread {
- Socket clientSocket;
- static boolean isPlayable = true;
- public static PrintStream os[] = new PrintStream[2];
- Serving (Socket clientSocket) {
- this.clientSocket = clientSocket;
- }
- public synchronized void run() {
- try {
- os[GameGlobals.clientCount++] = new PrintStream (clientSocket.getOutputStream(), true);
- while(GameGlobals.isPlayable);
- for(int i = 0; i < GameGlobals.clientCount; i++) {
- os[i].close();
- }
- clientSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (NoSuchElementException e) {
- System.out.println("Connection closed by client.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement