Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.Scanner;
- public class MainClient implements Runnable {
- static Socket userSocket;
- private Scanner INPUT;
- private PrintWriter OUT;
- String MESSAGE = "";
- public MainClient(Socket X){
- this.userSocket = X;
- }
- public void checkConnection() throws IOException{
- if(!userSocket.isConnected()){
- for(int i = 1; i<= MainServer.connectionArray.size(); i++){
- if(MainServer.connectionArray.get(i) == userSocket){
- MainServer.connectionArray.remove(i);
- }
- for(int i2 = 1; i2 <= MainServer.connectionArray.size(); i2++){
- Socket TEMP = MainServer.connectionArray.get(i-1);
- PrintWriter OUTw = new PrintWriter(TEMP.getOutputStream());
- OUTw.println(TEMP.getLocalAddress().getHostAddress() + " LEFT");
- }
- }
- }
- }
- public void run(){
- try{
- try{
- INPUT = new Scanner(userSocket.getInputStream());
- OUT = new PrintWriter(userSocket.getOutputStream());
- while(true){
- checkConnection();
- if(!INPUT.hasNext()){
- return;
- }
- MESSAGE = INPUT.nextLine();
- System.out.println("CLIENT - " + MESSAGE);
- for(int i3 = 1; i3 <= MainServer.connectionArray.size(); i3++){
- Socket TEMPs = MainServer.connectionArray.get(i3-1);
- PrintWriter TEMPo = new PrintWriter(TEMPs.getOutputStream());
- TEMPo.println(MESSAGE);
- TEMPo.flush();
- System.out.println("TO - " + TEMPs.getLocalAddress().getAddress());
- }
- }
- }finally{
- userSocket.close();
- }
- }catch(Exception X){
- X.printStackTrace();
- }
- }
- public static void main(String args[]){
- new MainClient(userSocket);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement