Results 1 to 2 of 2
  1. #1
    Paul279 is offline Member
    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    0

    Default Thread and calling Methods

    Hello,

    I am new in Threads, so i have a comprehension problem.

    I have two classes. One is called "Client" and the second is "ClientThread".
    In the Class Client I start a new ClientThread in a new Thread
    Java Code:
    clientThread = new Thread(new clientThread(ipAdresseServer, serverport));
    clientThread.start();
    ClientThread is starting well, looking good.


    Now in my ClientThread i have the run() Method and also other methods like send(Object message) and receive(Object message)
    I would like something to send, so I would write the code below:
    Java Code:
    clientThread.send(message)
    But there is the Problem. I can`t access the method send. So my Question is, how can I call my method send?

    For a better understanding, the source code is below, i have deleted the initComponents(), that would only blow up the source code and is not important .

    Client: (Sticky points at line 25,26,27)
    Java Code:
    public class Client extends javax.swing.JFrame {
    
        int ServerConnected = 0;
        static Thread clientThread;
        
        public Client(){
            initComponents();     
            
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
    
        private void jButton_ServerKontaktierenActionPerformed(java.awt.event.ActionEvent evt) {                                                           
    
            String portString, ipAdresseServer;
            int serverport;
            int help = 1;
            
            ipAdresseServer = jTextField_ServerIPAdress.getText().toString();
            portString = jTextField_ServerPort.getText().toString();
            serverport = Integer.valueOf(portString);
                
            try {
                 clientThread = new Thread(new clientThread(ipAdresseServer, serverport));
                 clientThread.start();
                 clientThread.send("Test");
            } catch (UnknownHostException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                jTextArea_Error.setText(jTextArea_Error.getText().toString()+"Kein Host gefunden oder Verbindung abgebrochen!\n");
                //Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                help = 0;
            }
            
            if(help == 1)
            {
                ServerConnected = 1;
                jTextArea_Error.setText(jTextArea_Error.getText().toString()+"Zu Server verbunden\n");
                
                new ClientHauptform().setVisible(true);
                this.setVisible(false);
                  
            }
    
        }
    ClientThread:
    Java Code:
    public class clientThread implements Runnable{
     
        static int alive = 1;
        ObjectOutputStream  out;
        ObjectInputStream  in;
        Socket socket;
    
        clientThread(String ip, int port) throws UnknownHostException, IOException {
                socket = new Socket(ip, port);
        }
            
      
        @Override
        public void run()
        {
           System.out.println("Client: Bin im Run");       
           ObjectInputStream in = null;
                try {
                    in = new ObjectInputStream(socket.getInputStream());
                    while(alive == 1){
                        try {
                            in.readObject();
                            switch(in.toString())
                            {
                                case "getStringArrayTaenzeJurorenansicht":
                                    System.out.println("GetStringArrayTaenzeJurorenansicht empfangen ;-)");
                                    break;
                            }
                        } catch (ClassNotFoundException | IOException e) {
                            System.out.println(socket.getInetAddress().getHostAddress()+" disconnected from the Server");
                            break;
                        }
                    }
                }catch(SocketException e){
                    System.out.println(socket.getInetAddress().getHostAddress()+" disconnected from the Server");
                    System.out.println("hall");
                }catch (IOException e) {
                   System.out.println(socket.getInetAddress().getHostAddress()+" disconnected from the Server");
                    System.out.println("hall");
                }     
                
            //Socketverbindungen ordnungsgemäß schließen    
            try {
                out.close();
                in.close();
                socket.close();
            } catch (IOException ex) {
                Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
                
                
        }
        
        public void send(Object message) throws IOException {
            ObjectOutputStream oos;
                try {
                    oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message);
                    oos.flush();
                    System.out.println("CLIENT: Habe etwas gesendet");
                } catch (IOException e) {
                    // TOD  O Auto-generated catch block
                }
        }
     
        public Object receive() throws IOException, ClassNotFoundException{
                
                return in.readObject();
        }
    So I hope anybody can give me an advice to correct my problem
    Thank you
    Last edited by Paul279; 10-17-2012 at 04:40 PM.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Thread and calling Methods

    You have a line of code

    Java Code:
    clientThread = new Thread(new clientThread(ipAdresseServer, serverport));
    where the first ocurrence of clientThread if of type Thread (don't use the same name for different type of objects) and will know that the passed runnable will have a run() method and nothing else about the runnable clientThread. If you you want to call methods in the runnable you need to have an reference to the runnable.

Similar Threads

  1. Calling methods
    By Alkor in forum New To Java
    Replies: 15
    Last Post: 02-25-2012, 02:33 PM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM

Posting Permissions

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