Results 1 to 3 of 3

Thread: Help with Java program

  1. #1
    Senior Member
    Join Date
    Jun 2003
    Posts
    772

    Help with Java program

    Can anyone tell me why this program hangs, it doesn't crash and it also doesn't throw an exception or anything, it just hangs. If I define the hostname and port inside the Connect class instead of in the constructor like I do now it doesn't hang. Is this a VM issue? I am testing this by using netcat as a server: nc -L -p 80 -vv .

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.net.*;
    import java.io.*;
    
    public class Crat extends JFrame {
    	
    	JButton connect = new JButton("Connect");
    	JButton send = new JButton("Send some text");
    	Connect conn = new Connect("192.198.1.3", 80);
    	boolean connected = false;
    	
    	public Crat() {
    		super("Crat client");
    		
    		connect.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				if (connected == false) {
    				conn.establishConnection();
    				connected = true;
    			         }
    		         }
    	         });
    		send.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) {
    				if (connected == true)
    				conn.writer.println("Hello world!");
    		         }
    		});
    		 
    		JPanel pane = new JPanel();
    		pane.add(connect);
    		pane.add(send);
    		
    		setContentPane(pane);	
        }
        
        public static void main(String[] args) {
        	JFrame frame = new Crat();
        	
        	WindowListener l = new WindowAdapter() {
       
        		public void windowClosing(WindowEvent e) {
        			System.exit(0);
        			
        		}
            };
            frame.addWindowListener(l);
            
            frame.pack();
            frame.setVisible(true);
        }
    
    }
    
    class Connect {
    	
    	Socket socket = null;
             InputStreamReader isr = null;
    	BufferedReader reader = null;
    	PrintWriter writer = null;
    	String address;
    	int PORTNUM;
    	
    	public Connect(String host, int port) {
    		address = host;
    		PORTNUM = port;
        }
    	
    	void establishConnection() {
    		
    		try {
    			socket = new Socket(address, PORTNUM);
    			isr = new InputStreamReader(socket.getInputStream());
    			reader = new BufferedReader(isr);
    			writer = new PrintWriter(socket.getOutputStream(), true);
    	    } catch (IOException e) {
    	    	System.err.println("Exception: " + e.getMessage());
    	    	System.exit(1);
    	    }
        }
    }
    EDIT: I forgot to mention that it hangs upon clicking the Connect button, also, sorry for the lack of comments, well it's not that this piece of code really needs it but anyway.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  2. #2
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    Sorry, but I can't get it to emulate this behaviour. But then again I (obviously) don't have a crat server running anywhere. However, connecting to google on port 80 produced no hanging.

    EDIT: Trying with netcat also did not reproduce the bug. Must be a VM issue. Get 1.4.2_03

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  3. #3
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    Hehe, the server part is not yet usable.
    Well I just declare the variables in the Connect class so it's not a big deal.
    I have 1.4.2_01-b06, must be the VM indeed.
    Crat stands for Crappy Remote Administration Tool by the way, this is the client as simple as possible and so is the server (still being programmed).
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

Posting Permissions

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