eire_ireland
asked on
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);
}
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(
String SecondThreadValue = (String)playerEntries.get(
display.append("Player1 sent" +FirstThreadValue);
display.append("Player2 sent" +SecondThreadValue);
}
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
while (true)
{
++clientsConnected;
if (clientsConnected > 2)
new CannotConnect(serve.accept
else new PlayerThread(serve.accept(
}
clientsConnected (variables and method names should start with a small letter, class names with a capital) may need to be rest later after disconnects
ASKER
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( ));//retur n 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);
}
while (true)
{
++ClientsConnected;
if (ClientsConnected > 2)
new CannotConnect(serve.accept
else if (ClientsConnected == 1){
new FirstClient(serve.accept()
}
else if (ClientsConnected == 2){
new SecondClient(serve.accept(
}
///if the two flags are true do this?
String FirstThreadValue = (String)playerEntries.get(
String SecondThreadValue = (String)playerEntries.get(
display.append("Player1 sent" +FirstThreadValue);
display.append("Player2 sent" +SecondThreadValue);
}
> 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);
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);
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?
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?
ASKER
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();
--------------------Config uration: j2sdk1.4.0_02 <Default>----------------- ---
C:\Documents and Settings\Adrian Ron\Desktop\Serverz.java:6 8: cannot resolve symbol
symbol : variable a
location: class Serverz
a.join();
^
Process completed.
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();
--------------------Config
C:\Documents and Settings\Adrian Ron\Desktop\Serverz.java:6
symbol : variable a
location: class Serverz
a.join();
^
Process completed.
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.
And you also need to ensure that the two player threads terminate (run() method exits) once they have completed.
You still seems to be using a different class for each player thread - this isn't necessary
ASKER
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.
There's nothing wrong with using ifs, but as in the code i gave you before, it should be
if (clientsConnected < LIMIT)
if (clientsConnected < LIMIT)
ASKER
new PlayerThread(serve.accept( ), "Player" + clientsConnected);
So I define two new threads as PlayerThreadPlayer1 and PlayerThreadPlayer2
So I define two new threads as PlayerThreadPlayer1 and PlayerThreadPlayer2
>>So I define two new threads
It's one class, not two.
It's one class, not two.
ASKER
But im executing different commands in each thread. The two client threads are not identical
>>But im executing different commands in each thread
How? I thought the only difference was in the message each sends?
How? I thought the only difference was in the message each sends?
ASKER
new PlayerThread(serve.accept( ), "Player" + clientsConnected);
What exactly does that command execute?
What exactly does that command execute?
It creates a new instance of the PlayerThread class
ASKER
"Player" + clientsConnected
This part?
This part?
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'
// 'Player1' or 'Player2'
ASKER
So i define a new class called PlayerThread, but this is one thread, output is different for each client?, i think im lost?
> 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.
> 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.
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.
You just need to ensuer you wait until both users have entered values before displaying them.
>>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?
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?
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ive decided not to use maps, im using global variables instead and it seems to be working ok so far
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ya, it might be better to use maps but im stuck for time.... but cheers anyway
8-)
I think some points for objects would not have gone amiss ;-)
I think some points for objects would not have gone amiss ;-)
ASKER
Damn, I thought I did, I meant to split the points between the two of yee....
don't worry - i'll post him 25
ASKER
ok, thats grand
done!
Did the actual question get answered here?
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.