0

I'm trying to send a message across two computers using Java.net and DataInput/OutputStream but whenever I enter a IP other than localhost it doesn't send. localhost works just fine but when I enter my IP it can't connect. (I am using my external IP and I tested it on the same and different PC's and I'm not using a VPN.)

I would like to know how to get it where I can connect to an IP from a different computer.

EDIT: The IPv6 works but not IPv4. But it's the long one that doesn't show in the findmyip sites & It only works when both programs are on the same system

This is the basic listen part for the message

s = new Socket("localhost", 1201); //heres where the ip goes
            dis = new DataInputStream(s.getInputStream());
            dos = new DataOutputStream(s.getOutputStream());
            String msgin="";
            
            while(!msgin.equals("exit")) {
                msgin = din.readUTF();
                msg_area.setText(msg_area.getText().trim()+"\n Server:\t"+msgin);
            }
            
            } catch(Exception e) {
                e.printStackTrace();
            }
        

This is the write message part, I get the error at dout.writeUTF but that's only when I use a real IP

try {
                    String msgout = "";
                    msgout = msg_text.getText().trim();
                    System.out.println(msgout);
                    dout.writeUTF(msgout);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

Heres the error log for the client side:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at chats.chat_client$2.actionPerformed(chat_client.java:91)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6631)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6396)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5007)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4839)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Heres the error log for the server side:

java.net.ConnectException: Connection timed out: connect
    at java.base/java.net.PlainSocketImpl.connect0(Native Method)
    at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:101)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at java.base/java.net.Socket.connect(Socket.java:558)
    at java.base/java.net.Socket.<init>(Socket.java:454)
    at java.base/java.net.Socket.<init>(Socket.java:231)
    at chats.chat_client.main(chat_client.java:46)
Tyler Moen
  • 81
  • 5
  • 1
    If both computers are on the same LAN, you need to use IP addresses on that LAN. I assume by your mention of "external IP" that you're not doing that. And what firewalling do you have in place on either of these computers? Can you ping from one to the other? – J.Backus Sep 18 '20 at 23:14
  • @J.Backus I've tried it on the same network as well as other networks on the same and different devices. I don't think I have anything for a firewall other than the security on our computer, McAfee (ik it's bad.) I've been using the IPv4 which I'm guessing is the local/external one – Tyler Moen Sep 18 '20 at 23:24
  • @JanezKuhar I used `ss = new ServerSocket(1201); s = ss.accept();` with them both delecared static. I use the statement in my first code snip-it, replacing the first line with the code I just presented you – Tyler Moen Sep 18 '20 at 23:55
  • 1
    If you want to send messages over the internet (and not just between 2 computers connected via LAN), you should look into [port forwarding](https://en.m.wikipedia.org/wiki/Port_forwarding) – Janez Kuhar Sep 19 '20 at 00:00
  • If you seek further assistance, you should provide us with a [mcve](https://stackoverflow.com/help/minimal-reproducible-example) - a simplified version of your code that others can run. – Janez Kuhar Sep 19 '20 at 00:03
  • The behavior you're getting suggests that your server socket is only listening on the loopback interface. But I just tried it, and `ServerSocket` seems to listen on all interfaces the way you've created it. Just for yucks, you could try explicitly telling it to listen on `0.0.0.0` just to be sure. – CryptoFool Sep 19 '20 at 00:06
  • 1. `ServerSocket` doesn't connect, it accepts: amd there is no `ServerSocket` in this code. 2. If you got the error on `writeUTF()` you *did* connect. Otherwise you couldn't have got this far. 3. A `NullPointerException` has nothing to do with `ServerSocket` or `Socket` either. 4. You have both `dout` and `dos`. Which is it? 5. Your 'error log' is from the client side, not the server side. Duplicate. – user207421 Sep 19 '20 at 01:10

0 Answers0