Socket Server try statement

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


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!

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!!

are you sure execution is not being paused by a blocking call? e.g. socket accept?

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


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

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

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.