简体   繁体   English

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

[英]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.我试图在 mac 中使用 eclipse 运行 Java 套接字,但它不起作用。 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(); 
        } 
    } 
}

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.您无法打开 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 的端口。

Unix-based systems declare ports < 1024 as "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.在生产中部署服务器时,请使用管理员权限运行它。

I had the same issue and my port numbers were below 1024 changing port number to above 1024 solved my problem.我遇到了同样的问题,我的端口号低于 1024,将端口号更改为 1024 以上解决了我的问题。 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 的端口称为特权端口,在 Linux(以及大多数 UNIX 风格和类 UNIX 系统)中,任何非 root 用户都不允许打开它们。

Many systems declare ports that are less than 1024 as "admin rights" ports.许多系统将小于 1024 的端口声明为“管理员权限”端口。 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.意思是,如果您仅将此用于基本测试,请使用更高的端口,例如 2000。这将清除您通过运行当前程序获得的异常。

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

Change that to something such as:将其更改为以下内容:

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

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

}

Run Server program with root (Administrator).使用 root(管理员)运行服务器程序。

  • Windows: Run as Administrator the IDE/Editor. Windows:以管理员身份运行 IDE/编辑器。
  • Ubuntu/macOS: sudo java... Ubuntu/macOS: sudo java...

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.我正在使用端口号 8000,但仍然无法从 java 程序绑定到端口。 It was.network filter running as part of eset endpoint security that was blocking the connection.它是作为阻止连接的 eset 端点安全的一部分运行的网络过滤器。 I added a rule in eset firewall to allow port 8000, and it started working.我在 eset 防火墙中添加了一条允许端口 8000 的规则,它开始工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在OpenShift的Tomcat 7上创建ServerSocket时出现java.net.BindException - java.net.BindException When Creating ServerSocket on Tomcat 7 on OpenShift java.net.BindException:权限被拒绝为root - java.net.BindException: Permission denied as root Openshift-java.net.BindException:权限被拒绝 - Openshift - java.net.BindException: Permission denied 抛出java.net.BindException的单独线程中的ServerSocket:绑定失败:Android上的EACCES(权限被拒绝) - ServerSocket in seperate thread throwing java.net.BindException: bind failed: EACCES (Permission denied) on Android 在Mac上的Android中创建数据报套接字时出现java.net.BindException - java.net.BindException when creating datagram socket in Android on Mac java.net.BindException:绑定失败:尝试为UDP连接创建DatagramSocket时EACCES(权限被拒绝) - java.net.BindException: bind failed: EACCES (Permission denied) when trying to create DatagramSocket for UDP connection java.net.BindException:启动Wiser SMTP服务器时拒绝权限 - java.net.BindException: Permission denied when starting Wiser SMTP Server Eclipse:初始化端点java.net.BindException时出错:权限被拒绝:443 - Eclipse: Error initializing endpoint java.net.BindException: Permission denied:443 java.net.BindException:绑定失败:EACCES(权限被拒绝)TCP Server - java.net.BindException: bind failed: EACCES (Permission denied) TCP Server NameNode:java.net.BindException - NameNode: java.net.BindException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM