Solved

some code not executing

Posted on 2003-12-06
35
362 Views
Last Modified: 2010-03-31
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);
}
0
Comment
Question by:eire_ireland
  • 15
  • 12
  • 8
35 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9889833
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9890456
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
 

Author Comment

by:eire_ireland
ID: 9891956
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 9893014
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9893044
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
 

Author Comment

by:eire_ireland
ID: 9895426
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
 
LVL 92

Expert Comment

by:objects
ID: 9895473
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9895661
You still seems to be using a different class for each player thread - this isn't necessary
0
 

Author Comment

by:eire_ireland
ID: 9895973
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9895999
There's nothing wrong with using ifs, but as in the code i gave you before, it should be

if (clientsConnected < LIMIT)
0
 

Author Comment

by:eire_ireland
ID: 9896051
new PlayerThread(serve.accept(), "Player" + clientsConnected);

So I define two new threads as PlayerThreadPlayer1 and PlayerThreadPlayer2
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896093
>>So I define two new threads

It's one class, not two.
0
 

Author Comment

by:eire_ireland
ID: 9896108
But im executing different commands in each thread. The two client threads are not identical
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896149
>>But im executing different commands in each thread

How? I thought the only difference was in the message each sends?
0
 

Author Comment

by:eire_ireland
ID: 9896266
new PlayerThread(serve.accept(), "Player" + clientsConnected);

What exactly does that command execute?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896289
It creates a new instance of the PlayerThread class
0
 

Author Comment

by:eire_ireland
ID: 9896311
"Player" + clientsConnected

This part?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896376
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
 

Author Comment

by:eire_ireland
ID: 9896523
So i define a new class called PlayerThread, but this is one thread, output is different for each client?, i think im lost?
0
 
LVL 92

Expert Comment

by:objects
ID: 9899900
> 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
 
LVL 92

Expert Comment

by:objects
ID: 9899923
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9900008
>>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
 
LVL 92

Expert Comment

by:objects
ID: 9900062
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9900139
I think it could be useful to identify things correctly - don't you think so Jim?
0
 
LVL 92

Expert Comment

by:objects
ID: 9900158
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 9903493
Yes, OK - i see what you mean. The clients should identify themselves.
0
 

Author Comment

by:eire_ireland
ID: 9903856
Ive decided not to use maps, im using global variables instead and it seems to be working ok so far
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 9903874
OK - shall we draw this leg of the proceedings to a close then?
0
 

Author Comment

by:eire_ireland
ID: 9903916
Ya, it might be better to use maps but im stuck for time.... but cheers anyway
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9903966
8-)

I think some points for objects would not have gone amiss ;-)
0
 

Author Comment

by:eire_ireland
ID: 9904210
Damn, I thought I did,  I meant to split the points between the two of yee....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9904226
don't worry - i'll post him 25
0
 

Author Comment

by:eire_ireland
ID: 9904249
ok, thats grand
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9904294
done!
0
 
LVL 92

Expert Comment

by:objects
ID: 9906719
Did the actual question get answered here?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
web application structure 18 99
Chrome and Firefox Java 5 50
ejb entity bean example issue 2 16
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question