![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: online game
the client gets connected and the data gets sent from the Server to the Client just fine but the trouble comes when trying to send data from the client to the server. I have the sendData method inside client but when i enter a string to send to the Server i think i need a receive method in the Server class to get the data correct? When i call the receive method inside Server it hangs like it is waiting to receive something from the client. But I am pretty sure the connections are right or else the Server would not send the Client something right? Here is the code
the Server class java Syntax (Toggle Plain Text)
java Syntax (Toggle Plain Text)
java Syntax (Toggle Plain Text)
|
|
|
|
|
|
#22 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: online game
ok I think I got it. It is a little rough around the edges but the clients take turns entering a string at the keyboard and then it sends it to the server and the other client also receives it. I have questions about the loops though. Since there are only 2 players can the connections be made before the main loop. So in the Server class can the s.accept() call be made outside of the loop twice for the 2 clients? Also in the NewMain class can the client.connect() and client.streams() methods be called before the loop or do they need to be done every time? Right now the clients take turns which i guess works since battleship is a turn based game anyway when its one persons turn the other will not be sending anything.
Server class java Syntax (Toggle Plain Text)
java Syntax (Toggle Plain Text)
java Syntax (Toggle Plain Text)
|
|
|
|
|
|
#23 |
|
Programmer
Join Date: Dec 2006
Posts: 50
Rep Power: 2
![]() |
Re: online game
You raise a good question about the multiple clients connecting at once and sending data simultaneously. I didn't have not tried working with several clients connecting at once and share their information through the server.
I don't have a solid answer right now. Finals are over this Thursday, then I can actually help you or one of the big guys/gals here can solve this one. |
|
|
|
|
|
#24 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: online game
well i think what i have works because i connect 2 clients and then whatever i type into 1 client's keyboard input gets printed on the other clients screen. I think that is really all i need. The string is passed from one client to the other via the Server. I am just wondering the best way to organize it with my battleship game because it would be better i think if there was a way where the clients would only connect once and the streams would only be established once. It would be less work but i have taken those methods out of the loops and have not gotten it to work right. I guess if the connections have to be made and streams created each time then it wouldn't be the end of the world. I also need to figure out how the put the Client NewMain into a method because then i would just use that method on the input the user enters on the keyboard and that would be sent to the client and used in the player's takeTurn method in the battleship game.
|
|
|
|
|
|
#25 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: online game
I added it to the Player class in my battleship game and it does send the numbers to the other client but I do not think all of them. I think this can be fixed by changing when they are prompted and stuff. Also, it applies what the player enters to itself instead of the other player. And the third thought i have is if both players have a ship at the same location would it be possible to hit your own ship then as well as the other player's ship? Here is my Player class. java Syntax (Toggle Plain Text)
|