32

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(); 
        } 
    } 
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Kevbear
  • 341
  • 1
  • 4
  • 10
  • what version of jdk are you using – Ker p pag Aug 28 '14 at 09:07
  • 1
    Without the code you are using it will be much harder. –  Aug 28 '14 at 09:08
  • We don't know enough to properly answer the question. What is the code? What JDK are you using? – DonyorM Aug 28 '14 at 09:12
  • I'm using jdk 1.6 and the code for MessageServer.java – Kevbear Aug 28 '14 at 09:16
  • 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(); } } } – Kevbear Aug 28 '14 at 09:17
  • 1
    @Kevbear You can see for yourself that posting code in comments is utterly illegible, i.e. a complete waste of time. Edit it into your question. – user207421 Aug 28 '14 at 09:43
  • 1
    @Kevbear: When you ask a question and somebody replies, the polite thing to do would be to accept an answer. – carlspring Jan 12 '15 at 20:31

7 Answers7

70

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.

carlspring
  • 31,231
  • 29
  • 115
  • 197
26

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.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • is it possible to run it with root privileges? – kurumkan Apr 16 '16 at 08:07
  • 1
    Yes, that should make it work but it also means that the whole application now has root access. So you need to be much more careful what the code could possibly do if someone was evil. – Aaron Digulla Apr 19 '16 at 12:19
  • Thank you Aaron. So I decided to use only over 1024 ports – kurumkan Apr 19 '16 at 13:29
  • 1
    Is this unix-based design level decision as ridiculous as Billion Dollar Mistake? If it was made based on scary assumption: what if someone was scary in deed, then how that guy got the access credentials? Otherwise: now there is a caste of exclusive ports up to 1023. – Oleksii Kyslytsyn Oct 26 '17 at 21:16
4

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.

confused
  • 151
  • 4
2

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); 
Cats4Life
  • 31
  • 7
0

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

}
JVD
  • 31
  • 5
0

Run Server program with root (Administrator).

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

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.