Java NetBeans:Sending multiple Messages: Erroneous sym type: Soc

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Multiple Type
Click For Summary
The program developed in NetBeans for sending messages from a server to a client encounters a "java.net.BindException: Address already in use" error when attempting to send multiple messages. This occurs because a new ServerSocket is created each time the button is pressed without closing the previous one. To address this, methods for creating and closing connections were implemented, but a new error arises due to a typo in the code, where "Sockect" is incorrectly spelled instead of "Socket." Additionally, the closeConnection method fails to compile because it attempts to access the instance variable cSocket in a static context. Properly managing socket connections and correcting the spelling error are essential for resolving these issues.
zak100
Messages
462
Reaction score
11
Hi,

I made a program to send a message from a Server to a client on the same machine in NetBeans on a Button click. I used the following code in the button handler:
Java:
private void sendMsgButtonActionPerformed(java.awt.event.ActionEvent evt) { 

  // TODO add your handling code here:

  try{

  ServerSocket sSocket=new ServerSocket(800);

  System.out.println("Server Started");

  Socket cSocket=sSocket.accept();  PrintWriter writer=new PrintWriter(cSocket.getOutputStream());

  writer.println("Hello I am the Server");  writer.flush();

  writer.close();  Thread.currentThread().sleep(2000);

  cSocket.close();

  sSocket.close();

  }catch(SocketTimeoutException s)

  {

  System.out.println("Socket timed out!");

   break;

  }

  catch(IOException e) {

  e.printStackTrace();

  }  }
It worked but when i pressed the button again to send another message , i got following error:

java.net.BindException: Address already in use: JVM_Bind

at java.net.DualStackPlainSocketImpl.bind0(Native Method)

at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)

at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)

at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)

at java.net.ServerSocket.bind(ServerSocket.java:375)

at java.net.ServerSocket.<init>(ServerSocket.java:237)

at java.net.ServerSocket.<init>(ServerSocket.java:128)

at ServerFrame.sendMsgButtonActionPerformed(ServerFrame.java:83)

at ServerFrame.access$000(ServerFrame.java:13)

at ServerFrame$1.actionPerformed(ServerFrame.java:39)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)

at java.awt.Component.processMouseEvent(Component.java:6535)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)

at java.awt.Component.processEvent(Component.java:6300)

at java.awt.Container.processEvent(Container.java:2236)

at java.awt.Component.dispatchEventImpl(Component.java:4891)

at java.awt.Container.dispatchEventImpl(Container.java:2294)

at java.awt.Component.dispatchEvent(Component.java:4713)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)

at java.awt.Container.dispatchEventImpl(Container.java:2280)

at java.awt.Window.dispatchEventImpl(Window.java:2750)

at java.awt.Component.dispatchEvent(Component.java:4713)

at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)

at java.awt.EventQueue.access$500(EventQueue.java:97)

at java.awt.EventQueue$3.run(EventQueue.java:709)

at java.awt.EventQueue$3.run(EventQueue.java:703)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)

at java.awt.EventQueue$4.run(EventQueue.java:731)

at java.awt.EventQueue$4.run(EventQueue.java:729)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

To handle this error, i created two methods: createConnection( ) & closeConnection( ) but now i am getting following error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Sockect.close

at Server2JFrame.closeConnection(Server2JFrame.java:44)

at Server2JFrame.main(Server2JFrame.java:153)

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol

symbol: class Sockect

location: class Server2JFrame

at Server2JFrame.<init>(Server2JFrame.java:24)

at Server2JFrame$2.run(Server2JFrame.java:150)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)

at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)

at java.awt.EventQueue.access$500(EventQueue.java:97)

at java.awt.EventQueue$3.run(EventQueue.java:709)

at java.awt.EventQueue$3.run(EventQueue.java:703)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

C:\Users\HP\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 2 seconds)
My complete server code is:

