0

Here Is The server code

   void bind(String ip,int port)
   {
    socket=ServerSocketChannel.open();
    socket.configureBlocking(false);

    socket.register(acceptChannel=Selector.open(),SelectionKey.OP_ACCEPT);//Since Non Blocking Create Selector
    socket.bind(new InetSocketAddress(ip,port));//Binding To Specified IP,Port So clients can connect

    accept=threadService.scheduleAtFixedRate(this,100,interval,TimeUnit.MILLISECONDS);
   }

    void run()//Just Loops And Checks For New Clients
    {
     try
     {
      if(acceptChannel.selectNow()==0){return;}

      Set<SelectionKey> channels=acceptChannel.selectedKeys();
      Iterator<SelectionKey> iterator=channels.iterator();

      while(iterator.hasNext())
      {
       SelectionKey key=iterator.next();
       ServerSocketChannel server=(ServerSocketChannel)key.channel();

       SocketChannel client=server.accept();
       client.configureBlocking(false);

       //Do Stuff With Client        

       iterator.remove();
      }
      channels.clear();
     }
     catch(Exception ex){errors.newClientError(ex,mainSocket,client);}
   }

And Here Is The Client Code

    SocketChannel clientSocket;
    void connect(String IP,int port)throws IOException
     {
      clientSocket=SocketChannel.open();
      clientSocket.configureBlocking(false);

      clientSocket.register(connectChannel,SelectionKey.OP_CONNECT);//Non Blocking So Loop to check for status
      clientSocket.connect(new InetSocketAddress(IP,port));//Do Actual Connection Here

      waiting=threadService.scheduleAtFixedRate(this,100,10,TimeUnit.MILLISECONDS);
     }

     public void run()//Loops and checks for successful connection
     {
      try
      {
       if(connectChannel.selectNow()==0){return;}

       Set<SelectionKey> channels=connectChannel.selectedKeys();
       Iterator<SelectionKey> iterator=channels.iterator();

       while(iterator.hasNext())
       {
        SelectionKey client=iterator.next();
        SocketChannel channel=(SocketChannel)client.channel();

        if(channel.finishConnect())
        {
         client.cancel();

         iterator.remove();
         channels.clear();

         //Yeah we are connected job done

         return;
        }

        iterator.remove();
       }
       channels.clear();
      }
      catch(Exception ex)
      {

      }
     }

As You Can See Both my client and server must be in non blocking mode for project specific purposes.

Now This Code Works When

1)Both client and server are in same computer and IP parameter of both client & server is "localhost"

2)Both client and server are in same computer and IP parameter of both client & server is my router's network address[Im in windows so I go to cmd type ipconfig and pass the IPV4 address into both these methods]

The problem is that My Client cannot connect to my server when he is on an different system connected over wifi/lan.

I Bind My Server To My Router's IPV4 Address And Give That same address to My Client's connect() method on an different machine but he gets "ConnectionTimedOutException"

The port parameter is same for both client & server which is 8001

Both the systems firewalls are disabled for this test.

Any idea on what's going wrong?

Sync it
  • 1,180
  • 2
  • 11
  • 29
  • Is the IP of "clientSocket.connect(new InetSocketAddress(IP,port));" the IP of your server on the other PC? (use ip config to find out the IP) –  Feb 15 '20 at 14:16
  • Yes. My client uses "My" IPV4 address in his clientsocket.connect() and I use the same IPV4 address in my server sockets bind method. In general both of us use the same IP for connect & bind which is my IPV4 address – Sync it Feb 15 '20 at 15:41
  • Also to note my IPV4 address is 192.168.XXX.XXX and the web tells me that this address isn't routable from external network and they suggested using 'port forwarding' . I found the settings for my router but don't know what to do next – Sync it Feb 15 '20 at 15:43
  • if so then you have to check the config file. How? read this:https://stackoverflow.com/questions/10018955/how-to-access-my-localhost-from-another-pc-in-lan –  Feb 15 '20 at 16:30
  • but I don't have nor do I use an config file anywhere. I just statically typed the ip address on both client & server program – Sync it Feb 15 '20 at 16:58
  • A `Selector` is not a channel, and should not be stored in a variable called `acceptChannel`. – user207421 Feb 16 '20 at 06:56

1 Answers1

0

I had to use Port forwarding to solve my problem

I undertook the following steps

1)Client uses my global[not local ipv4] address which I got from the net[whatsmyip.com] in the connect() method

2)In my server I call bind() using my local ipv4 address[from ipconfig]

3)The common port I used was 8001 which I had to configure for port forwarding in my router settings so it forwards all packets from my global ip:port XXX.XXX.XXX.XXX:8001 to my local ipv4 address:port 192.168.xxx.xxx:8001

This approach works but is not efficient as my IP changes when I disconnect and reconnect. If anyone knows of a better solution to connect to an server without configuring an static IP please do share. Thank u

Sync it
  • 1,180
  • 2
  • 11
  • 29