'java.net.BindException: Permission denied when creating a ServerSocket on Mac OSX

I Tried to run a Java socket in mac with eclipse but it doesn't work. I got this error:

Exception in thread "main" java.net.BindException: Permission denied

    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:521)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:414)
    at java.net.ServerSocket.bind(ServerSocket.java:326)
    at java.net.ServerSocket.<init>(ServerSocket.java:192)
    at java.net.ServerSocket.<init>(ServerSocket.java:104)
    at server.MessageServer.main(MessageServer.java:11)

How can i make it to run?

package server; //ChatServer 
import java.io.*; 
import java.net.*; 

public class MessageServer { 

public static void main (String args[]) throws IOException { 
    int port = 100; 
    ServerSocket server = new ServerSocket (port); 
    System.out.println("Server is started!"); 

    while (true) { 
        Socket client = server.accept (); 
        System.out.println ("Accepted from " + client.getInetAddress ()); 
        MessageHandler handler = new MessageHandler (client); 
        handler.start(); 
        } 
    } 
}


Solution 1:[1]

You can't open a port below 1024, if you don't have root privileges and from the code you posted in your comment, you seem to be trying to open port 100 which confirms my theory.

You need to use a port which is higher than 1024, if you're running the code under a non-root user.

Solution 2:[2]

Unix-based systems declare ports < 1024 as "privileged" and you need admin rights to start a server.

For testing, use a port number >= 1024.

When deploying the server in production, run it with admin rights.

Solution 3:[3]

I had the same issue and my port numbers were below 1024 changing port number to above 1024 solved my problem. Ports below 1024 are called Privileged Ports and in Linux (and most UNIX flavors and UNIX-like systems), they are not allowed to be opened by any non-root user.

Solution 4:[4]

Many systems declare ports that are less than 1024 as "admin rights" ports. Meaning, if you're only using this for basic testing use a higher port such as 2000. This will clear the exception that you're getting by running your current program.

    int port = 100; 
    ServerSocket server = new ServerSocket (port); 

Change that to something such as:

    int port = 2000; 
    ServerSocket server = new ServerSocket (port); 

Solution 5:[5]

MyServer.java

import java.io.*;
import java.net.*;
public class MyServer
{
    ServerSocket ss;
    Socket s;
    DataOutputStream dos;
    DataInputStream dis;
    public MyServer()
    {
        try 
        {
        System.out.println("Server Started ");
        ss=new ServerSocket(4444);
        s=ss.accept();
        System.out.println(s);
        System.out.println("Client Connected");
        dis=new DataInputStream(s.getInputStream());
        dos=new DataOutputStream(s.getOutputStream());
        ServerChat();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }   
    public static void main(String arg[])
    {
        new MyServer();
    }
    public void ServerChat()throws IOException
    {

        String str;
        do
        {
        str=dis.readUTF();
        System.out.println("Client msg : "+str);
        dos.writeUTF("Hello "+str);
        dos.flush();
        }while(!str.equals("stop"));

    }

}

MyClient.java

import java.io.*;
import java.net.*;
public class MyClient
{
    Socket s;
    DataInputStream din;
    DataOutputStream dout;
    public MyClient()
    {
    try
    {
        s=new Socket("localhost",4444);
        System.out.println(s);
        din = new DataInputStream(s.getInputStream());
        dout = new DataOutputStream(s.getOutputStream());
        ClientChat();
    }
    catch(Exception e)
    {
    System.out.println(e);  
    }   
    }
    public void ClientChat() throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s1;
    do
    {
    s1=br.readLine();
    dout.writeUTF(s1);
    dout.flush();
    System.out.println("Server Msg : "+din.readUTF());
    }while(!s1.equals("stop"));
    }
    public static void main(String arg[])
    {
    new MyClient();
    }

}

Solution 6:[6]

Run Server program with root (Administrator).

  • Windows: Run as Administrator the IDE/Editor.
  • Ubuntu/macOS: sudo java...

Solution 7:[7]

This is an old question, and I might be replying too late, but I would like to anyways share my experience in case anyone hits the issue.

I was using port# 8000, but still unable to bind to the port from a java program. It was network filter running as part of eset endpoint security that was blocking the connection. I added a rule in eset firewall to allow port 8000, and it started working.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community
Solution 3 confused
Solution 4 Cats4Life
Solution 5 JVD
Solution 6 Huỳnh Tấn Lực
Solution 7 user4768441