Java:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author HP
*/
public class Server2JFrame extends javax.swing.JFrame {
    /**
     * Creates new form Server2JFrame
     */
    ServerSocket sSocket;
    Sockect cSocket;
    PrintWriter writer;
  
    public void createConnection( ){
         try{
         sSocket=new ServerSocket(800);
         System.out.println("Server Started");
         cSocket=sSocket.accept();    
        Thread.currentThread().sleep(2000);
      
        }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
    public static void closeConnection(){
        cSocket.close();
        sSocket.close();
    }
          
    public Server2JFrame() {
        createConnection( );
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        sendMesgButton = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        sendMesgButton.setText("Send Multiple Message");
        sendMesgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendMesgButtonActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(sendMesgButton)
                .addContainerGap(82, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(sendMesgButton)
                .addGap(35, 35, 35))
        );
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(121, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                      
    private void sendMesgButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        writer=new PrintWriter(cSocket.getOutputStream());
        writer.println("Hello I am the Server");
        writer.flush();
        writer.close();
    }                                            
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Server2JFrame().setVisible(true);
            }
        });
        closeConnection( );
    }
    // Variables declaration - do not modify                   
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton sendMesgButton;
    // End of variables declaration                 
}
and the client code is:

Java:
import java.net.*;
import java.io.*;
public class NewJFrame extends javax.swing.JFrame {
    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        recvMesgButton = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        recvMesgButton.setText("Received Client Message");
        recvMesgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recvMesgButtonActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addComponent(recvMesgButton)
                .addGap(34, 34, 34))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(recvMesgButton)
                .addContainerGap(71, Short.MAX_VALUE))
        );
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(85, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                      
    private void recvMesgButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
       try{
       Socket toServer=new Socket(InetAddress.getLocalHost(),800);
       BufferedReader bufr=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
       System.out.println(bufr.readLine());
       bufr.close();
       toServer.close();
       }catch(IOException e){
           e.printStackTrace();
            break;
       }
    }                                            
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                   
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton recvMesgButton;
    // End of variables declaration                 
}

Some body please guide me how to get around with this problem.

Zulfi.
 
Technology news on Phys.org
The first thing I do is to check for examples:

http://cs.lmu.edu/~ray/notes/javanetexamples/

and then see if my code is missing something.

It looks like your code isn't closing the socket or terminating correctly (ie maybe its still running the first version). Netbeans has an indicator at the bottom right that shows what processes are running. Sometimes though it gets confused and a process that you launched is running but it thinks it has ended.

Basically you need to still clean up stuff when you get an exception. What happens is when the exception is thrown everything that would normally be executed is skipped until your program has caught the exception then it gets back on track.
 
Last edited by a moderator:
It looks to me as if you are trying to start a server at port 800 every time you push the button. That is not allowed, and that is why you get the error messages. Try to check if the server is already started and do not create a new one if it is.
 
Hi,
I am now getting address already Bind. I have created separate methods for createConnection( ) & closeConnection( ). I have checked that i am not invoking these methods repeatedly. I am closing the conncections in the exception handling code.
Java:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author HP
*/
public class Server2JFrame extends javax.swing.JFrame {

    /**
     * Creates new form Server2JFrame
     */
    static ServerSocket sSocket;
    static Socket cSocket;
    static PrintWriter writer;
   
    public static void createConnection( ){
         try{
         sSocket=new ServerSocket(800);
         System.out.println("Server Started");
         cSocket=sSocket.accept();     
        //Thread.currentThread().sleep(2000);
       
        }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            closeConnection();
            break;
         }
        catch(IOException e) {
//            closeConnection();
            e.printStackTrace();
        }
    }
    public static void closeConnection(){
        try{
        cSocket.close();
        sSocket.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
           
    public Server2JFrame() {
       
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        sendMesgButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        sendMesgButton.setText("Send Multiple Message");
        sendMesgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendMesgButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(sendMesgButton)
                .addContainerGap(82, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(sendMesgButton)
                .addGap(35, 35, 35))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(121, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void sendMesgButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        writer=new PrintWriter(cSocket.getOutputStream());
        writer.println("Hello I am the Server");

        writer.flush();
        writer.close();
    }                                             

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        createConnection( );
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Server2JFrame().setVisible(true);
            }
        });
        closeConnection( );
    }

    // Variables declaration - do not modify                    
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton sendMesgButton;
    // End of variables declaration                  
}

