0

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!

Jake
  • 13
  • 4
  • Does it work from a different computer on the same network? – user253751 Feb 10 '15 at 02:01
  • It works from another computer on the same network without port forwarding using the private IP. Using localhost and the private IP of my comp always works: using my public static IP doesn't work at all, even from the same computer. – Jake Feb 10 '15 at 02:44
  • If it works from another computer in the same network, then it is most likely a port forwarding problem (therefore off-topic for SO). – user253751 Feb 10 '15 at 02:44
  • Thank you for the help anyway! I'll see what I can do. – Jake Feb 10 '15 at 02:46

1 Answers1

0

To test if it is a configuration problem the first thing you have to do is to test with tcpdump or wireshark. Check if a TCP SYN message is arriving to the server, if it is, we discard a configuration problem related to port forwarding. Then check the SYN message is responded with a SYN/ACK from the server. If it is not responded it could be due to local firewall blocking connections.

I don't know if the timeout is when the client tries to connect. Is it happening here?

Socket connectionSocket=new Socket(serverAddress,port);

After you solve this problem you must consider this for the communication with the server: How to read all of Inputstream in Server Socket JAVA

Community
  • 1
  • 1
rodolk
  • 4,969
  • 3
  • 22
  • 32
  • I believe it's a port forwarding problem. After I port forwarded, I found and used the command "netstat -anp tcp" which shows all active TCP port forwards and found that even after port forwarding that port to my computer, it didn't show up under netstat. That means that it's a port forwarding problem and thus considered off topic for StackOverflow. Thanks for the help, though. – Jake Feb 12 '15 at 22:05
  • @cheeze, if you run the tools I mentioned you can have more details. Among the possibilities, the problem could be that a SYN is actually received but for some reason a SYN/ACK is not returned. In that case you won't see the active TCP connection while Port Forwarding could be correctly set and the problem is somewhere else. It's one of the possibilities. Good luck. – rodolk Feb 13 '15 at 14:47