2015-02-10 51 views
0

我想進入Java網絡一段時間了,基於Java網絡教程,我創建了自己的服務器和客戶端。代碼:簡單的Java服務器不工作

package com.gmail.dudewithtude42.projectfilos; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Scanner; 

public class ProjectFilos { 
    public static void main(String[] args) { 
     System.out.println("Main Menu\n1. Host Game\n2. Join Game"); 
     Scanner localScanner=new Scanner(System.in); 
     int port; 
     inputLoop:while (true){ 
      String choice=localScanner.nextLine(); 
      switch (choice){ 
      case "1": 
       System.out.println("What port do you want to use?"); 
       port=localScanner.nextInt(); 
       try{ 
        System.out.println("Generating socket..."); 
        ServerSocket serverSocket = new ServerSocket(port); 
        System.out.println("Connecting socket..."); 
        Socket clientSocket=serverSocket.accept(); 
        System.out.println("Creating server output..."); 
        PrintWriter serverOutput=new PrintWriter(clientSocket.getOutputStream(),true); 
        System.out.println("Creating server input..."); 
        BufferedReader serverInput=new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
        System.out.println("All systems operational."); 
        String inputLine; 
        while ((inputLine=serverInput.readLine())!=null){ 
         serverOutput.println(inputLine); 
         System.out.println(inputLine); 
        } 
        serverSocket.close(); 
       }catch (IOException serverException) { 
        serverException.printStackTrace(); 
       } 
       break inputLoop; 
      case "2": 
       System.out.println("What is the IP address of the server?"); 
       InetAddress serverAddress=null; 
       try{ 
        serverAddress=InetAddress.getByName(localScanner.nextLine()); 
       }catch (UnknownHostException ipException) { 
        ipException.printStackTrace(); 
       } 
       System.out.println("What port do you want to connect to?"); 
       port=localScanner.nextInt(); 
       try{ 
        System.out.println("Generating socket..."); 
        Socket connectionSocket=new Socket(serverAddress,port); 
        System.out.println("Creating output..."); 
        PrintWriter clientOutput=new PrintWriter(connectionSocket.getOutputStream(),true); 
        System.out.println("Creating server input..."); 
        BufferedReader clientInput=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
        System.out.println("Creating standard/local input..."); 
        BufferedReader clientStandardInput=new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("All systems operational."); 
        String userInput; 
        while ((userInput=clientStandardInput.readLine())!=null){ 
         clientOutput.println(userInput); 
         System.out.println("echo: "+clientInput.readLine()); 
        } 
        connectionSocket.close(); 
       }catch (IOException clientException){ 
        clientException.printStackTrace(); 
       } 
       break inputLoop; 
      default: 
       break; 
      } 
     } 
     localScanner.close(); 
    } 
} 

此代碼既是服務器又是客戶端在一個程序中的用戶可訪問性。但是,我無法完全正確地工作。該代碼

-Works在localhost精細

-Works罰款時,連接到我的電腦的專用IP

試圖從不同的路由器

我有連接時-Doesn't工作嘗試通過一個端口轉發TCP/UDP端口(本例中爲42424),使用相同的內部和外部開始和結束端口並轉發到我的計算機(它正在運行服務器),但嘗試了遠程連接的端口轉發,但無濟於事。每次連接超時,沒有其他錯誤。

我的問題是:我的程序是否存在固有的錯誤,或者這更可能是路由器/防火牆問題?如果這是一個防火牆問題,我將如何解決它?任何幫助表示讚賞!

+0

是否從不同的電腦工作在同一網絡上? – immibis 2015-02-10 02:01:15

+0

它可以在同一網絡上的另一臺計算機上工作,無需使用私有IP進行端口轉發。使用本地主機和我的電腦的私有IP總是有效的:使用我的公共靜態IP根本不起作用,即使是在同一臺電腦上。 – cheeze 2015-02-10 02:44:26

+0

如果它在同一網絡中的另一臺計算機上工作,那麼它很可能是一個端口轉發問題(因此對於SO來說是非題目)。 – immibis 2015-02-10 02:44:59

回答

0

要測試它是否是配置問題,首先要做的就是使用tcpdump或wireshark進行測試。檢查TCP SYN消息是否到達服務器,如果是,我們放棄與端口轉發有關的配置問題。 然後檢查SYN消息是否被來自服務器的SYN/ACK響應。如果沒有響應,可能是由於本地防火牆阻塞連接。

我不知道客戶端嘗試連接時是否超時。它發生在這裏嗎?

Socket connectionSocket=new Socket(serverAddress,port); 

後你解決這個問題,你必須考慮這種與服務器的通信:How to read all of Inputstream in Server Socket JAVA

+0

我相信這是一個端口轉發問題。在端口轉發後,我發現並使用了顯示所有活動TCP端口轉發的命令「netstat -anp tcp」,並發現即使將端口轉發到我的計算機後,它也沒有顯示在netstat下。這意味着這是一個端口轉發問題,因此被認爲是StackOverflow的主題。不過,感謝您的幫助。 – cheeze 2015-02-12 22:05:13

+0

@cheeze,如果你運行我提到的工具,你可以有更多的細節。在這些可能性中,問題可能是實際接收到SYN,但由於某種原因不會返回SYN/ACK。在這種情況下,您將看不到活動的TCP連接,而端口轉發可以正確設置,並且問題在其他地方。這是可能性之一。祝你好運。 – rodolk 2015-02-13 14:47:08