java - 为什么我获得拒绝连接到端口 338 的权限?

标签 java exception permissions

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

为什么会出现以下错误?

输入输出错误 权限被拒绝

我正在使用端口 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);
        }
    }
}

最佳答案

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

在家里尝试更高的端口进行测试,你应该没问题。

关于java - 为什么我获得拒绝连接到端口 338 的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12744027/

相关文章:

java - Openshift 在 JBoss AS7 上部署失败,MYSQL 依赖错误

python - 从 sklearn 尝试 "LeaveOneGroupOut"时收到 python 异常

ios - 是什么原因导致此 iOS 权限提示 "use Bluetooth for new connections"?

java - 连接到在 eclipse 中运行的服务器套接字

java - 为什么有些 float 出现尾随 0

java - Android 在推送通知上设置 RTL 支持

c# - 'throw' 和 'throw new Exception()' 之间的区别

hadoop - 启动Hive CLI时出现奇怪的ServiceConfigurationError

.net - 在 .NET 中,检查当前用户是否可以写入目录

apache - Perl 不允许写入文件