Jump to content

Recommended Posts

I have two parts of java code one part connects to a server and the other is the server.

 

I start the server part. Ouput in the console in eclipse confirms this.

I then start the client with localhost as the connection host and it works again confirmed by no output in the console. I stop both restart the server and try starting the client with localhost replaced with my global IP and it can't connect.

The server is definatley running as if I goto can you see me dot org and put in the port (90) it says the port is open and connectable and the server tells me that it recieved the connection.

Then I try to connect the client to my HTTP port (80) via the global IP address and it works fine.

 

This has thoroughly confused me.

 

Does anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/187608-java-socket-confusion/
Share on other sites

The Server

package main;
import java.net.*;
import java.io.*;

public class KnockKnockServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        try {
        	serverSocket = new ServerSocket(90);
        	System.out.println(serverSocket);
        } catch (IOException e) {
            System.err.println(e.getMessage()); 
            System.exit(1);
        }    
        try {
            clientSocket = serverSocket.accept();
            System.out.println(clientSocket);
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }    
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;
        KnockKnockProtocol kkp = new KnockKnockProtocol();
        outputLine = kkp.processInput("hi");
        out.println(outputLine);
        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

 

The Client

package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class EchoClient {
    public static void main(String[] args) throws IOException {
    	String host = "88.108.76.198";
    	//host = "192.168.0.2";
        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            echoSocket = new Socket(host, 90);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: "+host+".");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: "+host);
            System.err.println(e.getMessage());
            System.exit(1);
        }
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
    }
}

These are both from the suns tutorial.

Must be a router problem....  The code seems fine, and outside connections can connect to it.

 

 

Some routers/ISPs don't handle very well an IP address looping back to itself through the internet.

 

 

But then, I don't know how port 80 would work....  Hrmmm...  So when you run the server on port 80 it connects, or did you just point the client to Apache or something?

I have apache running on port 80 and so I thought for a laugh I will see if it can connect to it via the outside IP.

 

So changed the port to 80. Hit run. No errors.

 

That is where I got confused because it would appear that neither server OR client are at fault. The router can be ruled out to some degree because the apache port with apache running works. An outside website can connect to the server code.

 

Internal IPs work as well which pushes the blame onto the router but then how does the apache one work.

 

Disabled my firewall, anti virus both together. Nada. Then disabled one and left the other enabled. Nada.

 

Tried changing the ports to no avail :(.

 

Thanks for your time.

 

 

 

 

ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=90]  <= Server socket.

 

Couldn't get I/O for the connection to: xx.xxx.xx.xxx

Connection refused: connect                                              <= The clients I/O error message if that is any help. I googled it but couldn't find any help as everyone else with this said the error was that the server wasn't running and they had proved it by using other means like the website to see :(.

 

To add to the confusion:

 

Server running: Website says it can connect and the server does acknowledge it.

 

Server not running: Website says it can't connect.

 

I love creating the impossible problem. Thanks for the huge amount of time you have given to trying to help me with this.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.