java.net.BindException:在 Mac OSX 上创建 ServerSocket 时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25544849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:37:09  来源:igfitidea点击:

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

javamacossockets

提问by Kevbear

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

我试图用 Eclipse 在 mac 中运行 Java 套接字,但它不起作用。我收到此错误:

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

回答by Aaron Digulla

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

基于 Unix 的系统将小于 1024 的端口声明为“特权”,并且您需要管理员权限才能启动服务器。

For testing, use a port number >= 1024.

对于测试,请使用端口号 >= 1024。

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

在生产中部署服务器时,请使用管理员权限运行它。

回答by carlspring

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 100which confirms my theory.

您无法打开 1024 以下的端口,如果您没有 root 权限,并且从您在评论中发布的代码来看,您似乎正在尝试打开端口100,这证实了我的理论。

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

如果您在非 root 用户下运行代码,则需要使用高于 1024 的端口。

回答by JVD

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

}

回答by confused

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.

我遇到了同样的问题,我的端口号低于 1024,将端口号更改为 1024 以上解决了我的问题。低于 1024 的端口称为特权端口,在 Linux(以及大多数 UNIX 风格和类 UNIX 系统)中,任何非 root 用户都不允许打开它们。

回答by Cats4Life

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.

许多系统将小于 1024 的端口声明为“管理员权限”端口。意思是,如果您仅将此用于基本测试,请使用更高的端口,例如 2000。这将清除您通过运行当前程序获得的异常。

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

Change that to something such as:

将其更改为以下内容:

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