BrandB
BrandB

Reputation: 3

Java error: java.net.BindException: Address already in use: JVM_Bindin server run class

When I run the client program I get this error. I checked to see if the port number is being used and already tried changing the port multiple times.

the server uses three classes:

the main and gui

class ServerGui extends Server implements ActionListener
{
public ServerGui() throws Exception
{
    JFrame jfrm = new JFrame("Check in Desk");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(500, 500);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setVisible(true);
    //menu bar
    JMenuBar jmb = new JMenuBar();
    JMenu jmView = new JMenu("View");
    // items
    JMenu jmEdit = new JMenu("Edit");
    // items
    JMenu jmHelp = new JMenu("Help");
    // items
    jmb.add(jmView);
    jmb.add(jmEdit);
    jmb.add(jmHelp);
    //add menuBar
    jfrm.setJMenuBar(jmb);

}

public void actionPerformed(ActionEvent ae)
{
    String comString = ae.getActionCommand();
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() 
        {
            public void run()
            {                 
                try{
                    new ServerGui();
                }
                catch(Exception e)
                {
                    System.out.println(e + "damn");
                }
            }
        });
}
}

The server class

public class Server extends Thread
{

public Server() throws IOException
{               
    this.start();       
}

public void run()
{     
    try
    {
        while(true)
        {
            ServerSocket socketOnWhichToListenForClients= new ServerSocket(9876);  
            Socket socketBackToClient = socketOnWhichToListenForClients.accept();
            new ClientHandler(socketBackToClient);
        }
    }
    catch(Exception e)
    {
        System.out.println(e + "in server run class");
    }
}

}

client handler

public class ClientHandler extends Thread
{
private Socket socketBackToClient;
public ClientHandler(Socket socket)
{
    socketBackToClient = socket;
    this.start();
}
public void run()
{
    try
    {
       InputStream is = socketBackToClient.getInputStream();
       BufferedReader message = new BufferedReader(new InputStreamReader(is));
       String input = message.readLine();
       System.out.println(input);
       socketBackToClient.close();

    }
    catch(Exception e)
    {
        System.out.println("Error");
    }
}
}

The client gui

public class ClientGui extends Client implements ActionListener
{
//private String name = "";
public ClientGui() throws Exception
{
    name = JOptionPane.showInputDialog("Enter your name.");
    if(name.equals("")) 
        return;  
    JFrame jfrm = new JFrame("Check in Desk");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(300, 170);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.setVisible(true);
    jfrm.setResizable(false);
    //buttons
    Font font1 = new Font("SansSerif",Font.PLAIN, 24);
    Font font2 = new Font("SansSerif",Font.PLAIN, 20);

    JButton jb1 = new JButton("Help");
    jb1.setPreferredSize(new Dimension(100, 100));
    jfrm.add(jb1);
    JButton jb2 = new JButton("Check In");
    jb2.setPreferredSize(new Dimension(125, 100));
    jfrm.add(jb2);

    jb1.setFont(font1);
    jb2.setFont(font2);
    //menu bar
    JMenuBar jmb = new JMenuBar();
    JMenu jmTools = new JMenu("Tools");
    JMenuItem jmiName = new JMenuItem("Name");
    JMenuItem jmiIP = new JMenuItem("Your IP");
    jmTools.add(jmiName);
    jmTools.add(jmiIP);
    // items
    JMenu jmEdit = new JMenu("Edit");
    // items
    JMenu jmHelp = new JMenu("Help");
    JMenuItem jmiAbout = new JMenuItem("About");
    jmHelp.add(jmiAbout);
    // items
    jmb.add(jmTools);
    jmb.add(jmEdit);
    jmb.add(jmHelp);
    //add menuBar
    jfrm.setJMenuBar(jmb);

    jmiName.addActionListener(this);
    jmiIP.addActionListener(this);
    jb1.addActionListener(this);
    jb2.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)
{
    String confirmHelp = "";
    String confirmCheckIn = "";
    if(ae.getActionCommand().equals("Name"))
        JOptionPane.showMessageDialog(null,"Your name is " + getClientName());
    if(ae.getActionCommand().equals("Your IP"))
        JOptionPane.showMessageDialog(null,"Your IP is " + getIpAddress());
    if(ae.getActionCommand().equals("Help"))
    {
        confirmHelp = JOptionPane.showInputDialog("Are You Sure You Want Help?");
        if(confirmHelp != null && confirmHelp.equalsIgnoreCase("yes"))
        {
            Help();
        }
    }
    if(ae.getActionCommand().equals("Check In"))
    {
    confirmCheckIn = JOptionPane.showInputDialog("Are You Sure You Want To Check       In?");
        if(confirmCheckIn != null && confirmCheckIn.equalsIgnoreCase("yes"))
        {
            CheckIn();
        }

    }
}

public static void main(String[] args)
{

    SwingUtilities.invokeLater(new Runnable() 
        {
            public void run()
            {

                try{
                    new ClientGui();
                    Socket socket = new Socket(InetAddress.getLocalHost(), 9876);              //new Client(); // add constructor with name or pc numb
                }
                catch(Exception e)
                {
                    System.out.println("client closed");
                }
            }
        });
}
}

The client

public class Client
{
public String name = "";
Socket clientSocket;
public Client() throws Exception
{

    try
    {
       // clientSocket = new Socket(InetAddress.getLocalHost(), 9);
        OutputStream os = clientSocket.getOutputStream();
        PrintWriter pwrite = new PrintWriter(os, true);
        pwrite.print("yolo");
    }
    catch(Exception e)
    {
        //System.out.println("Error cant connect");   add later
    }
}


public void Help()
{
    System.out.println("you pressed help");
}

public void CheckIn()
{
    System.out.println("you pressed Check In");
}

public String getIpAddress()
{
    String str = "";
    try{
        InetAddress ip = InetAddress.getLocalHost();
        str = ip.toString();
    }
    catch(UnknownHostException e)
    {
        System.out.println("can not find IP address");
    }

    return str;
}

public String getClientName()
{
    return name;
}

}

Upvotes: 0

Views: 2459

Answers (2)

user207421
user207421

Reputation: 310860

The problem is that you keep recreating the ServerSocket inside the while loop. Create it once, before the loop.

Upvotes: 1

Braj
Braj

Reputation: 46841

The problem is at below lines where same port is used in loop to create ServerSocket that results into below exception.

try {
    while (true) {
        ServerSocket socketOnWhichToListenForClients = new ServerSocket(9876);
    }
} catch (Exception e) {
    System.out.println(e + "in server run class");
}

Look at at the exception:

java.net.BindException: Address already in use: JVM_Bindin server run class

Don't forget to close the ServerSocket in the end. Use finally block to close it.

Upvotes: 0

Related Questions