[java] Java socket trubble

Started by
1 comment, last by Furie 18 years, 7 months ago
Im having problem with this java socket. I have written fairly simple program that simply waits for an incomming connection. when It gets an accept it creates a outputstream and sends a small message. this program works while im connecting to the local port (ither 127.0 or 192.168 adress, im currently behind a firewall but the port im connectiong to is forwared to this computer) and I get the message sent back to me. It works also if im on a computer outside of this network and connect. what doesnt work is when I try to connect to the program through the external port from inside the network. when I try I get this message: "Connection accepted from <internal ip:192.168>" "Connection reset by peer: socket write error" The connection seems to get accepted.. but then just doesnt work. Its also kind of wierd that instead of getting an external ip, I get the internal ip. Have anyone else had this problem? do anyone know what could be done about it? If you could help i would really apriciate it ^_^; here is the simple code. import java.io.*; try{ ServerSocket serversocket = null; Socket socket = null; serversocket = new ServerSocket(5333); socket = serversocket.accept(); System.out.println("Server:Connection accepted from: " + socket.getInetAddress()); OutputStream ut = socket.getOutputStream(); String hej = "Message!"; ut.write(hej.getBytes()); ut.flush(); }catch(Exception e){ System.out.println("Error?:" + e.getMessage()); }
Advertisement
Sounds like a combination of you having no idea what your firewall is setup to do, and you not choosing which interface to bind to.

Look in the java 1.4 / 1.5 javadocs for java.net and find NetworkInterface class.

Use that class to get hold of the network interface you want to listen on, and bind your serversockets to it. No modern computer has only one network interface, so you really ought to be choosing which to bind to, or binding to them all.

As for your firewall? I'm not going to help with that because it's potentially a massive process and you really should learn for yourself I'm afraid. Find out what firewalls are running on your DSL router, *and* on each of the machines in your LAN, and learn how to configure them.

Finally, if you have problems, download ethereal, do packet-sniffing, and look at the packets going around your LAN - it may become obvious what's going wrong just from looking at the ICMP packets.
Found out what the problem was. I doesnt really have anything to do with java. but rather that the firwal didnt have 2 way NAT.

Since where this program is going to be used there whont likely be a 2 way NAT there ither. So im going to solve the problem by Checking if its the same network. and if it is ill do a Multicast via UDP to get the ip off that server.

(If its the same network but a diffrent sub-net it will still check out as the same network, but the UDP multicast whont return anything. In wish case the connection problem whont ocure. so ill just connect to the external adress)

This topic is closed to new replies.

Advertisement