I have wanted to get into Java networking for some time now and, based on the Java networking tutorial, I have created my own server and client. Code:
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();
}
}
This code is both the server and the client in one program for user accessibility. However, I cannot quite get it to work properly. The code
-Works fine on localhost
-Works fine when connecting to the private IP of my computer
-Doesn't work when trying to connect from a different router
I have tried port forwarding for the long-distance connection, using a TCP/UDP port forward over one port (42424 in this case), using the same internal and external starting and ending port and forwarding to my computer (which is running the server) but to no avail. Every time the connection times out, no other errors.
My question is: is there something inherently wrong with my program, or is this more likely a router/firewall problem? And if it is a firewall problem, how would I fix it? Any help is appreciated!