Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: ServerAndClient via Ip

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question ServerAndClient via Ip

    Hey,
    Iam trying to connect a server with a client via public ip, but it is always is unable to connect.
    When I try it with using localhost (in the same network) its working fine.

    I already forwarded my ports in my router and firewall, but it will not connect when I use my
    public ip address.

    Error
    Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    	at java.net.PlainSocketImpl.connect(Unknown Source)
    	at java.net.SocksSocketImpl.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.connect(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at java.net.Socket.<init>(Unknown Source)
    	at main.ClientMain.main(ClientMain.java:10)

    Client
    public class ClientMain {
    	public static void main(String[] args) throws IOException {
    		Socket client = new Socket("localhost", 4545);
     
    		DataOutputStream output = new DataOutputStream(client.getOutputStream());
    		output.writeUTF("Message from the client.");
     
    		DataInputStream input = new DataInputStream(client.getInputStream());
    		System.out.println(input.readUTF());
     
    		client.close();
    	}
    }

    Server
    public class ServerMain {
    	public static void main(String[] args) throws IOException {
    		ServerSocket server = new ServerSocket(4545);
     
    		while(true) {
    			System.out.println("Waiting for client on port " + server.getLocalPort() + ".");
    			Socket client = server.accept();
     
    			System.out.println("Client connected from " + client.getInetAddress() + ".");
     
    			DataInputStream input = new DataInputStream(client.getInputStream());
    			System.out.println(input.readUTF());
     
    			DataOutputStream output = new DataOutputStream(client.getOutputStream());
    			output.writeUTF("Message from the server.");
     
    			client.close();
    		}
    	}
    }

    Thanks for help!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ServerAndClient via Ip

    Is there a Firewall blocking the connection?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServerAndClient via Ip

    Quote Originally Posted by Norm View Post
    Is there a Firewall blocking the connection?
    I dont think so. The ports are forwarded in my router and my firewall.
    Is there any port wich is automatically forwarded, so that I could test it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ServerAndClient via Ip

    The only thing I can suggest is double check the firewall.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServerAndClient via Ip

    Quote Originally Posted by Norm View Post
    The only thing I can suggest is double check the firewall.
    In my firewall are 2 rules for the port 4545 (in/out).

    When I try it via ip I use this code:
    public class ClientMain {
    	public static void main(String[] args) throws IOException {
    		Socket client = new Socket("81.198.251.108", 4545);  //This is just an example ip
     
    		DataOutputStream output = new DataOutputStream(client.getOutputStream());
    		output.writeUTF("Message from the client.");
     
    		DataInputStream input = new DataInputStream(client.getInputStream());
    		System.out.println(input.readUTF());
     
    		client.close();
    	}
    }
    Is that correct?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ServerAndClient via Ip

    Is anything printed on your server's console?

    Your last code connects to my HTTP server at 127.0.0.1 but no message is received probably because it expects an HTTP header.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServerAndClient via Ip

    Quote Originally Posted by Norm View Post
    Is anything printed on your server's console?
    Nope, there is just the text from the regular start. ("Waiting for client on port 4545.")

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ServerAndClient via Ip

    If the code works with the localhost address, then I'd suspect the problem is outside of the programs.
    I don't have any experience with configuring routers or firewalls.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServerAndClient via Ip

    Quote Originally Posted by Norm View Post
    If the code works with the localhost address, then I'd suspect the problem is outside of the programs.
    I don't have any experience with configuring routers or firewalls.
    Socket client = new Socket("81.198.251.108", 4545);

    Just putting in the ip in there is correct or?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ServerAndClient via Ip

    Have you tried it with a known public site to see if it works?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ServerAndClient via Ip

    Quote Originally Posted by Norm View Post
    Have you tried it with a known public site to see if it works?
    System.out.println(InetAddress.getByName("www.google.com").getHostAddress());

    The output of this is: "216.58.207.36"



    public class ClientMain {
    	public static void main(String[] args) throws IOException {
    		Socket client = new Socket("216.58.207.36", 80);
    		client.close();
    	}
    }

    And when I put the ip in the connect function and add the normal port for HTTP (80) it gets a connection and closes the connection without any errors, so I think its working and the problem is any kind of hardware, firewall or something like that, the question now is just .. what?

    Even if I put my windows firewall of, its not working.

    Edit: I think I found a problem:
    ServerSocket server = new ServerSocket(4545);
    System.out.println(server.getInetAddress());

    The output is: "0.0.0.0/0.0.0.0"

    Anyone any idea?
    Last edited by RealNiclas; July 26th, 2018 at 04:54 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ServerAndClient via Ip

    No idea. That code works with the localhost. I don't know what the expected println output would be.
    If you don't understand my answer, don't ignore it, ask a question.