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