Jump to content

Java chat program, works on single PC, but not over internet?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts
I got bored so I started writing a chat client in java, its IRC style, a server and as many clients as you want. It works fine on a single computer (using 127.0.0.1 as the IP) but not over the internet.

I've tried running the server and client on my home PC, set up the ports to be forwarded on my router and enabled them in the windows firewall but I get the error "Connection refused" when trying to connect using my external IP (ISP) address, same with me running the server and a mate running the client.

The class chatHandler was basically stolen off the internet, the rest I wrote myself or modified bits of code to suit. I wrote it in netbeans with the GUI builder.

I've hosted both projects in the single zip, if you want to run the application just open the .jar file in the dist folders, or view the source via the src folder, or alternatively you can load the project up in NetBeans so its easier to play with the GUI.

So yeah if anyone can help me out with why its not working that'd be great, I've added the port to my firewall (default 60111). My ultimate goal is to implement an actual protocol so it isn't just sending plain text, then I can easily send files to mates etc.

Attached Files



#2
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Have you tried different computers on your home network? If that works, you know its your router at fault not your programming ;)

You probably also need to setup portfowarding for someone trying to connect to your computer (that you didnt initiate) behind your router, or put it in the routers DMZ.

#3
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts

Stu_328 said:

Have you tried different computers on your home network?

I only have the one PC, my NIC goes straight to my router then to the internet.

#4
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Tyry as I have described above

#5
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts

Stu_328 said:

Tyry as I have described above

Yep both users added both UDP/TCP ports to windows firewall, then added port forwarding for that port on our routers, restarted and tried, no success.

I also thought running both the client and server on my home PC, and then entering my external IP would work, surely the packets would go to my router, it would realise that is me and then send them straight back, essentially exactly the same as when using the loopback just 2 extra steps?

#6
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
can you directly connect without the router? if you can, try doing that and then try connecting to your server.
Posted via CodeCall Mobile

#7
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts
Well the router is an all in one ADSL Modem + 1 port router, so no I cannot. I could try unbridging the connection and see what happens then, but surely it shouldn't be this much trouble.

#8
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
If your on Windows, try telnet'ing into the server (via ip address).
EDIT: I don't know how you expect your program to work when you have comments like this in it:
//wtf do we need this for?
If you don't know what parts of your code does, how do you expect it to work?

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#9
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts

Termana said:

If you don't know what parts of your code does, how do you expect it to work?

I didn't write that code, but I know it works.

#10
Cander

Cander

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Termana said:

If your on Windows, try telnet'ing into the server (via ip address).
EDIT: I don't know how you expect your program to work when you have comments like this in it:

//wtf do we need this for?

If you don't know what parts of your code does, how do you expect it to work?

Good point.

#11
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts
Ok I understand how my code works now what I have done recently is this:

have added java.exe, javaw.exe, the ports Im using to the windows firewall and also the ports I am using to my router.

I have also tried changing :
server = new ServerSocket(port);

to:
InetAddress IP = InetAddress.getByName(IPAddress);
server = new ServerSocket(port, backlog, IP);

(IPAddress = 192.168.1.3 my lan NIC)

As I believe it was binding to my local loopback adaptor, hence not seeing any packets when using a packet analyser, however now I get an exception, after printing a stacktrace from the client I get this.

However I can now connect using 192.168.1.3 just not using my external IP address.

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at chatclient.Client.run(Client.java:60)
at java.lang.Thread.run(Thread.java:619)


Why is my connection getting refused? I've set up a virtual server and port forwarding on my router, and turned off the windows firewall.

Edited by rolls, 31 March 2009 - 03:16 AM.


#12
rolls

rolls

    Newbie

  • Members
  • PipPip
  • 10 posts
Yey I got it working, for some reason the person I was testing it with it didn't work for but it does with everyone else.