Created
November 30, 2013 23:53
-
-
Save hemmrich/7726234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eecs285.siegegame; | |
import java.io.BufferedReader; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.Socket; | |
public class MiniServer extends Thread { | |
private Socket socket = null; | |
String playerName; | |
int clientNum; | |
BufferedReader iStream; | |
DataOutputStream oStream; | |
public MiniServer(Socket socket, int numClient) { | |
super("MiniServer"); | |
clientNum = numClient; | |
this.socket = socket; | |
} | |
public void run() { | |
// Announce connection of player | |
System.out.println("Player " + clientNum + " Connected"); | |
// get IO functionality to client | |
try { | |
DataInputStream is = new DataInputStream(socket.getInputStream()); | |
iStream = new BufferedReader(new InputStreamReader(is)); | |
oStream = new DataOutputStream(socket.getOutputStream()); | |
} catch (IOException e) { | |
System.out.println("ERROR: getting InputStream or OutputStream"); | |
} | |
// first, get name from user | |
// Sorry that this part of the code is so confusing/ugly. | |
// In short, all it does is determine whether the string sent to | |
// the server contains "NAME", as that is how I specified that a name | |
// is being sent to the server | |
String data; | |
boolean namePicked = false; | |
while (!namePicked) { | |
if ((data = getInput(iStream)) != null) { | |
String usableData = ""; | |
for (int i = 0; i < data.length(); i++) { | |
if (Character.isLetterOrDigit(data.charAt(i))) | |
usableData += data.charAt(i); | |
} | |
if (usableData.contains("NAME")) { | |
usableData = usableData.substring(4); | |
Server.playerNames[clientNum] = usableData; | |
if (isNameTaken(usableData)) | |
System.out.println("Name taken result = true"); | |
playerName = usableData; | |
System.out.println("Player " + clientNum | |
+ " has changed their name to " + usableData); | |
broadcastToClients("Player " + clientNum | |
+ " has changed their name to " + usableData + '\n'); | |
namePicked = true; | |
} | |
} | |
} | |
while (true) { | |
// if data has been received from client | |
if ((data = getInput(iStream)) != null) { | |
System.out.println(data); | |
data += '\n'; | |
broadcastToClients(data); | |
} | |
} | |
} | |
// helpful methods | |
public String getInput(BufferedReader istream) { | |
String receivedData = null; | |
try { | |
receivedData = istream.readLine(); | |
} catch (IOException e) { | |
System.out.println("ERROR: getting data"); | |
System.exit(-1); | |
} | |
return receivedData; | |
} | |
public void broadcastToClients(String data) { | |
for (int i = 0; i < Server.allClients.length; i++) { | |
if (Server.allClients[i] == null) | |
continue; | |
try { | |
Server.allClients[i].oStream.writeChars(data); | |
} catch (Exception e) { | |
System.out.println("ERROR: writing to server: " + i); | |
} | |
} | |
} | |
// Server.playerNames[] should contain the input string only | |
// once if the name hasn't been picked before | |
public static boolean isNameTaken(String input) { | |
int numOccurences = 0; | |
System.out.println("in isNameTaken"); | |
System.out.println(" input = " + input); | |
for (int i = 0; i < Server.MAX_PLAYERS; i++) { | |
System.out.println(" i = " + i); | |
System.out.println(" playerNames[i] = " + Server.playerNames[i]); | |
if (Server.playerNames[i].contentEquals(input)) { | |
numOccurences++; | |
} | |
} | |
if (numOccurences > 1) // this instance of the name will be in the array | |
return true; | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eecs285.siegegame; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class Server { | |
static MiniServer[] allClients; //contains all connected clients | |
static String[] playerNames = {"Player 0", "Player 1", "Player 2", "Player 3"}; //contains all player names | |
static boolean connectionAllowed = true; //if additional players can join | |
static int numPlayer = 0; | |
final static int MAX_PLAYERS = 4; | |
public static void main(String[] args) throws IOException { | |
ServerSocket serverSocket = null; | |
try { | |
serverSocket = new ServerSocket(45000); | |
} catch (IOException e) { | |
System.err.println("Could not listen on port: 45000"); | |
System.exit(-1); | |
} | |
allClients = new MiniServer[MAX_PLAYERS]; | |
while(connectionAllowed && numPlayer < MAX_PLAYERS) { | |
//accept connection and start a new MiniServer for that client | |
Socket clientSocket = serverSocket.accept(); | |
MiniServer server = new MiniServer(clientSocket, numPlayer); | |
allClients[numPlayer] = server; | |
server.start(); | |
System.out.println("Server " + numPlayer + " started"); | |
numPlayer++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment