Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3936036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:53:55+00:00 2026-05-19T23:53:55+00:00

I have a server that waits for incoming client messages and uses UDP. When

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:53:56+00:00Added an answer on May 19, 2026 at 11:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a linux server program that waits for incoming connections from a client
I have a server that listens for a connection on a socket: public class
I have a simple server that waits for a client to connect, reads the
Let me try to explain what I need. I have a server that is
I have several server processes that once in a while respond to messages from
I have a TCP server that creates a (blocking) socket, waits until it is
I have a server that sends data via a socket, the data is a
I have a server that hosts my Subversion code base. That server is currently
I have a php server that is running my domain name. For testing purposes
I have a server application that receives information over a network and processes it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.