Results 1 to 3 of 3

Thread: Need help again with Java program.

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

    Need help again with Java program.

    If you are wondering what program it's about, see this thread: http://www.antionline.com/showthread...hreadid=260759

    I'm adding a feature to take a screenshot on a system and transfer the image to the computer were Crat (the client part) runs.

    In Crat.java there is a class Connect (some formatting is lost):

    Code:
    class Connect {                           //class which is used for the connection. Only for txt streams
                                              //host (address) is hardcoded, this will change, it's just a detail
    	
    	Socket socket = null;
             InputStreamReader isr = null;
    	BufferedReader reader = null;
    	PrintWriter writer = null;
    	String address = "192.168.1.3";
    	int PORTNUM;
    	
    	public Connect(int port) {            //port is defined upon usage of constructor.
    		PORTNUM = port;                   //Java VM 1.4.2-01-b6 hangs when also the host is defined in the contructor,
    		                                  //works fine with newest 1.4.2 VM
        }
    	
    	void establishConnection() {          //void for establishing the connection and setting up I/O.
    		
    		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);
    	    }
        }
        
        void closeConnection() {              //void to close connection if needed
        	
        	try {
        	     reader.close();
        	     writer.close();
        	     socket.close();
            } catch (IOException e) {
            	         System.err.println("Exception: " + e.getMessage());
            	         System.exit(1);
            }
        }
    }
    which works fine and if you look at Crat.java you see that I have used it multiple times. But this class is not usable to transfer binary data (image) because of the InputStreamReader and PrintWriter which are only usable for all kinds of unicode text. Hence I created a new class (not on my website yet) that inherits from Connect:

    Code:
    class DataConnect extends Connect {     //class for transfer of image, inherits from Connect
    	
    	Socket socket = null;
    	BufferedInputStream buffer = null;
    	DataInputStream data_reader = null;
    	
    	public DataConnect(int port) {
    	    PORTNUM = port;
             }
        
        void establishConnection() {
        	
        	try {
        		socket = new Socket(address, PORTNUM);
        		buffer = new BufferedInputStream(socket.getInputStream());
        		data_reader = new DataInputStream(buffer);
            } catch (IOException e) {
            	System.err.println("Exception: " + e.getMessage());
            	System.exit(1);
            }
        }
    }
    It might use Connect's methods so it inherits from it.
    Upon compiling Crat.java with this class added this error occurs:

    Crat.java:140: Connect(int) in Connect cannot be applied to ()
    public DataConnect(int port) {

    Although I have been coding Java for a few years I never really had the need to use inheritance. Anybody knows what I'm doing wrong? I know I can simply add DataConnect's functionnality to Connect and it is solved but I would like to do it this way.
    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
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    The constructor in Connect is not being explicitly called by DataConnect, so it is being called by default (and no parameters are being passed to it. Connect's constructor though requires an integer (port) argument. To fix it add a line to the DataConnect constructor to properly call the Connect constructor.:
    Change
    public DataConnect(int port) {
    PORTNUM = port;
    }
    to
    Code:
    public DataConnect(int port) {
    	super(port);    
            PORTNUM = port;
    }
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday
    Visit alastairgrant.ca

  3. #3
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    Thank you!
    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
  •