Advertisement
TermSpar

Game Client

Mar 25th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.Scanner;
  4.  
  5. public class MainClient implements Runnable {
  6.     static Socket userSocket;
  7.     private Scanner INPUT;
  8.     private PrintWriter OUT;
  9.     String MESSAGE = "";
  10.    
  11.     public MainClient(Socket X){
  12.         this.userSocket = X;
  13.     }
  14.    
  15.     public void checkConnection() throws IOException{
  16.         if(!userSocket.isConnected()){
  17.             for(int i = 1; i<= MainServer.connectionArray.size(); i++){
  18.                 if(MainServer.connectionArray.get(i) == userSocket){
  19.                     MainServer.connectionArray.remove(i);
  20.                 }
  21.                
  22.                 for(int i2 = 1; i2 <= MainServer.connectionArray.size(); i2++){
  23.                     Socket TEMP = MainServer.connectionArray.get(i-1);
  24.                     PrintWriter OUTw = new PrintWriter(TEMP.getOutputStream());
  25.                     OUTw.println(TEMP.getLocalAddress().getHostAddress() + " LEFT");
  26.                 }
  27.             }
  28.         }
  29.     }
  30.    
  31.     public void run(){
  32.         try{
  33.             try{
  34.                 INPUT = new Scanner(userSocket.getInputStream());
  35.                 OUT = new PrintWriter(userSocket.getOutputStream());
  36.                
  37.                 while(true){
  38.                     checkConnection();
  39.                    
  40.                     if(!INPUT.hasNext()){
  41.                         return;
  42.                     }
  43.                    
  44.                     MESSAGE = INPUT.nextLine();
  45.                     System.out.println("CLIENT - " + MESSAGE);
  46.                    
  47.                     for(int i3 = 1; i3 <= MainServer.connectionArray.size(); i3++){
  48.                         Socket TEMPs = MainServer.connectionArray.get(i3-1);
  49.                         PrintWriter TEMPo = new PrintWriter(TEMPs.getOutputStream());
  50.                         TEMPo.println(MESSAGE);
  51.                         TEMPo.flush();
  52.                         System.out.println("TO - " + TEMPs.getLocalAddress().getAddress());
  53.                     }
  54.                 }
  55.             }finally{
  56.                 userSocket.close();
  57.             }
  58.            
  59.         }catch(Exception X){
  60.             X.printStackTrace();
  61.         }
  62.     }
  63.    
  64.     public static void main(String args[]){
  65.         new MainClient(userSocket);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement