如何关闭UDP服务器而不引发异常?[英] How do I close a UDP server without throwing an exception?

问题描述

我有一个等待传入客户端消息并使用 UDP 的服务器.当我尝试关闭 Udp 连接时,我得到一个 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 );

        }       
}                                        

我的输出和堆栈跟踪

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)

如何关闭服务器不抛出异常?

推荐答案

异常被关闭,因为你关闭了异常.您可以捕获此异常并继续,而不是将其打印出来.这可能是处理此问题的最佳方法.

本文地址:https://www.itbaoku.cn/post/978528.html