Results 1 to 2 of 2
  1. #1
    AMaineJR is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default Thread location problem?

    I got a client/server program setup to connect and send alphanumeric data back and forth. Within the same program, I want to click a button to start a thread for capturing the cursor's x and y coordinates. I can get it to work at times, depending on where I place the thread object. Can someone look at what I've got give me a tip? The error I am getting is Line 181 in SocketClient.java (the last line in the code section). I uploaded the entire code for both the server and client if anyone needs it, but I'm posting the Client file, so you can see what I'm looking at.

    I think my problem is the location and implementation of the thread object, because it doesn't have access to the proper socket out stream. In C++, I'd pass a pointer, but Java doesn't do that. The actual problem is, that when I click the button, the client sends the server the location of the mouse at that instance, but then begins printing error to the console.


    Java Code:
    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.MouseInfo;
    import java.awt.event.*;
    import javax.swing.*;
    
    import java.io.*;
    import java.net.*;
    
    class SocketClient extends JFrame implements ActionListener {
    
       JLabel text, clicked;
       JButton button;
       JButton mouseToggle;
       JPanel panel;
       JPanel buttonsPanel;
       JTextField textField;
       Socket socket = null;
       PrintWriter out = null;		//Client Socket Output to Server
       BufferedReader in = null;	//Client Socket Input from Server
       boolean runMouse = false;	//Control variable for thread
       Thread mouseWatcher;			//Thread to mouseMotion class
       String host;
       int port;
    
    
       SocketClient()
       { //Begin Constructor
    	   host = "localhost";
    	   port = 4444;
    	   
    	   text = new JLabel("Text to send over socket:");
    	   textField = new JTextField(20);
    	   button = new JButton("Send Text");
    	   mouseToggle = new JButton("Toggle Mouse");
    	   mouseToggle.addActionListener(this);
    	   button.addActionListener(this);
    
    	   panel = new JPanel();
    	   buttonsPanel = new JPanel();
         
    	   panel.setLayout(new BorderLayout());
    	   panel.setBackground(Color.white);
    	   buttonsPanel.setLayout(new FlowLayout());
    	   buttonsPanel.setBackground(Color.white);
    	   getContentPane().setLayout(new GridLayout(2,1));
    	   getContentPane().add(panel);
    	   getContentPane().add(buttonsPanel);
         
    	   panel.add("North", text);
    	   panel.add("Center", textField);
         
    	   buttonsPanel.add(button);
    	   buttonsPanel.add(mouseToggle);
       } //End Constructor
    
    
      public void actionPerformed(ActionEvent event)
      {
    	  Object source = event.getSource();
    
    	  if(source == button)
    	  {
    		  //Send data over socket
              String text = textField.getText();
              out.println(text);
              textField.setText(new String(""));
    
              //Receive text from server
              try
              {
            	  String line = in.readLine();
            	  System.out.println("Text received :" + line);
              } 
              catch (IOException e)
              {
            	  System.out.println("Read failed");
            	  System.exit(1);
              }
         }
         
         if(source == mouseToggle)
         {
             if (runMouse == false)
             {
            	 System.out.println("Mouse Capture Started");
            	 mouseToggle.setText("Mouse On");
            	 runMouse = true;
            	 mouseWatcher.start();
             }
             else
             {
           	  	try
           	  	{
           	  		System.out.println("Mouse Capture Stopped");
           	  		mouseToggle.setText("Mouse Off");
           	  		runMouse = false;
           	  		mouseWatcher.wait(2000);
           	  	}
           	  	catch (Exception e)
           	  	{
           	  		System.out.println("Error in Mouse Capture Routine");
           	  		mouseToggle.setText("Mouse Error");
           	  		runMouse = true;
           	  	}
             }
         }
    }
      
      public void listenSocket()
      {
    	  //Create socket connection
    	  try
    	  {
    		  socket = new Socket(host, port);
    		  out = new PrintWriter(socket.getOutputStream(), true);
    		  in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    	  } 
    	  catch (UnknownHostException e) 
    	  {
    		  System.out.println("Unknown host: " + host);
    		  System.exit(1);
    	  } 
    	  catch  (IOException e) 
    	  {
    		  System.out.println("I/O Error");
    		  System.exit(1);
    	  }
      }
      
      public static void main(String args[])
      {
    	  	SocketClient frame = new SocketClient();
          	frame.setTitle("Client Program");
          	WindowListener l = new WindowAdapter() 
          	{
          		public void windowClosing(WindowEvent e) 
          		{
                              System.exit(0);
          		}
          	};
          	frame.addWindowListener(l);
          	frame.pack();
          	frame.setVisible(true);
          	frame.listenSocket();
          	frame.mouseWatcher = new Thread(new MouseMotion(frame.out));
      }
    }
    //END OF SocketClient CLASS
    
    
    //BEGIN MouseMotion Thread CLASS
    class MouseMotion implements Runnable
    {
    	PrintWriter output;
    
    	public MouseMotion()
    	{
    	}
    	
    	public MouseMotion(PrintWriter socketOut)
    	{  //This passes the Client Output Socket to the Thread Class
    		output = socketOut;
    	}
    	
    	public void run()
    	{
    		while (true)
    		{
    			try
    			{
    				//Send Mouse Coordinates to Client Output Socket
    				output.println(MouseInfo.getPointerInfo().getLocation().x + "," + MouseInfo.getPointerInfo().getLocation().y);
    				this.wait(200);
    			}
    			catch (Exception e)
    			{
    				//THIS IS THE ERROR THAT IS SHOWING UP
    				System.out.println("Error");
    			}
    		}
    	}
    }
    Attached Files Attached Files
    Last edited by AMaineJR; 10-01-2010 at 03:29 AM. Reason: Updating Problem

  2. #2
    AMaineJR is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    I forgot to mention I'm not 100% comfortable with threads. Also, if I don't use a thread, I can place a while loop and sleep method within the actionlistener to perform the update, however, the button doesn't respond to another click. Which is why I assumed to use a thread.

Similar Threads

  1. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  2. Problem with a thread
    By dotabyss in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-14-2010, 10:56 PM
  3. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM
  4. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 02:24 AM
  5. Problem here guys: Set location or setBounds
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-03-2008, 07:19 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
  •