Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: PrintWriter out problem

  1. #1
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default PrintWriter out problem

    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...
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!


  2. #2
    Junior Member Randor's Avatar
    Join Date
    Nov 2012
    Location
    Olean, NY
    Posts
    11
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: PrintWriter out problem

    I FINALLY figured this out:

    I needed to just move thise code:
                        Socket SOCK = SERVER.accept();
     
                        System.out.println("Client Connected from : " + SOCK.getLocalAddress().getHostName());
     
                        Scanner INPUT = new Scanner(SOCK.getInputStream());
    outside the while true loop..
    When I die, I want it to be peacful and in my sleep, like my grandfather. Not flailing and screaming like the people in his car!!

Similar Threads

  1. PrintWriter: passing File reference vs String
    By kahwa in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 12th, 2012, 12:56 AM
  2. Null Exception Error & Help with PrintWriter
    By willo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 29th, 2011, 08:08 AM
  3. [SOLVED] Printwriter class: displaying File contents to screen with included formatting?
    By mwebb in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: November 25th, 2011, 10:38 AM
  4. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 02:42 PM
  5. Problems with PrintWriter
    By dbridle in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 29th, 2010, 03:51 PM