Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Networking problems

Options
  • 07-12-2007 11:56am
    #1
    Registered Users Posts: 427 ✭✭


    Hey
    I'm trying to build a server and client for an online game. Its a very basic turn taking game and only one client has to contact the server at any one time.

    For example, when it is player A's turn the server will tell client A to begin its turn. The server will then wait for a response from client A. Absolutely no data will be sent during this wait period. When the server gets client A's response it will then tell client B to begin its turn and the process will repeat. Client A will be waiting for input from the server until its next turn.

    Is it necessary to multithread the server in this program? I'd really like to avoid it.

    My plan is as follows:
    	ServerSocket serverSocket;
    
    	Socket clientOne;
    	PrintWriter outOne;
    	BufferedReader inOne;
    
    	Socket clientTwo;
    	PrintWriter outTwo;
    	BufferedReader inTwo;
    
    	Socket clientThree;
    	PrintWriter outThree;
    	BufferedReader inThree;
    
    There will be one ServerSocket which will bind to a specific port. Each Client will listen on this ServerSocket. I know they cannot listen at the same time in a singly threaded application but I intend to listen to one at a time, since I know exactly which one will be sending information at any given time. Can you see any problems with this idea? I know multithreading would be better but we really need to avoid it as it completely ruins our design.

    Also, Can I have multiple Sockets on a ServerSocket? All will be connected simultaneously but only one will actually be sending/receiving infrmation.
    Thanks,
    Kev


Comments

  • Registered Users Posts: 413 ✭✭ianhobo


    hmm havent looked at Java networking in a while, and if wrong, other will soon correct me

    You're basic plan seems sound enough for starting out.
    As you have described the game, I don't see any reason why you must make it multithreaded right now given the overhead, but if you were doing a lot of processing on the side with user input or working with it before communicating with the next client, you might consider it, but a simple polling system should suffice for now.

    A serious of if's in a while loop would do.
    If there is input from client1; do input()
    if input from client2, do input()

    The listen sockets will have input buffers, providing a bit of leeway here too.

    Start out as planned, and run some tests communication. if your missing packets or connections due to the server being busy with another process before getting around checking the ports again, then you'll know its time to make it multi threaded. In which case, you would probably only need 2 thread, a listen thread, and a process thread.

    The listen thread will ensure you don't miss any data, and you can just stick the captured data it into a buffer to be processed by the other thread when it runs


  • Registered Users Posts: 427 ✭✭Kevo


    Thanks. Seems to be working.

    Just one problem. When I'm reading from a socket and there is nothing to read, I get an IOException. I can't find a method which just waits until there is some data to read and then returns it.

    heres an example of my code for the client:
    	try
    	{
                clientSocket = new Socket("localhost", 3333);
                out = new PrintWriter(clientSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            }
    	catch (UnknownHostException e)
    	{
                System.err.println("Don't know about host: taranis.");
                System.exit(1);
            }
    	catch (IOException e)
    	{
    		System.err.println("Couldn't get I/O for the connection to: taranis.");
    		System.exit(1);
            }
    
    
    	try
    	{
    		input = in.readLine();
    	}
    	catch (IOException e)
    	{
                System.err.println("Couldn't get I/O for the connection to: taranis.");
                System.exit(1);
            }
    


  • Registered Users Posts: 413 ✭✭ianhobo


    Try this from the Java API. BufferedReader has a ready() method.
    If the test is true, perform the read, if it's not ready, skip the read this time

    boolean ready()
    Tell whether this stream is ready to be read.

    http://java.sun.com/j2se/1.3/docs/api/java/io/BufferedReader.html


  • Registered Users Posts: 2,426 ✭✭✭ressem


    BufferedReader.readline() does block.

    Have you checked the detail message of the IOException?


Advertisement