【发布时间】: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