Hey :) First off this might actually be a network problem! But im also uncertain of the java code and how networking works in java. The program is basically just to transfer a file if that matters! So this is my code for setting up the connections:

Server:

    //connection(s)
    private ServerSocket listener; //used to listen
    private Socket connection; //Opens the actual connection

    //variables
    private final int PORT = 443; //ports 80 / 443 / 3306/ 3389 is open on 83.170.73.***.

    //** constructor code and so forth nothing related to the connection part

    listener = new ServerSocket(PORT);
    text.append("Listening for new connections on: " + PORT); // updates a textfield for GUI

    //accept one incoming connection
    connection = listener.accept();

    //open streams and use the connection

Client:

    //connection info
    private String REMOTE_MACHINE = "127.0.0.1"; //"83.170.73.***";
    private int port = 443; //ports 443 / 3306/ 3389 is open on 83.170.73.***.

    //*constructor code, nothing related to the connection.

    connection = new Socket(REMOTE_MACHINE, port);
    text.append("\nConnected"); // updates a text field for the GUI

    //*open streams and use the connection

Since its only a few lines of code theres actually involved i think it might be a network problem, but according to http://www.yougetsignal.com/tools/open-ports/ atleast the ports in the comments above is open and should work. I dont have a firewall other than windows and the JVM is allowed through there (i assume its not individual .jar files i need to allow?).

My program works beatutifully when i use the local ip (127.0.0.1) but when i use the external ip it tries to connect for 10 seconds or so and then returns an EOFException or a ConnectException.
Is the code i did right? If so is there any other methods of setting up a connection that is more reliable(/easier for my stupid ISP to handle :) ).

Thanks in advance!

The fact that it works on localhost does seem to suggest a network problem such as firewall, but your code doesn't show your exception handling. Do you catch and printStackTrace() all possible exceptions?

No, im not. I did try all the ports that are commented out. I realize that all the ports im trying to use have a genarel use, but does that mean i cant use them in my own context?

Im sorry my knowlegde of networking is next to nothing :( And currently my internet is managed by my bulding association so i have no control over the router therefore i have to make good with what ports they've opened, and as far as i can tell the ports i've tried is the only ones that are open..

Erm.. Well i'm currently cathcing, in the client class that is: UnknownHostException, IOException and ConnectException. I think that manages most of them?
EDIT: and yes all the exceptions are printing a stack trace

Edited 4 Years Ago by Krokcy

I have tried both. I don't know what exceptions happened at the client machine when i tried over WAN since i didn't actually run the program myself. But the end result was the same.

Sorry, I'm out of ideas. Your code looks fine - that's the standard way to do it - so I'm going to put all my money on a network problem, with a firewall as the prime suspect. I don't think there's a Java solution to that!

Have you tested whether the ports are opened at the server? Use telnet host port command to see what you get. If you get connection refused, the port is closed. If you get accepted, it is opened and may be used by others at the time. If you get time out, it is being blocked by firewall.

Edited: Here is a list of how ports are being used.

Edited 4 Years Ago by Taywin

Ok! So the telnet command let me connect through port 443. (on a side note neat feature didnt even know it exisisted :) )

But i still get an EOFException. Its kind of wiered tho. Because the client actually thinks its connected, it opens all the streams and then it tries to read from the stream which is wehre i get the EOFException. But the server never accepted any connection.

Oh I got it... Your server does not keep listening... That's why it throws EOFException because your server gets the connection and drops it right away. You need a loop... Here is a simple server side code for you to look at.

import java.net.*;
import java.io.*;

public final class Server {
  public static void main (String args[]) {
    int serverPort = 2020;
    ServerSocket listen = null;
    try {
      listen = new ServerSocket(serverPort);
      System.out.println(" ~~ The server is running...");
      System.out.println("    To stop, use Ctrl+C");
      while(true) {
        Socket socket = listen.accept();
      } // end while
    } // end try
    catch(IOException e) {
      e.printStackTrace();
      System.exit(1);
    }
    finally {
      if(listen != null) {
        try { listen.close(); }
        catch(IOException e) { e.printStackTrace(); }
      }
    }
  }
}

Edited 4 Years Ago by Taywin

It does, i just cut it out because i figured it wasn't important oO . This is my actual code for the server:

try {
            ExecutorService executor = Executors.newFixedThreadPool(200);
            //start listening on PORT
            listener = new ServerSocket(PORT);
            text.append("Listening for new connections on: " + PORT);
            int count = 0; //counts threads/connections
            while(true) {
                //accept one incoming connection
                connection = listener.accept();
                text.append("\nConnected");
                sender = new SenderThread(connection.getInputStream(), connection.getOutputStream(), text, count); //thread that manages datastreams and sends the file
                executor.execute(sender); //starts the thread
                text.append("\nSERVER: Waiting for new connection");
                count++;
            }



        }catch(IOException e) {
            e.printStackTrace();
        }

Edited 4 Years Ago by Krokcy

What is connection variable? A Socket? May you try to declare new Socket inside it instead of declare it as global to the scope (see my code at line 13)?

Ye connection is a Socket. And i have changed it so i decalre it locally. It didn't, noticeably, change anything..

Hmm... If a port is opened but being used by other application/user, you can't use that port... You may try the telnet trick to see what other port is possible to try. Also, check the list on wikipedia I gave you too to see which port you are allow and which aren't?

Oka, thanks for your help! Do you know if there is a tool to check for open ports in a range rather then doing one at a time? Because i dont realy know wich ports are more likely to open/unsued ;s

You could run a script to call the command. Could you do any Ruby, Perl, Python, VBS, etc? They all are script languages that can easily call a command line and get result.

Ye I did it in a .bat file:

ECHO testing ports
%t%
set t=8400
:A
set /a t=%t%+1
telnet host %t%
IF NOT %t%==8500 GOTO A
pause

Again thanks for your help ! :)

This question has already been answered. Start a new discussion instead.