How do I close a UDP server without throwing an ex

2019-02-20 23:09发布

问题:

I have a server that waits for incoming client messages and uses UDP. When I try to close the Udp connection I get an IOException..

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;

        }
}

private void btn_stopActionPerformed(java.awt.event.ActionEvent evt) {

        if ( serverStarted ){

                    Txt_Log.setText( Txt_Log.getText()+"\nServer has been stopped ...." ) ;
                    serverStarted = false ;
                    drop_DBconnection() ;
                    closeServer();

                    btn_start.setEnabled( true ) ;
                    btn_stop.setEnabled( false );

        }       
}                                        

My output and stacktrace

Server started
Server is started..... Error1 socket
closed Server is stopped.....
java.net.SocketException: socket closed
         at java.net.PlainDatagramSocketImpl.receive0(NativeMethod)
         at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
         at java.net.DatagramSocket.receive(DatagramSocket.java: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?

回答1:

The exception is closed because you closed the exception. You can catch this exception and continue rather than printing it out. This is likely to be the best way to handle this.



标签: java udp