【问题标题】:Why do I get permission denied connecting to port 338?为什么我的权限被拒绝连接到端口 338?
【发布时间】:2012-09-26 11:29:06
【问题描述】:

我今天从大学获得了这个示例代码,它在大学内部运行良好,但是当我在家用机器上运行它(使用 Eclipse)时,我的权限被拒绝。大学的机器是Windows(7),我家的电脑是Linux(Ubuntu)。

为什么会出现以下错误?

I/O 错误 权限被拒绝

我使用的是 338 端口。

代码副本:

import java.io.*;
import java.net.*;
import java.util.*;

public class Server 
{
    public static void main(String[] args)
    {
        try
        {
            // First create the input from the keyboard
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Server Program");

            // Get the port to listen on
            System.out.print("Enter port number to listen on: ");
            String port_string = input.readLine();

            // The port number needs to be an int, so convert the String to an int
            int port = Integer.parseInt(port_string);

            // Create a ServerSocket to listen on this address
            ServerSocket server = new ServerSocket(port);

            // Accept an incoming client connection on the server socket
            Socket sock = server.accept();

            // Create the output stream to the client
            DataOutputStream network = new DataOutputStream(sock.getOutputStream());

            // Send message
            network.writeUTF("Welcome " + sock.getInetAddress().getHostName() + ". We are " + new Date() + "\n");

            // Close sockets.  This will cause the client to exit
            sock.close();
            server.close();
        }
        catch (IOException ioe)
        {
            System.err.println("Error in I/O");
            System.err.println(ioe.getMessage());
            System.exit(-1);
        }
    }
}

【问题讨论】:

  • 你从哪里得到消息(例外,作为对服务器的回答)?您使用的是哪个端口?
  • 哪个端口?大学/家里使用哪种操作系统?
  • 注意:在类 Unix 操作系统上,小于 1024 的端口号只有在你有 root 权限的情况下才能使用。尝试端口号 >= 1024。
  • 可能是各种各样的东西。 a)该端口不在公共范围内,因此可能已被占用。另外,b),防火墙可能阻塞了端口?

标签: java exception permissions


【解决方案1】:

1024 以下的端口在大多数现代操作系统(包括 Ubuntu)上具有特权,并要求您以管理员/root 或提升的特权运行程序。

在家里试试更高的端口测试应该没问题。

【讨论】:

    【解决方案2】:

    你说你家的机器运行的是 Ubuntu。

    在 Ubuntu(和其他类 Unix 操作系统)上,普通用户不允许监听小于 1024 端口的端口。

    尝试使用 >= 1024 的端口号运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      相关资源
      最近更新 更多