Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java: How to Create Interface

    I'm working on a multi-threaded server project and I created two classes:

    1. Class that will open a server socket and loop through accepting sockets. Each time it accepts a connection it spawns a new thread on class #2.

    2. This class, in the run() method will create an inputstream from the server socket and read each line coming through the server socket.

    What I want to do is in CLASS #1 implement an interface that will invoke a certain method in class #1 each time a line is entered in the server socket. How do you do this?

    Here is condensed class #1:
    Code:
    public Library() {
    	ServerSocket serverSocket=null;
    	boolean listening=true;
    	try {
    		serverSocket=new ServerSocket(801);
    		while(listening) new rServer(serverSocket.accept()).start();
    		serverSocket.close();
    	} catch(Exception e) { System.exit(0); }
    }
    static public void main(String[] args) { new Library(); }
    public void processCommand(String cmd, Thread thread){ }
    Here is the run() method of class #2.
    Code:
    public void run() {
    	try {
    		DataInputStream in=new DataInputStream(s.getInputStream());
    		DataOutputStream out=new DataOutputStream(s.getOutputStream());
    		out.writeChars("Number of connections: ");
    		out.writeChars(java.lang.String.valueOf(Thread.activeCount()-2)+"\r\n");
    		String line=null;
    		while(true) {
    			Thread.yield();
    			line=in.readLine();
    			System.out.print("Thread ");
    			System.out.print(Thread.currentThread());
    			System.out.println(" :" + line);
    			command(line);
    			if(line.equalsIgnoreCase("exit")) {
    				s.close();
    				Thread.currentThread().stop();
    			}
    		}
    	} catch(Exception e) {
    		System.out.println(e.getMessage());
    	}
    }
    See everything works, I just want to invoke method processCommand(String,Thread) each time a line comes in. I think I need to create an interface and implement on class #1, but I've never done that before. How?

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Solved, guess I was barking up the wrong tree with the interfaces, I really needed to create an abstract class. Here's what I did.

    I created an abstract class:
    Code:
    public abstract class rServerCommands {
    	public abstract void command(String cmd,Thread thr);
    }
    Then, in my first class I extened the abstract class:
    Code:
    public class Library extends rServerCommands {...
     ...while(listening) new rServer(serverSocket.accept(),this).start();
    Then, in the constructor of class 2:
    Code:
    public rServer(Socket s, rServerCommands obj) {...
    ...
    obj.command(...
    This way the main class, class 1, can pass itself as an argument to class 2. Since class 1 is a subclass of abstract rServerCommands, class 2 doesn't need to know what class it is, just that it is a rServerCommands class and can invoke the method command(String...etc).


  •  

    Posting Permissions

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