Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Timed thread to poll servers

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Timed thread to poll servers

    Hey everyone,

    New to the forums here. So a little background on what I am trying to do. I am in a group of two in my software design class and the assignment is Wink murder. My partner and I decided to go with a client/server set up to handle the 5-7 player minimum. Figured peer to peer gets to ugly after about three people. So I am writing the server. But I decided I am gonna write two servers. The game server and the master server that keeps a running total of all publicly available servers. So a client starts up, goes to the master server first and requests a list of all servers that have openings. I find it pointless to send a server that is full. Then that client picks a server and goes off to play. That's all I need to know about the client. But the game server is handling two different types of connections. The client connections and the master server connection. The master server, in order to keep its list up to date, will poll all servers in its list of known servers to verify they are still running. If not they are removed. I am having some trouble with the polling part. I send the string "POLL" to the server using a PrintStream and nothing happens. The server has a class to handle this one thing and I have some debug code in there to verify things are working but nothing ever pops up. Any suggestions? Also on a side note, the master server's server manager sleeps its thread for a second after all servers have been polled.

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    . I send the string "POLL" to the server using a PrintStream and nothing happens
    Hard to say why without having code to execute for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    this is the run method for the server manager
    	public void run()
    	{
    		while(sManagerThread.isAlive())
    		{
    			//System.out.println("Polling Servers");
    			int index = 0;
    			while(index != servers.size())
    			{
    				if(servers.get(index).pollServer())
    				{
    					index++;
    				}
    				else
    				{
    					servers.remove(index);
    					System.out.println("Server Count: " + String.valueOf(servers.size()));
    				}
    			}
     
    			try
    			{
    				Thread.sleep(1000);
    			}
    			catch(Exception e)
    			{
    			}
    		}
    	}
    this is the run method for the game server. this is inside a private class that the only job is to handle the polling
    		public void run()
    		{
    			while(mcThread.isAlive())
    			{
    				try
    				{
    					if(mcSkt.getInputStream().available() > 0)
    					{
    						InputStream in;
    						in = mcSkt.getInputStream();
    						BufferedReader bin = new BufferedReader(new InputStreamReader(in));
     
    						String data = bin.readLine();
    						System.out.println(data);
     
    						if(data == "POLL")
    						{
    							String toSend = "";
    							toSend += String.valueOf(cCount);
    							toSend += ",";
    							toSend += String.valueOf(mCount);
    							System.out.println("Responding to POLL");
    							pStream.println(toSend);
    						}
    						else
    						{
    							System.out.println("Error Reading Network Data");
    						}
    					}
    				}
    				catch(Exception e)
    				{
    					System.out.println("Error Reading Network Data");
    				}
    			}
     
    		}
    and this will probably be important. this is the code that the master server's server manager will call.
    public boolean pollServer()
    	{
    		pStream.print("POLL");
    		InputStream in;
    		try 
    		{
    			Thread.currentThread().sleep(250);
    			in = skt.getInputStream();
    			BufferedReader bin = new BufferedReader(new InputStreamReader(in));
    			String data = bin.readLine();
    			int secondStart = 0;
    			String cCountMessage = "";
    			try
    			{
    				for(int i = 0; i < data.length(); i++)
    				{
    					if(data.charAt(i) == ',')
    					{
    						secondStart = i + 1;
    						break;
    					}
    					else
    					{
    						cCountMessage += data.charAt(i);
    						cCount = Integer.valueOf(cCountMessage);
    					}
    				}
    				String mCountMessage = "";
    				for(int i = secondStart; i < data.length(); i++)
    				{
    					if(data.charAt(i) == ',')
    					{
    						secondStart = i + 1;
    						break;
    					}
    					else
    					{
    						mCountMessage += data.charAt(i);
    						mCount = Integer.valueOf(mCountMessage);
    					}
    				}
    			}
    			catch(Exception e)
    			{
    			}
    			return true;
    		} 
    		catch (IOException | InterruptedException e) 
    		{
    			System.out.println("Error Polling Server" + IP);
    			return false;
    		}
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    How can this code be compiled and executed for testing? Can you post code that will compile and execute?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    I would, but there are more than one class file involved. can I share a link to a dropbox folder on here?

    I hope so. here is the zip, inside are the source(completed as far as now) for the master server and game server. the game server is still being worked on.
    https://dl.dropbox.com/u/36695541/winkMurder.zip

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    no. because there are a lot of class dependencies. download the zip and open it. all the code is in there. but you will need to change the properties.prop file. inside there is a line that has "Master-IP:" on it. you will need to set that to be whatever IP you run the master server on

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    Sorry, that sounds like too much work to do to setup for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    okay, well here is some more info then. I ran it. and the server manager will output polling servers until a game server actually needs to be polled. then it quits. so would that put the error on the game server side?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    Hard to say what causes the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    the thread doesn't start on the server side that handles the polling requests!

    ///---UPDATE---///
    okay, so I got the game server thread to start. but the master server still stops outputting "Polling servers" when a game server connects

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    Without code that compiles and executes, I can't make any suggestions other than to add lots of println statements to print out messages to show execution flow and the values of variables as they are changed and used.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    I got it narrowed down to the issue I think. I ran the debug with some breakpoints and I think the issue is being generated from the InputStream. it seems like it never gets the InputStream from the socket.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    never gets the InputStream from the socket.
    I've not seen that happen but there is always a first time.
    Is that on the server or the client?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    its on the game server which is a "client" of the master server.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Timed thread to poll servers

    I was referring to the type of socket. But it really doesn't make any difference as a help to solving the problem.
    Without a Short, Self Contained, Correct Example to test with I can't do any debugging.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timed thread to poll servers

    omg! I just found the issue. on the master server, when I sent the poll request I didn't use println. so when I tried to read the string there wasnt any way to know that I had reached the end of the line.

Similar Threads

  1. Replies: 0
    Last Post: October 5th, 2012, 07:27 AM
  2. Simple Java Poll - Please Help
    By BurningSpirit in forum What's Wrong With My Code?
    Replies: 25
    Last Post: April 12th, 2012, 04:40 PM
  3. Replies: 2
    Last Post: December 19th, 2011, 01:49 PM
  4. File Servers and Java
    By Satch in forum Java Networking
    Replies: 0
    Last Post: December 20th, 2010, 12:25 AM