Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Socket Server try statement  (Read 1508 times)
0 Members and 1 Guest are viewing this topic.
Offline Xyle
« Posted 2009-01-03 00:23:27 »

I am trying to capture when a client connects to the server in order to list a new game in a lobby, the problem is, nothing happens until 2 players connect due to using try statements in run method of the server. I take it, when using a try statement, the code doesnt continue, until the try statement is met?


for example

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
while(true){
     try1{
          if(this is true){
               print try1
          {
           else{
               do that
           }
     }
      catch(){
          print error
     }

     try2{}

     print something
}


So according to this code thats inside the run method of the server class, print try1 doesnt print until try1 and try2 are met

Im not fully understanding this too well, any help would be greatly appreciated!

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Xyle
« Reply #1 - Posted 2009-01-03 00:27:16 »

Ok, I guess my real code didnt reflect what I typed above. After putting the print in the if statement like I have above, I got the results I needed.

Be back soon!!

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Online moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #2 - Posted 2009-01-03 02:22:11 »

are you sure execution is not being paused by a blocking call? e.g. socket accept?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xyle
« Reply #3 - Posted 2009-01-03 04:35:06 »

Wheeew,
Man this client server architecture thing sure is confusing... Im on hour 12 now.

I tested and tested and came to find out that it really is hanging in the try statements

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
public void run() {
   int Rnd = 0;
   while(true){
      ////////////////////////////////////////////////////////////
     try {
         if(client_socket1 == null){
            client_socket1 = listen_socket.accept();
           
            if(client_socket1 != null){
                 System.out.println("Client1 has connected");
            }
         }
      }
      catch(IOException e) {
         fail(e, "Exception while listening for connections");
      }
      ////////////////////////////////////////////////////////////  
     try{
         if(client_socket2 == null){
            client_socket2 = listen_socket.accept();
           
            if(client_socket2 != null){
               //Client 2 can only exist if client 1 exists
              System.out.println("Client2 has connected.");
               TTTConn conn1 = new TTTConn(client_socket1, client_socket2);
            }
         }
      }
      catch(IOException e) {
         fail(e, "Exception while listening for connections");
      }
     
      ////////////////////////////////////////////////////////////
     Rnd = Rnd + 1;
      System.out.println("Rnd: "+Rnd);  
   }
}


The above code works and prints Client1 has connected when the first user connects, then Client2 has connected when the second user connects. The Out.PrintLn doesnt happen until 2 users have connected. I wasnt prepared for that.

But, this is not the direction I was going for, so Im looking at more stuff, hehehehe. Im looking at a chat server that consistantly accepts connections and deletes them in unused. I like this style a bit better.

Thanks for the help though

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline woogley
« Reply #4 - Posted 2009-01-03 05:34:57 »

Well, since you're looking at stuff to figure out, here are a few key things that may help you..

Concurrency - running separate code simultaneously (to avoid that 'hanging' problem you have)

Arrays - to avoid writing redundant code pieces (client_socket1, client_socket2, etc..). Anytime you write similar code in the same file - (or worse: copy and paste a snippet of your code) - you should take a step back and evaluate how you are doing things.

Server-Client example - basic server example
Offline Xyle
« Reply #5 - Posted 2009-01-03 05:46:41 »

Thank you very much!

It just so happened that I used the Knock Knock trail to create the classes I was looking for. I was flopping back and forth between using a server to pair up people for a 2 player game or using the server and sending appropriate data to the right user.

I really appreciate the help, thank you.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!