Hello, i have a problem when im trying to connect client-server.
I have tried it at 127.0.0.1 and it works perfectly and also at lan with other computers. However, i cannot make it work at internet with the error message connection refused. Why i am getting this error and how can i fix it?
notes:
i get my ip from whatsmyip...., i have disabled the window firewall and from other programs and i have port forward the specific port i want to use, in our case 6112.
Here is the source (which i actually found over the internet but thats another story):
SERVER:
import java.io.*; import java.net.*; import java.util.*; /** When started allows one client to connect. It listens on port 8189. * It returns whatever a connected client sends. * It shuts down when the client sends a Bye line. * */ public class SimpleServer{ /** Instantiates an instance of the SimpleServer class and initilaizes it. */ public static void main(String[] args){ SimpleServer simpleserver = new SimpleServer(); simpleserver.init(); } /** Sets up a ServerSocket and listens on port 8189. */ public void init(){ ServerSocket serversocket = null; Socket socket = null; try{ //establish a server socket monitoring port 8189 //port 8189 is not used by any services serversocket = new ServerSocket(6112); System.out.println("Listening at 127.0.0.1 on port 8189"); //wait indefinitely until a client connects to the socket socket = serversocket.accept(); //set up communications for sending and receiving lines of text data //establish a bufferedreaderr from the input stream provided by the socket object InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); //establish an printwriter using the output streamof the socket object //and set auto flush on PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true); //for binary data use // DataInputStream and DataOutputStream //for serialized objects use // ObjectInputStream and ObjectOutputStream String datetimestring = (Calendar.getInstance()).getTime().toString(); printwriter.println("You connected to the Simple Server at " + datetimestring); printwriter.println("Send Bye to disconnect."); String lineread = ""; boolean done = false; while (((lineread = bufferedreader.readLine()) != null) && (!done)){ System.out.println("Received from Client: " + lineread); printwriter.println("You sent: " + lineread); if (lineread.compareToIgnoreCase("Bye") == 0) done = true; } System.out.println("Closing connection."); bufferedreader.close(); inputstreamreader.close(); printwriter.close(); socket.close(); }catch(UnknownHostException unhe){ System.out.println("UnknownHostException: " + unhe.getMessage()); }catch(InterruptedIOException intioe){ System.out.println("Timeout while attempting to establish socket connection."); }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); }finally{ try{ socket.close(); serversocket.close(); }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); } } } }
and the client:
import java.io.*; import java.net.*; /** Connects to a SimpleServer which is listening on port 8189 */ public class SimpleClient{ String serverurl = "editted by me"; int serverport = 6112; /** Instantiates an instance of the SimpleClient class and initilaizes it. */ public static void main(String[] args){ SimpleClient simpleclient = new SimpleClient(); simpleclient.init(); } /** Connects to the SimpleServer on port 8189 and sends a few demo lines * to the server, and reads, displays the server reply, * then issues a Bye command signaling the server to quit. */ public void init(){ Socket socket = null; try{ System.out.println("Connecting to " + serverurl + " on port " + serverport); socket = new Socket(serverurl,serverport); //Set socket timeout for 10000 milliseconds or 10 seconds just //in case the host can't be reached socket.setSoTimeout(10000); System.out.println("Connected."); InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); //establish an printwriter using the output streamof the socket object //and set auto flush on PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true); printwriter.println("Hey howze it going today!"); printwriter.println("This is some anonymous user on the client side."); printwriter.println("Let's see what happens when I type this."); printwriter.println("Oh well, I've had enough."); printwriter.println("Bye"); String lineread = ""; while ((lineread = bufferedreader.readLine()) != null){ System.out.println("Received from Server: " + lineread); } System.out.println("Closing connection."); bufferedreader.close(); inputstreamreader.close(); printwriter.close(); socket.close(); System.exit(0); }catch(UnknownHostException unhe){ System.out.println("UnknownHostException: " + unhe.getMessage()); }catch(InterruptedIOException intioe){ System.out.println("Timeout while attempting to establish socket connection."); }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); }finally{ try{ socket.close(); }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); } } } }
Thank you.