Hello all,
I seem to be having a problem with printwriter, it sends to the server the first time but then i cannot get it to send anything else (particularly the disconnect function).
import java.net.*; import java.io.*; import java.util.*; public class A_Chat_Client implements Runnable{ Socket SOCK; Scanner INPUT; Scanner SEND = new Scanner(System.in); PrintWriter OUT; public A_Chat_Client(Socket X) { this.SOCK = X; } public void run() { try { try { INPUT = new Scanner(SOCK.getInputStream()); OUT = new PrintWriter(SOCK.getOutputStream()); OUT.flush(); CheckStream(); } finally { SOCK.close(); } }catch(Exception e) { e.printStackTrace(); } } public void DISCONNECT() { try { SEND("CLOSE"); // <------ this does not send SOCK.close(); System.exit(0); } catch(Exception e) { System.out.println("Error: " +e); } } public void CheckStream() { while(true) { RECEIVE(); } } public void RECEIVE() { if(INPUT.hasNext()) { String MESSAGE = INPUT.nextLine(); if(MESSAGE.contains("ST,")) { String[] reFeed = MESSAGE.split(","); if(reFeed[0].contains("ST")) { switch (reFeed[1]) { case "1": //Game has not started yet, lets get them in! Bongo.LOC_Lobby(); break; case "2": //In Prep Time, Still Time To Get In! Bongo.LOC_GameSetUp(); break; default: //Game has started so lets throw them in purgatory Bongo.LOC_Wait(); break; } } } else { System.out.println(""+MESSAGE); //A_Chat_Client_GUI.TA_CONVERSATION.append(MESSAGE + "\n"); } } } public void SEND(String X) throws IOException { OUT.println(Bongo.UserName + "," + X); OUT.flush(); } }
I do a check in the server with a simple System.out.println to tell me when the server receives anything from the client side but nothing..
The DISCONNECT function is called from here:
MainWindow.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { System.out.println("Gonna Disconnect!"); ChatClient.DISCONNECT(); } catch (IOException ex) { System.out.println(Error: "+e+"\n"); } } });
and this is the server side:
package gameserver; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Scanner; public class GameServer { public static ArrayList<Socket> ConnectionArray = new ArrayList<>(); public static ArrayList<String> CurrentUsers = new ArrayList<>(); public static ArrayList<Socket> NGSocks = new ArrayList<>(); public static ArrayList<String> NGUsers = new ArrayList<>(); public static void main(String[] args) { GameFlow gf = new GameFlow(); new Thread(gf).start(); try { final int PORT = 444; ServerSocket SERVER = new ServerSocket(PORT); System.out.println("Waiting for Clients"); while(true) { Socket SOCK = SERVER.accept(); System.out.println("Client Connected from : " + SOCK.getLocalAddress().getHostName()); Scanner INPUT = new Scanner(SOCK.getInputStream()); String UserName = INPUT.nextLine(); String[] InFeed = UserName.split(","); System.out.println("Got Here!! "+ InFeed[1] + "\n"); if(InFeed[1].equals("START")) { System.out.println("START envoked!!\n"); if(GameFlow.getMode() == 3) { AddUserNextGame(InFeed[0], SOCK); } else { AddUserName(InFeed[0], SOCK); } PrintWriter OUT = new PrintWriter(SOCK.getOutputStream()); OUT.println("ST," + GameFlow.getMode() + "," + GameFlow.getTime()); OUT.flush(); } else if (InFeed[1].equals("CLOSE")) { for(int i = 1; i <= GameServer.CurrentUsers.size(); i++) { if(CurrentUsers.get(i-1).equals(InFeed[1])) { CurrentUsers.remove(i-1); ConnectionArray.remove(i-1); } } } } }catch(Exception e) { System.out.println(e); } }
thanks for any help...