4 Replies Latest reply on Dec 7, 2005 4:40 AM by 800384

    Monopoly Multi-player game

    807596
      Hi there,

      As part of a group project i am developing a Networked Monopoly game.

      At the moment, I am passing a Board() Data Structure from the Server to the Client Machine.

      It partially sends the Board Object from the Server to the Client but i get a Runtime error when writing from Server to Client:
      java.net.SocketException: Software caused connection abort: socket write error
      I have investigated a few things:

      1. I have set the Server Socket to infinite Timeout
      2. I have turned off the Firewall

      Both of these ideas have not worked.....

      Any suggestions would be much appreciated!

      Thanks!
        • 1. Re: Monopoly Multi-player game
          807596
          You should post the relevant code, make sure you use the code formatting tags when you do. Relevant code means the bits of code that send and receive your data. Until then it's almost impossible to make any sort of suggestion regarding your problem.

          Given the type of exception someone will probably reply within the hour with a solution.
          • 2. Re: Monopoly Multi-player game
            807596
            As suggested here is the code:

            Monopoly Server Snippet:
                public void run()
                {
                    while(true)
                    {
                        if(playersJoined == 0)
                        {          
                                MonopolyServer.first = new ServerToClientChannel(4441);
                                initialiser = (Initialise)MonopolyServer.first.readObject();
                                
                                //test code
                                System.out.println("Server, GameType: "+initialiser.getGameType());
                                System.out.println("Server, NoOfPlayers: "+initialiser.getNumberOfPlayers());
                                System.out.println("Server, PlayerName: "+initialiser.getPlayername());
                                System.out.println("Server, QuickStart: "+initialiser.getQuickStart());
                                
                                MonopolyServer.player = new ServerToClientChannel[initialiser.getNumberOfPlayers()];
                                MonopolyServer.shortGame = initialiser.getQuickStart();
                                MonopolyServer.player[0] = MonopolyServer.first;
                                MonopolyServer.gameState = new Board();
                                playersJoined++;
                                MonopolyServer.gameState.addNewPlayer(new Player(playersJoined, 1500, initialiser.getPlayername()));
                                MonopolyServer.player[0].writeObject(MonopolyServer.gameState);
                        }
                        else
                        {
                                // TODO: change to accept ports in any order
                                MonopolyServer.player[playersJoined] = new ServerToClientChannel(4441+playersJoined);
                                initialiser = (Initialise)MonopolyServer.player[playersJoined].readObject();
                                
                                //test code
                                System.out.println("Server, GameType: "+initialiser.getGameType());
                                System.out.println("Server, NoOfPlayers: "+initialiser.getNumberOfPlayers());
                                System.out.println("Server, PlayerName: "+initialiser.getPlayername());
                                System.out.println("Server, QuickStart: "+initialiser.getQuickStart());
                                
                                MonopolyServer.gameState.addNewPlayer(new Player(playersJoined, 1500, initialiser.getPlayername()));
                                playersJoined++;
                                for(int i=0; i<playersJoined; i++)
                                {
                                        MonopolyServer.player.writeObject(MonopolyServer.gameState);
            }
            }
            }
            }


            ServerToClientChannel Snippet:
            public ServerToClientChannel(int socketNumber)
                 {
                      try 
                            { 
                                serverSocket = new ServerSocket(socketNumber);
                            } 
                      catch (IOException e) {
                           System.err.println("Could not listen on port: "+socketNumber);
                           System.exit(1);   }
                      try 
                            { 
                                clientSocket = serverSocket.accept();
                                //test code
                                clientSocket.setTcpNoDelay(false);
                            } 
                      catch (IOException e) {
                           System.err.println("Accept failed.");
                           System.exit(1);   }
                      try { ois = new ObjectInputStream(clientSocket.getInputStream());}
                      catch (IOException e) { System.out.println("ObjectInputStream failed"); }          
                      try 
                            { 
                                oos = new ObjectOutputStream(clientSocket.getOutputStream());
                            }
                      catch (IOException e) {System.out.println("ObjectOutputStream failed"); }          
                 }
                    
                    // writeObject
                 public void writeObject(Object ob)
                 {     try {oos.writeObject(ob);}
                      catch(IOException e) 
                            {
                                System.out.println("IO output exception");
                                System.out.println(e.getMessage());
                            }
                 }
            Monopoly Client
             public void run() 
                {
                    //test code
                   System.out.println("Client: Entered Thread to listen for incoming objects"); 
                           
                   while(true)
                   {
                        //intialise ClientToServer Channel with Servers IP address and  assigned port
                        aPlayerChannel = new ClientToServerChannel(ipAddress, assignedPort);
                       
                       //assign the received Board Object from the Monopoly Server
                       aPlayerBoardObject = (Board) aPlayerChannel.readObject();
            [i]The Exception is:

            IO output exception
            java.net.SocketException: Software caused connection abort: socket write error
            at java.net.SocketOutputStream.socketWrite0(Native Method)
            at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
            at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
            at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1676)
            at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1585)
            at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1167)
            at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1121)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1278)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1392)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:294)
            at monopoly.ServerToClientChannel.writeObject(ServerToClientChannel.java:47)
            at monopoly.InitialisationThread.run(MonopolyServer.java:98)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
            • 3. Re: Monopoly Multi-player game
              807596
              Okay, i've browsed through your code and you seem to have a misconception concerning the usage of ServerSocket. You're creating a server socket for every connection you wish to establish, and this is not the correct way to use them at all.

              A server socket's only job is to process incoming connections using the accept method. As such, you need only one of them for your entire server. Basically a server should work like this :

              main server loop
              ServerSocket serverSocket = new ServerSocket(1234);
              
              while(serverIsRunning) {
              
                  Socket socket = serverSocket.accept();
              
                  ServerToClientChannel newChannel = new ServerToClientChannel(socket);
                  newChannel.start(); // <- should start it's own thread   
              }
              An accept() call will block until a client wishes to connect to the server. Once it does, the accept will return a new Socket you can use to talk to the client, and the main server loop will block again on accept() waiting for the next client to connect. Browse through a few recent threads on this forum, some have some code that'll explain it.

              Your exceptions are probably related to your connection initialisation not being correct. But fix the code so that it uses the server socket correctly before trying to fix that problem. That will also eliminate the need to reserve a range of ports for your server. Good luck.
              • 4. Re: Monopoly Multi-player game
                800384
                remonw:

                Well done! Vey nice, glad to see another professional network guy out here.