Kevbear
Kevbear

Reputation: 361

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(); 
        } 
    } 
}

Upvotes: 34

Views: 61099

Answers (9)

zakmck
zakmck

Reputation: 3014

There are several ways to overcome this:

  • Run the service under root (not very safe)
  • Use a different port
  • Use a different port and (reverse) proxy to it via an upfront web server (my preferred one is nginx)
  • Proxy/forward from 80 to another port via firewall (allows for port only, web proxy has more options)
  • In Linux, run your service through systemctl, see here

Upvotes: 0

Rahul Jha
Rahul Jha

Reputation: 1

Apache Tomcat: java.net.BindException: Permission denied (Bind failed) :443

I was struggling with this error and after hours figured out that root user was not logged in and the user was restarting tomcat without sudo.

if you face this issue, try logging as root and then restart tomcat.

Upvotes: 0

user4768441
user4768441

Reputation: 1

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.

Upvotes: 0

Huỳnh Tấn Lực
Huỳnh Tấn Lực

Reputation: 45

Run Server program with root (Administrator).

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

Upvotes: 0

Cats4Life
Cats4Life

Reputation: 41

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); 

Upvotes: 2

confused
confused

Reputation: 161

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.

Upvotes: 4

JVD
JVD

Reputation: 31

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();
    }

}

Upvotes: 0

carlspring
carlspring

Reputation: 32677

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.

Upvotes: 73

Aaron Digulla
Aaron Digulla

Reputation: 328760

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.

Upvotes: 28

Related Questions