burtybob Posted January 7, 2010 Share Posted January 7, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/ Share on other sites More sharing options...
corbin Posted January 7, 2010 Share Posted January 7, 2010 Hrmmm.... Very strange problem. Can we see some code? Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-990619 Share on other sites More sharing options...
burtybob Posted January 7, 2010 Author Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-990624 Share on other sites More sharing options...
burtybob Posted January 7, 2010 Author Share Posted January 7, 2010 Also telnet can connect via localhost 90 but not my global IP 90. Could it be something to do with the router? Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-990633 Share on other sites More sharing options...
corbin Posted January 8, 2010 Share Posted January 8, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-990730 Share on other sites More sharing options...
burtybob Posted January 8, 2010 Author Share Posted January 8, 2010 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 . Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-990832 Share on other sites More sharing options...
corbin Posted January 8, 2010 Share Posted January 8, 2010 Hrmmmm, try stopping Apache for a minute, switching the server to listen on port 80, then have the client connect to it. If that works, then either your router or ISP is blocking port 90. If that still doesn't work, then it's a Java problem. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991302 Share on other sites More sharing options...
burtybob Posted January 8, 2010 Author Share Posted January 8, 2010 Did that and it then connects to the routers web interface which also runs on port 80. surely if port 90 was blocked then then website wouldn't be able to connect to the server program I had written? Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991310 Share on other sites More sharing options...
corbin Posted January 8, 2010 Share Posted January 8, 2010 Oh, yeah, I forgot that the website was saying it could connect.... Does the website say it can't connect if you take the server down? Maybe it's reporting a false positive. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991367 Share on other sites More sharing options...
burtybob Posted January 8, 2010 Author Share Posted January 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991371 Share on other sites More sharing options...
corbin Posted January 9, 2010 Share Posted January 9, 2010 Did you try running your Java server on 80? Maybe for some weird reason your ISP only blocks looped back WAN requests on certain ports or something. Besides some weird situation like that, I don't know what this could be lol. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991575 Share on other sites More sharing options...
burtybob Posted January 9, 2010 Author Share Posted January 9, 2010 Yeah I did but then it connected to the routers web interface. I shall do a test with a couple of friends later when they are online and see if I can get the client to connect to the server on there machine and then I will post back the results Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991628 Share on other sites More sharing options...
burtybob Posted January 9, 2010 Author Share Posted January 9, 2010 Ok so my mate telnets to the server fine on port 90. grrr I can't do external testing but at least I know it works *yay* Thank you corbin for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991755 Share on other sites More sharing options...
corbin Posted January 9, 2010 Share Posted January 9, 2010 Hrmm, still not resolved, but I guess whatever works . lol. Quote Link to comment https://forums.phpfreaks.com/topic/187608-java-socket-confusion/#findComment-991831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.