2012-10-05 114 views
1

我今天從大學獲得了這個示例代碼,它在大學裏面工作得很好,但是當我在家用機器上運行它(使用Eclipse)時,我獲得了權限被拒絕的權限。大學的機器是Windows(7),我家裏的電腦是Linux(Ubuntu)。爲什麼我的權限被拒絕連接到338端口?

爲什麼我會收到以下錯誤?

錯誤I/O 權限被拒絕

我使用端口338

複製代碼:1024下

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); 
     } 
    } 
} 
+0

你在哪裏運行它得到消息(例外,作爲服務器的答案)?你正在使用哪個端口? – SJuan76

+0

什麼錯誤?哪裏? –

+0

哪個端口?哪個操作系統在大學/在家? –

回答

3

端口是最先進的操作系統的(包括Ubuntu的)特權,並要求您以管理員/ root或具有較高權限運行該程序。

嘗試更高的端口在家測試,你應該沒問題。

2

你說你的家用機器正在運行Ubuntu。

在Ubuntu(和其他類Unix操作系統),普通用戶不得少端口上偵聽端口超過1024

嘗試用端口號> = 1024

相關問題