server app


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: server app

  1. #1
    Alan Shiers Guest

    server app

    Hi there:
    I'm trying to work on a server application which is part of a game.
    If you've created or helped to create server applications before please
    read on.

    I wanted the game to be networkable over the Net. I've come across a
    small snag however. When the application first launches it runs a
    function called execute(). In this function is this line of code:
    players[i] = new Player(server.accept(), this, i);
    This line of code is inside a for loop and is used to create player
    objects everytime someone logs onto the server app via a Client applet.
    In this case the game only allows for two players so the players[] array
    has only two indexes.

    For clarity, the Player object has the following constructor:

    public Player(Socket s, JBattleShipsServer server, int num)
    {
    currentPlayer = (num == 0 ? "Player1" : "Player2");
    connection = s;

    try
    {
    InputStream is = connection.getInputStream();
    System.out.println("InputStream for " + currentPlayer + " is "
    + is.toString());
    input = new DataInputStream(is);
    output = new DataOutputStream(connection.getOutputStream());
    }
    catch(IOException e)
    {
    e.printStackTrace();
    System.exit(1);
    }
    control = server;
    number = num;
    }

    Now, there is nothing wrong with any of this code except the fact that
    the server app also has a menu bar with some drop down menus which
    cannot be accessed while the execute() function is running. You see,
    what happens is that when the execute() function runs and reaches the
    line: players[i] = new Player(server.accept(), this, i);
    the entire application is suspended (brought to a grinding hault) until
    two individuals log on via the Client applet. Only then, does the for
    loop finish executing, the rest of the code in execute() continues to
    run, the game commences, and I'm able to access the menus on the menu
    bar. This is not acceptible. I need to access the menus on the menu bar
    whether there is anyone logged on and playing a game or not! If anyone
    has had a similar experience in designing a server app, please provide
    some ideas for a workaround.

    Regards,

    Alan


  2. #2
    John MacLatchie Guest

    Re: server app


    By default, when a the server waits for a connection, accept() method, it
    waits indefinitely until a connection arrives. The thread in which the accept()
    method is performed is "blocked". The application can specify a time-out
    period for the server socket such that the server will block only until either
    a connection arrives or the time-out period expires. This of course still
    leaves some period of time in which the thread will be blocked and hence
    not respond. The best approach is to separate the server and the menu manipulation
    into individual threads thus when the connection function blocks the menu
    manipulation function will still be available.
    ...John

    Alan Shiers <jshiers@istar.ca> wrote:
    >
    >--------------8D41671524FC9D226ABCB54F
    >Content-Type: text/plain; charset=us-ascii
    >Content-Transfer-Encoding: 7bit
    >
    >Hi there:
    > I'm trying to work on a server application which is part of a game.
    >If you've created or helped to create server applications before please
    >read on.
    >
    >I wanted the game to be networkable over the Net. I've come across a
    >small snag however. When the application first launches it runs a
    >function called execute(). In this function is this line of code:
    >players[i] = new Player(server.accept(), this, i);
    >This line of code is inside a for loop and is used to create player
    >objects everytime someone logs onto the server app via a Client applet.
    >In this case the game only allows for two players so the players[] array
    >has only two indexes.
    >
    >For clarity, the Player object has the following constructor:
    >
    >public Player(Socket s, JBattleShipsServer server, int num)
    >{
    > currentPlayer = (num == 0 ? "Player1" : "Player2");
    > connection = s;
    >
    > try
    > {
    > InputStream is = connection.getInputStream();
    > System.out.println("InputStream for " + currentPlayer + " is "
    >+ is.toString());
    > input = new DataInputStream(is);
    > output = new DataOutputStream(connection.getOutputStream());
    > }
    > catch(IOException e)
    > {
    > e.printStackTrace();
    > System.exit(1);
    > }
    > control = server;
    > number = num;
    >}
    >
    >Now, there is nothing wrong with any of this code except the fact that
    >the server app also has a menu bar with some drop down menus which
    >cannot be accessed while the execute() function is running. You see,
    >what happens is that when the execute() function runs and reaches the
    >line: players[i] = new Player(server.accept(), this, i);
    >the entire application is suspended (brought to a grinding hault) until
    >two individuals log on via the Client applet. Only then, does the for
    >loop finish executing, the rest of the code in execute() continues to
    >run, the game commences, and I'm able to access the menus on the menu
    >bar. This is not acceptible. I need to access the menus on the menu bar
    >whether there is anyone logged on and playing a game or not! If anyone
    >has had a similar experience in designing a server app, please provide
    >some ideas for a workaround.
    >
    >Regards,
    >
    >Alan
    >
    >--------------8D41671524FC9D226ABCB54F
    >Content-Type: text/html; charset=us-ascii
    >Content-Transfer-Encoding: 7bit
    >
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    ><HTML>
    >Hi there:
    ><BR>*** I'm trying to work on a server application which
    >is part of a game.* If you've created or helped to create server applications
    >before please read on.
    ><P>I wanted the game to be networkable over the Net.* I've come across
    >a small snag however.* When the application first launches it runs
    >a function called execute().* In this function is this line of code:*
    >players[i] = new Player(server.accept(), this, i);
    ><BR>This line of code is inside a <I>for loop</I> and is used to create
    >player objects everytime someone logs onto the server app via a Client
    >applet.* In this case the game only allows for two players so the
    >players[] array has only two indexes.
    ><P>For clarity, the Player object has the following constructor:
    ><P>public Player(Socket s, JBattleShipsServer server, int num)
    ><BR>{
    ><BR>***** currentPlayer = (num == 0 ? "Player1"
    >: "Player2");
    ><BR>***** connection = s;
    ><P>***** try
    ><BR>***** {
    ><BR>******** InputStream is = connection.getInputStream();
    ><BR>******** System.out.println("InputStream
    >for " + currentPlayer + " is " + is.toString());
    ><BR>******** input = new DataInputStream(is);
    ><BR>******** output = new DataOutputStream(connection.getOutputStream());
    ><BR>***** }
    ><BR>***** catch(IOException e)
    ><BR>***** {
    ><BR>******** e.printStackTrace();
    ><BR>******** System.exit(1);
    ><BR>***** }
    ><BR>***** control = server;
    ><BR>***** number = num;
    ><BR>}
    ><P>Now, there is nothing wrong with any of this code except the fact that
    >the server app also has a menu bar with some drop down menus which cannot
    >be accessed while the execute() function is running.* You see, what
    >happens is that when the execute() function runs and reaches the line:*
    >players[i] = new Player(server.accept(), this, i);
    ><BR>the entire application is suspended (brought to a grinding hault) until
    >two individuals log on via the Client applet.* Only then, does the
    >for loop finish executing, the rest of the code in execute() continues
    >to run, the game commences, and I'm able to access the menus on the menu
    >bar. This is not acceptible.* I need to access the menus on the menu
    >bar whether there is anyone logged on and playing a game or not!*
    >If anyone has had a similar experience in designing a server app, please
    >provide some ideas for a workaround.
    ><P>Regards,
    ><P>Alan</HTML>
    >
    >--------------8D41671524FC9D226ABCB54F--
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center