some code not executing

Below is part of a server program that accepts connections from 2 clients.  The problem is the last four lines of code in this sample dont seem to get executed, that is no output is put to screen. The lines of code work if I execute them within the thread, ie accessing the other threads value from a thread.

while (true)
{
   ++ClientsConnected;
   
   if (ClientsConnected > 2)
            new CannotConnect(serve.accept());
   
    else if (ClientsConnected == 1){
          new FirstClient(serve.accept());
          }
    //first client thread
   
      else if (ClientsConnected == 2){
        new SecondClient(serve.accept());
       
            }
   String FirstThreadValue = (String)playerEntries.get("First");
        String SecondThreadValue = (String)playerEntries.get("Second");
         display.append("Player1 sent" +FirstThreadValue);
          display.append("Player2 sent" +SecondThreadValue);
}
eire_irelandAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
bit complicated logic just to start two threads :)

Why don't you just do something like:

new FirstClient(serve.accept());
new SecondClient(serve.accept());

The logic for what each player entered will need to be handled elsewhere as you need to wait until the players have sent something. How you do this depends on your game play requirements.
0
CEHJCommented:
You only need two types of thread class [a possible third could be an abstract superclass but leave that for now]. You can name the thread at this stage as well in a 'PlayerThread' constructor, e.g.

while (true)
{
      ++clientsConnected;  
      if (clientsConnected > 2)
            new CannotConnect(serve.accept());
      else new PlayerThread(serve.accept(), "Player" + clientsConnected);// 'Player1' or 'Player2'
}   

clientsConnected (variables and method names should start with a small letter, class names with a capital) may need to be rest later after disconnects
0
eire_irelandAuthor Commented:
I dont really understand what yee are trying to say, is there a way of waiting until the two threads have executed and then trying to get the values from each thread.

while (true)
{
   ++ClientsConnected;
   
   if (ClientsConnected > 2)
           new CannotConnect(serve.accept());
    else if (ClientsConnected == 1){
         new FirstClient(serve.accept());//return a flag?
         }
      else if (ClientsConnected == 2){
        new SecondClient(serve.accept());//return a flag?
           }
///if the two flags are true do this?
   String FirstThreadValue = (String)playerEntries.get("First");
        String SecondThreadValue = (String)playerEntries.get("Second");
         display.append("Player1 sent" +FirstThreadValue);
          display.append("Player2 sent" +SecondThreadValue);
}
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

objectsCommented:
> is there a way of waiting until the two threads have executed

yes, if thats what you want to do.

FirstClient a = new FirstClient(serve.accept());
SecondClient b = new SecondClient(serve.accept());

// wait for thread to finish

a.join();
b.join();

String FirstThreadValue = a.getValueEntered();
String SecondThreadValue = b.getValueEntered();
display.append("Player1 sent" +FirstThreadValue);
display.append("Player2 sent" +SecondThreadValue);

0
CEHJCommented:
I thought the objective of the threads connecting was to send a message to the Server?
Surely it's up to the server to 'get the values from each thread' by reading what the clients have written to the socket's stream?
0
eire_irelandAuthor Commented:
That is the objective CEHZ. The server is reading what the clients have written to the sockets stream elsewhere in the server program. Am I being clear enough?
I tried using the join method but I got the following error when declaring a variable "a".

 else if (ClientsConnected == 1){
    FirstClient a = new FirstClient(serve.accept());
          }
      else if (ClientsConnected == 2){
       SecondClient b = new SecondClient(serve.accept());
       
            }
   a.join();
   b.join();

--------------------Configuration: j2sdk1.4.0_02 <Default>--------------------
C:\Documents and Settings\Adrian Ron\Desktop\Serverz.java:68: cannot resolve symbol
symbol  : variable a  
location: class Serverz
   a.join();
   ^
Process completed.
0
objectsCommented:
no need for ththose if statements, just declare your two threads as I posted above.
And you also need to ensure that the two player threads terminate (run() method exits) once they have completed.
0
CEHJCommented:
You still seems to be using a different class for each player thread - this isn't necessary
0
eire_irelandAuthor Commented:
If two clients are connected and another client tries to connect a thread is spawned informing the client that no more players can be added to the game. Thats why im using the if statements.
0
CEHJCommented:
There's nothing wrong with using ifs, but as in the code i gave you before, it should be

if (clientsConnected < LIMIT)
0
eire_irelandAuthor Commented:
new PlayerThread(serve.accept(), "Player" + clientsConnected);

So I define two new threads as PlayerThreadPlayer1 and PlayerThreadPlayer2
0
CEHJCommented:
>>So I define two new threads

It's one class, not two.
0
eire_irelandAuthor Commented:
But im executing different commands in each thread. The two client threads are not identical
0
CEHJCommented:
>>But im executing different commands in each thread

How? I thought the only difference was in the message each sends?
0
eire_irelandAuthor Commented:
new PlayerThread(serve.accept(), "Player" + clientsConnected);

What exactly does that command execute?
0
CEHJCommented:
It creates a new instance of the PlayerThread class
0
eire_irelandAuthor Commented:
"Player" + clientsConnected

This part?
0
CEHJCommented:
It names the thread that connected. That's a String parameter too be set as its name. As per my comment before

// 'Player1' or 'Player2'
0
eire_irelandAuthor Commented:
So i define a new class called PlayerThread, but this is one thread, output is different for each client?, i think im lost?
0
objectsCommented:
> If two clients are connected and another client tries to connect a thread is spawned
> informing the client that no more players can be added to the game. Thats why im
> using the if statements.

The if statement is not necessary to achieve this, you can simply start a new Thread after both player threads have started, or close the server socket if no connections are required.
0
objectsCommented:
I don't see any need to assign names to the threads either.

You just need to ensuer you wait until both users have entered values before displaying them.
0
CEHJCommented:
>>I don't see any need to assign names to the threads either.

If you don't assign a name then each client will have to identify itself as well as send its message. Otherwise, how are you going to know which 'player' is which?
0
objectsCommented:
What does the server need to know for, and what difference does it make if the server makes up names for them anyway. The server just deals with two players.

0
CEHJCommented:
I think it could be useful to identify things correctly - don't you think so Jim?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
Some arbitrary name assigned by the server is only of use to the server, and as the server already knows which is which it seems redundant.
0
CEHJCommented:
Yes, OK - i see what you mean. The clients should identify themselves.
0
eire_irelandAuthor Commented:
Ive decided not to use maps, im using global variables instead and it seems to be working ok so far
0
CEHJCommented:
OK - shall we draw this leg of the proceedings to a close then?
0
eire_irelandAuthor Commented:
Ya, it might be better to use maps but im stuck for time.... but cheers anyway
0
CEHJCommented:
8-)

I think some points for objects would not have gone amiss ;-)
0
eire_irelandAuthor Commented:
Damn, I thought I did,  I meant to split the points between the two of yee....
0
CEHJCommented:
don't worry - i'll post him 25
0
eire_irelandAuthor Commented:
ok, thats grand
0
CEHJCommented:
done!
0
objectsCommented:
Did the actual question get answered here?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.