我想進入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),使用相同的內部和外部開始和結束端口並轉發到我的計算機(它正在運行服務器),但嘗試了遠程連接的端口轉發,但無濟於事。每次連接超時,沒有其他錯誤。
我的問題是:我的程序是否存在固有的錯誤,或者這更可能是路由器/防火牆問題?如果這是一個防火牆問題,我將如何解決它?任何幫助表示讚賞!
是否從不同的電腦工作在同一網絡上? – immibis 2015-02-10 02:01:15
它可以在同一網絡上的另一臺計算機上工作,無需使用私有IP進行端口轉發。使用本地主機和我的電腦的私有IP總是有效的:使用我的公共靜態IP根本不起作用,即使是在同一臺電腦上。 – cheeze 2015-02-10 02:44:26
如果它在同一網絡中的另一臺計算機上工作,那麼它很可能是一個端口轉發問題(因此對於SO來說是非題目)。 – immibis 2015-02-10 02:44:59