& the errors are:
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at Server2JFrame.createConnection(Server2JFrame.java:29)
at Server2JFrame.main(Server2JFrame.java:152)
Exception in thread "main" java.lang.NullPointerException
at Server2JFrame.closeConnection(Server2JFrame.java:47)
at Server2JFrame.main(Server2JFrame.java:159)
BUILD SUCCESSFUL (total time: 7 seconds)

Somebody please guide me.
Zulfi.
 
Did you look at the examples link I posted earlier? Surely there's something that will help you.

You either have something running on your machine that is using port 800 or one your earlier runs is still running.

If you reboot does the program still say that its in use already if o then some other process on your machine is using that port and depending on your OS you can check to see who is via the command. Here's the Linux one:

http://askubuntu.com/questions/538208/how-to-check-opened-closed-port-on-my-computer
 
I am no Java expert, but I do know something about networking. When tracing through the error messages it ends up in "Address already in use". As already commented:
jedishrfu said:
You either have something running on your machine that is using port 800 or one your earlier runs is still running.
I shall only add that port numbers below 1024 are so-called "well-known ports" and should not be used for anything but the "well-known applications". From rfc 1700:

The assigned ports use a small portion of the possible port numbers. For many years the assigned ports were in the range 0-255. Recently, the range for assigned ports managed by the IANA has been expanded to the range 0-1023.

Port Assignments:

Keyword Decimal Description References
------- ------- ----------- ----------
...
mdbs_daemon 800/tcp
mdbs_daemon 800/udp
 
  • Like
Likes jedishrfu
Hi,
Thanks my friends for your guidance.
<Did you look at the examples link I posted earlier? >
I have tried to change my code according to the first example in the document. But now I m not getting the "Bind error" but its not showing the Button.
It prints the message server started.
I tried to use debugger but now run to Cursor option has grayed. So i can't use it.

My code is:
Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author HP
*/
public class Server2JFrame extends javax.swing.JFrame {

    /**
     * Creates new form Server2JFrame
     */
    static ServerSocket sSocket;
    static Socket cSocket;
    static PrintWriter writer;
   
    public static void createConnection( ){
         try{
         sSocket=new ServerSocket(9090);
         System.out.println("Server Started");
         cSocket=sSocket.accept();     
        //Thread.currentThread().sleep(2000);
       
        }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            closeConnection();
           
         }
        catch(IOException e) {
//            closeConnection();
            e.printStackTrace();
        }
    }
    public static void closeConnection(){
        try{
        cSocket.close();
        sSocket.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
           
    public Server2JFrame() {
       
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        sendMesgButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        sendMesgButton.setText("Send Multiple Message");
        sendMesgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendMesgButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(sendMesgButton)
                .addContainerGap(82, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addComponent(sendMesgButton)
                .addGap(35, 35, 35))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(121, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void sendMesgButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        try{
        writer=new PrintWriter(cSocket.getOutputStream(),true);
        writer.println("Hello I am the Server");

        writer.flush();
        writer.close();
        }catch(IOException e) {
            e.printStackTrace();
            try {
            cSocket.close();
            }catch(IOException ee){
               
            }
        }
       
    }                                             

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Server2JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        createConnection( );
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Server2JFrame().setVisible(true);
            }
        });
        closeConnection( );
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton sendMesgButton;
    // End of variables declaration                   
}

Some body please guide me why i am not seeing the button.

Zulfi.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 27 ·
Replies
27
Views
23K
  • · Replies 9 ·
Replies
9
Views
11K
  • · Replies 31 ·
2
Replies
31
Views
12K