0

The code is a simple program to send stuff from one computer to the other. It works if the client and server are connected to different networks, but won't work when its the same network. (port forwarding is enabled for all the ports in use)

This is for a different program which works kinda like a blockchain. I'm not sure if its a router problem. My guess is that the port forwarding won't work internally between the network clients, which would seem like a router problem. HELP!

Client Side:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception{
        String ipaddress = "70.121.56.92";
        DatagramSocket reciever = new DatagramSocket(3535);
        DatagramPacket pacc = new DatagramPacket(new byte[98],98);
        Scanner s = new Scanner(System.in);
        if (s.nextLine().equals("0")) {
            reciever.receive(pacc);
            System.out.println(Arrays.toString(pacc.getData()));
        }
    }
}

Server Side:

import java.net.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception{
        Scanner kb=new Scanner(System.in);
        DatagramSocket me=new DatagramSocket(3537);
        String msg="";
        while(!msg.equals("stop")){
            System.out.print("msg: ");
            msg=kb.nextLine();
            byte[] bs=new byte[msg.length()];
            for(int i=0; i <msg.length(); ++i){
                bs[i] = (byte) msg.charAt(i);
            }
            DatagramPacket dgp=new DatagramPacket(bs, bs.length, InetAddress.getByName("70.121.**.9*"//this is my public router address), 3535 );
            me.send(dgp);
        }
    }
}

On a different network it shows the string i put in the client on the server console. On the same network, it gets stuck inside the reciever.recieve() method

  • What does "won't work" mean? Have you done any packet analysis? – Joe C Jan 21 '19 at 22:40
  • it just won't recieve anything. packet analysis? im new to networking – Mohit Bhole Jan 21 '19 at 22:46
  • You can use a tool like Wireshark to see what packets are actually sent and received by both machines. – Joe C Jan 21 '19 at 23:00
  • Two hosts on the same LAN do not connect through a router. Frames are delivered from host-to-host on a LAN, but packets are delivered network-to-network through routers. – Ron Maupin Jan 21 '19 at 23:12
  • You should read more about packet transmission over the network (same or different), port mappings, NAT, firewalls, UDP hole punching, etc. You may refer this https://stackoverflow.com/questions/27129268/udp-hole-punching-java-example/27210603#27210603 – Kunjan Thadani Jan 28 '19 at 05:32

1 Answers1

0

The router routes packets received on its WAN connection to computers on the LAN, perhaps using the port-forwarding mechanism. If you send from the LAN to the WAN address, which your server is doing, then it's pretty likely that the router isn't "folding back" that address into the LAN through the port-forwarding mechanism.

You can easily validate this by having the server send to the actual LAN address of the client computer.

I don't think this is a router defect; I think that's just the way it is with NAT.