Advertisement
TermSpar

Game Server

Mar 25th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.net.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. import java.io.*;
  5.  
  6. public class MainServer {
  7.    
  8.     //GAME SERVER
  9.    
  10.     public static ArrayList<Socket> connectionArray = new ArrayList<Socket>();
  11.     public static ArrayList<String> usersArray = new ArrayList<String>();
  12.    
  13.     public static void main(String args[]) throws IOException{
  14.         try{
  15.             final int PORT = 666;
  16.             ServerSocket SERVER = new ServerSocket(PORT);
  17.             System.out.println("WAITING...");
  18.            
  19.             while(true){
  20.                 Socket userSocket = SERVER.accept();
  21.                 connectionArray.add(userSocket);
  22.                
  23.                 System.out.println("USER: " + userSocket.getLocalAddress().getHostAddress() + " CONNECTED");
  24.                
  25.                 addUser(userSocket);
  26.                
  27.                 //add a client accept thing here once the game client is made -ben
  28.                
  29.             }
  30.            
  31.         }catch(Exception ex){
  32.             ex.printStackTrace();
  33.         }
  34.     }
  35.    
  36.     public static void addUser(Socket X) throws IOException{
  37.         Scanner INPUT = new Scanner(X.getInputStream());
  38.         String userName = INPUT.nextLine();
  39.         usersArray.add(userName);
  40.        
  41.         for(int i = 1; i <= MainServer.connectionArray.size(); i ++){
  42.             Socket TEMP = MainServer.connectionArray.get(i-1);
  43.             PrintWriter OUT = new PrintWriter(TEMP.getOutputStream());
  44.             OUT.println("$:/" + usersArray);
  45.             OUT.flush();
  46.         }
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement