Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

client/server using UDP. How can i terminate the connection?

843790Jan 13 2008 — edited Jan 13 2008
hello everyone!

i 've implemented simple client/server chat application using UDP.
i 'd like the server terminates the connection when is received the string "TERMINATE"
by the client. Could anyone help me?
 // wait for packets to arrive, display data and echo packet to client
   private void waitForPackets()
   {
      while ( true ) { // loop forever

         // receive packet, display contents, return copy to client
         try {

            // set up packet
            byte data[] = new byte[ 100 ];
            DatagramPacket receivePacket = 
               new DatagramPacket( data, data.length );

            socket.receive( receivePacket ); // wait for packet
            if (receivePacket.getData().equals("terminate")) {System.exit(1);
            }
            // display information from received packet 
            displayMessage( "\nPacket received:" + 
               "\nFrom host: " + receivePacket.getAddress() + 
               "\nHost port: " + receivePacket.getPort() + 
               "\nLength: " + receivePacket.getLength() + 
               "\nContaining:\n\t" + new String( receivePacket.getData(), 
                  0, receivePacket.getLength() ) );

            sendPacketToClient( receivePacket ); // send packet to client
         }

         // process problems manipulating packet
         catch( IOException ioException ) {
            displayMessage( ioException.toString() + "\n" );
            ioException.printStackTrace();
         }

      } // end while

   } // end method waitForPackets
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2008
Added on Jan 13 2008
6 comments
609 views