Expand|Select|Wrap|Line Numbers
- public void run(){
- String user_message = null;
- try {
- connection = startServer();
- System.out.println("Server started");
- while ((true) && (serverStarted) ){
- try {
- user_message = receiveMessage();
- check_query( user_message , "," );
- } catch ( IOException ex ) {
- System.out.println("Error1 "+ex.getMessage());
- setError( "Error establishing connection " ) ;
- Txt_Log.setText(Txt_Log.getText() + "\n Error establishing connection0") ;
- break;
- }
- }
- System.out.println("Server is stopped.....");
- }
- catch ( SocketException ex ) {
- System.out.println("Error2 "+ex.getMessage());
- setError("Error establishing connection ") ;
- Txt_Log.setText(Txt_Log.getText() + "\n Error establishing connection1") ;
- }
- catch( NullPointerException ex){
- System.out.println("Error3 "+ex.getMessage());
- ex.printStackTrace();
- setError("Error Null Pointer ") ;
- Txt_Log.setText(Txt_Log.getText() + "\n Error :"+ex.getMessage()) ;
- }
- }
- /*
- * Method starts server datagram to bind to port
- */
- public DatagramSocket startServer() throws SocketException{
- return new DatagramSocket(10000);
- }
- /*
- * Method stops server so that server is not able to receive client requests
- */
- public void closeServer(){
- if (connection != null){
- //connection.disconnect();
- connection.close();
- //connection = null;
- }
- }
- /*
- * Method sends message via datagram to client
- */
- private void sendMessage( String message ) throws IOException{
- byte[] message_byte = message.getBytes();
- packet_send = new DatagramPacket( message_byte , message_byte.length , packet_receive.getAddress() , packet_receive.getPort() );
- connection.send( packet_send );
- }
- /*
- * Method receives messages from clients
- */
- private String receiveMessage() throws IOException,NullPointerException{
- if (connection!=null){
- byte[] buf = new byte[256];
- packet_receive = new DatagramPacket( buf , buf.length ) ;
- connection.receive( packet_receive ) ;
- return getString( packet_receive );
- }
- throw new NullPointerException("Connection cannot be null");
- }
- /*
- * Method retrieves data from packet and converts to string
- */
- private String getString(DatagramPacket packet){
- String string = new String ( packet.getData() , 0 , packet.getLength()).trim();
- return string;
- }
- private void drop_DBconnection(){ if (DB_connection!=null){ try{ DB_connection.close(); //DB_connection = null;
- }
- catch(SQLException e){
- Txt_Log.setText("Server connection Error...");
- setError("Cannot close database connection");
- }
- }
- }
- public void closeServer(){
- if (connection != null){
- //connection.disconnect();
- connection.close();
- //connection = null;
- }
- }
Server started Server is started..... Error1 socket closed Server is stopped..... java.net.SocketException: socket closed at java.net.PlainDatagramSocketImpl.receive0(Native Method) at java.net.PlainDatagramSocketImpl.receive(PlainData gramSocketImpl.java:136) at java.net.DatagramSocket.receive(DatagramSocket.jav a:712) at server.Server.receiveMessage(Server.java:608) at server.Server.run(Server.java:528) at java.lang.Thread.run(Thread.java:619)
How do I close the server so that no exception is thrown?