Steven Wijaya
Steven Wijaya

Reputation: 1

sending an object to thread back and forth

I'm trying to make a simple server client thread application in Java, about a quiz-like game. I have a Main class as its "brain", which will handle the main processes like asking the questions, checking the answers, etc. I have two other classes, ServerHandler and Player. ServerHandler is to connect Main with Player. So far, the problem is that I want to send properties of Main to ServerThread. I tried using this but it doesn't work. Any suggestions to help improve my program would also be welcome.

    public class Main 
    {
        public static int MYECHOPORT = 8189;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            // TODO code application logic here

            ServerSocket s = null;
            int count;
            count=0;
            Pemain [] player=new Pemain[3];

        try 
            {
            s = new ServerSocket(MYECHOPORT);
        } 
            catch(IOException e) 
            {
            System.out.println(e);
            System.exit(1);
        }
        while (true) 
            {
                for(int i=0;i<3;i++)
                {
                    player[i]=new Pemain();
                    player[i].setNo(i+1);
                    count++;
                }            
            try 
                {
            for(int i=0;i<3;i++)
                    {
                        player[i].setS(s.accept());
                    }
            } 
                catch(IOException e) 
                {
            System.out.println(e);
            continue;
            }

                if(count==3)
                {
                    for(int i=0;i<3;i++)
                    {
                        new ServerHandler(player[i].getS(), this).start();
                    }
                }


                // ignore
            }
        }  

    }

Upvotes: 0

Views: 132

Answers (2)

JB Nizet
JB Nizet

Reputation: 692033

First of all, try to avoid saying "it doesn't work". Tell how it doesn't work. Tell what happens. Tell the error message you get.

Second, you seem to have not grasped basic OO concepts such as objects and static methods, and dealing with very complex stuff such as socket IO and multiple threads is too early.

Now to your problem: you're trying to pass this as a method argument. this represents the object on which the current method is called. But the method where you're trying to do that is a static method, so it doesn't have any enclosing object. The code of the main method should probably look like that:

public static void main(String[] args) {
    Main main = new Main();
    main.execute();
}

Where the execute() method contains what your main method currently contains.

Read http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html for more information about static class members.

Upvotes: 0

PeterMmm
PeterMmm

Reputation: 24630

this won't work because you are in a static method (main()). Refactor to a non-static method.

Strip static fom main and rename to m(String[] args). Then insert this main method:

public static void main(String[] args) {
   new Main().m(args)
}

Upvotes: 1

Related Questions