servidores servidor practica ejercicios con cliente java udp

java - practica - ¿Cómo cierro un servidor UDP sin lanzar una excepción?



sockets con udp java (1)

La excepción está cerrada porque cerró la excepción. Puede ver esta excepción y continuar en lugar de imprimirla. Esta es probablemente la mejor manera de manejar esto.

Tengo un servidor que espera mensajes entrantes del cliente y usa UDP. Cuando trato de cerrar la conexión de Udp obtengo una 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 ); } }

Mi salida y 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)

¿Cómo cierro el servidor para que no se produzca ninguna excepción?