Results 1 to 8 of 8
  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Program stops, no Exception thrown

    I am truly mystified by what is happening here, I was attempting to code a client-server program, but after the two have (seemingly) connected, the code always stops at the instant I attempt to read from the client socket's input stream (ClientSide line 60), the program stops, it doesn't go through the next loop iteration or any code after, it is the most unusual thing. If someone could explain why, it would be great. TY

    ServerSide
    Java Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class ServerSide extends JFrame
    {
    	volatile ArrayList<Socket> clientSockets;
    	volatile ArrayList<PrintWriter> clientWriters;
    	String[] ClientNames;
    	ServerSocket serverSocket;
    	Thread acceptThread;
    	Thread runningThread;
    	int count;
    	boolean running=true, paused=false;
    	double now, last;
    	JLabel output;
    	public ServerSide()
    	{
    		//initialize a window with a label for output
    		super("Server");
    		setSize(300,500);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		output = new JLabel("Nothing has happened");
    		getContentPane().add(output);
    		setVisible(true);
    		
    		clientSockets = new ArrayList<Socket>();
    		clientWriters = new ArrayList<PrintWriter>();
    		try {
    			serverSocket = new ServerSocket(3945);	// initialize the server socket with the port 3945
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		// the thread that constantly runs to check for new clients
    		acceptThread = new Thread()
    		{
    			public void run()
    			{
    				Socket newClient = null;
    				try {
    					newClient = serverSocket.accept();
    					clientSockets.add(newClient);
    					System.out.println(newClient);
    					clientWriters.add(new PrintWriter(newClient.getOutputStream(),true));
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		};
    		now = System.nanoTime();
    		last = System.nanoTime();
    		// the thread that runs 
    		runningThread = new Thread()
    		{
    			public void run()
    			{
    				runLoop();
    			}
    		};
    		acceptThread.start();
    		runningThread.start();
    	}
    	public void runLoop()
        {
            final double SECOND = 1000000000;
            // essentially if it is running and not paused, and at least a 10th of a second has passed, write to all the connected clients
            while(running)
            {
                if (paused != true)
                {
                	now = System.nanoTime();
                    if((now - last) > (SECOND/10))
                    {
                    	
                    	count++;
        				output.setText("There are "+clientSockets.size()+" users connected\ncount: "+count);
        				for(int x=0;x<clientSockets.size();++x)
        				{
        					if(clientSockets.get(x) == null)
        					{
        						clientSockets.remove(x);
        						clientWriters.remove(x);
        						clientSockets.trimToSize();
        						clientWriters.trimToSize();
        						continue;
        					}
        					Socket sub = clientSockets.get(x);
        					try {
    							clientWriters.get(x).write("The count is "+count);
    						} catch (Exception e) {
    							e.printStackTrace();
    							//System.exit(1);
    						}
        				}
        				last = System.nanoTime();
                    }
                    else
                    {
                        Thread.yield();
                        try 
                        {
                            Thread.sleep(1);
                        } 
                        catch(Exception e) {} 
                    }
                }
                else
                {
                    Thread.yield();
                    try 
                    {
                        Thread.sleep(1);
                    } 
                    catch(Exception e) {} 
                }
            }
        }
    	public static void main(String[] args)
    	{
    		new ServerSide();
    	}
    }
    ClientSide
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class ClientSide extends JFrame
    {
    	Socket socket = null;
    	JLabel output;
    	boolean paused = false, running = true;
    	double now, last;
    	Thread runningThread;
    	BufferedReader reader;
    	public ClientSide()
    	{
    		super("Client");
    		setSize(400,400);
    		setResizable(false);
    		output = new JLabel("No output Recieved.");
    		getContentPane().add(output);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		
    		//initialize socket
    		try {
    			socket = new Socket(InetAddress.getLocalHost().getHostName(),3945);
    			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		} catch (Exception e) {
    			output.setText("Connect Attempt Failed: "+e.getMessage());
    		}
    		now = System.nanoTime();
    		last = System.nanoTime();
    		runningThread = new Thread()
    		{
    			public void run()
    			{
    				runLoop();
    			}
    		};
    		output.setText("Thread started");
    		runningThread.start();
    	}
    	public void runLoop()
        {
    		final double SECOND = 1000000000;
    		output.setText("In thread");
            while(running)
            {
                if (paused != true)
                {
                	now = System.nanoTime();
                    if((now - last) > (SECOND/10))
                    {
                    	output.setText("and it stopped");
                    	try {
                			output.setText("time: "+reader.readLine());	//!! Problem code
                		} catch (IOException e1) {
                			output.setText("Something Failed: "+e1.getMessage());
                		}
                    	last = System.nanoTime();
                    }
                    else
                    {
                        Thread.yield();
                        try 
                        {
                            Thread.sleep(1);
                        } 
                        catch(Exception e) {} 
                    }
                    
                }
                else
                {
                    Thread.yield();
                    try 
                    {
                        Thread.sleep(1);
                    } 
                    catch(Exception e) {} 
                }
            }
        }
    	public static void main(String[] args)
    	{
    		new ClientSide();
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default Re: Program stops, no Exception thrown

    Try to flush() the writer after you have written to the socket.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Program stops, no Exception thrown

    If that flush doesn't do anything - this is regular IO which is always blocking. So if a readLine() call blocks, that means there either isn't data to read, or there was data to read but it did not have a newline.

  4. #4
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Program stops, no Exception thrown

    It turns out it needed to be flushed, it works now ty!

  5. #5
    derekxec is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Program stops, no Exception thrown

    just checking out your code i was wondering....you set paused to false then no where in the code do you change it to true that i can see

    if (paused != true)
    lines 75 server code and 54 client code
    this line will always run since paused will always be false so why do you need the if statement? how come you dont change running to false instead of always making it true?

  6. #6
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Program stops, no Exception thrown

    Well paused was meant to make it so that the game could be stopped from running, without ending the while loop and stopping the program completely, I actually just fixed it so that it will accept multiple clients even across multiple machines in my LAN, they don't do anything just yet, but I consider it a personnel accomplishment, but I still cant seem to figure out why isConnected and isClosed still seem to return true even after the client which that socket was connected to was closed... I still need to fiddle with it.

  7. #7
    derekxec is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Program stops, no Exception thrown

    ahh ok i hope you didnt take my post as bad i was just wondering why you did it a certain way :D

  8. #8
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Program stops, no Exception thrown

    oh no no no, I took no offense, but I am really confused by why isConnected is returning true and isClosed are returning false, I even modified the clientside program so that it manually closes the socket, yet over on the server side it still returns true... I am really bewildered by this, because earlier I had been closing the input stream of the socket, which causes the socket to close as well, and caused turmoil on the server side, yet now it does not... odd

    Edit: finaly just up and googled and wouldn't ya know it found this- http://stackoverflow.com/questions/2...d-server-sides , so now im gonna make it so that it sends a close command before closing on the client side.
    Last edited by PRW56; 06-22-2013 at 04:09 AM.

Similar Threads

  1. wrongly thrown exception
    By dawnMist in forum New To Java
    Replies: 1
    Last Post: 11-29-2012, 01:24 PM
  2. Replies: 3
    Last Post: 05-02-2011, 08:02 PM
  3. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  4. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •