Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  !!! localhost all OK, but in reality packets never arrive !!! HELP !!!  (Read 2663 times)
0 Members and 1 Guest are viewing this topic.
Offline adamadas

Senior Newbie




*** Visualise The Imagination ***


« Posted 2006-05-29 17:54:23 »

I have a serious problem with the application i work. I implement a multiplayer mode for a game using NIO. Wher i run the application all look fine but when i install the server in another computer the packets never arrive through network.

How can i fix this problem???

Here is the code:

SERVER

import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class NIOServer {

  public final static int DEFAULT_PORT = 3120;
  public final static int MAX_PACKET_SIZE = 65507;

  public static void main(String[] args) {

    int port = DEFAULT_PORT;
       
    try {
      DatagramChannel channel = DatagramChannel.open();
      DatagramSocket socket = channel.socket();
      SocketAddress address = new InetSocketAddress(port);
      socket.bind(address);
      ByteBuffer buffer = ByteBuffer.allocateDirect(MAX_PACKET_SIZE);
      while (true) {
        SocketAddress client = channel.receive(buffer);
        //data s=buffer.getBytes();
        //System.out.print(buffer);
        buffer.flip();
        channel.send(buffer, client);
        buffer.clear();
      } // end while
    }  // end try
    catch (IOException ex) {
      System.err.println(ex);
    }  // end catch

  }  // end main

}


CLIENT

import java.net.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;

public class NIOClient{
   public  final static int DEFAULT_PORT = 3120;
     private final static int LIMIT = 1000;
    int port = DEFAULT_PORT;
       
    SocketAddress remote;
    DatagramChannel channel;
    Selector selector;
    ByteBuffer buffer;
   public NIOClient(){
      try{
         remote = new InetSocketAddress("localhost", port);
         channel = DatagramChannel.open();
         channel.configureBlocking(false);
            channel.connect(remote);
            selector = Selector.open();
            channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
            buffer = ByteBuffer.allocate(4);
      }
      catch (IOException ex) {
            System.err.println(ex);
       }
      
   }

     public int channel(int k){
        int r=0;
        try {
           
            int n = 0;
            int numbersRead = 0;
               // wait one minute for a connection
              selector.select(6000);
              Set readyKeys = selector.selectedKeys();
              Iterator iterator = readyKeys.iterator();
                   if (iterator.hasNext()) {
                     SelectionKey key = (SelectionKey) iterator.next();
                     iterator.remove();
                     if (key.isReadable()) {
                          buffer.clear();
                          channel.read(buffer);
                          buffer.flip();
                          int echo = buffer.getInt();
                          System.out.println("Read: " + echo);
                          numbersRead++;
                          r=echo;
                     }
                     if (key.isWritable()) {
                          buffer.clear();
                          buffer.putInt(k);
                          buffer.flip();
                          channel.write(buffer);
                          System.out.println("Wrote: " + k);
                          n++;
                         if (n == LIMIT) {
                            // All packets have been written; switch to read-only mode
                            key.interestOps(SelectionKey.OP_READ);
                          }
                     } 
                   }
               
      }  // end try
       catch (IOException ex) {
            System.err.println(ex);
       }
       return r;
     }
}
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2006-05-29 18:09:46 »

1  
remote = new InetSocketAddress("localhost", port);

You are still trying to connect to the locla computer, not the remote one.  You need to replace localhost with the ip address of the computer running the server.

Offline adamadas

Senior Newbie




*** Visualise The Imagination ***


« Reply #2 - Posted 2006-05-29 18:42:19 »

i know my friend...of course...this is when i have the problem !!!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2006-05-29 23:49:30 »

Then show the code you are using, not the code you were using.  We are not mind readers and know that you made the correct changes.  People have made simple mistakes before.

Offline adamadas

Senior Newbie




*** Visualise The Imagination ***


« Reply #4 - Posted 2006-05-30 11:41:21 »

Friend, you bring me problems...If you do not understand let the others understand. I do not have something to change, if you want to see the problem just put an IP address. p.x. 167.45.0.7. Replace the word "localhost". It is simple and you confuse the others in what i ask!

Please, check the problem. I have turned off the firewalls, i have checked the packet size and till the packets never arrive to the server when the systems runs on internet. I repeat that localhost and in a lan network all look fine.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2006-05-30 12:09:37 »

Yeah, ever heard of "never bite the hand that feeds you" ?

You seem to have quite an attitude for somebody who has a trival error in its sourcecode which somebody else actually takes the times to read and spot the error.


Then you say it doesn't work when dealing with more than 1 computer (first post) while in your last post you say it "I repeat that localhost and in a lan network all look fine." - you never said LAN was working.

So to get back to the original problem, and hoping you also don't burn my advise to the ground: you need to setup proper port-forwarding or setup a NAT server in your router for WAN->LAN bound traffic.


Last: seriously, drop that attitude.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline adamadas

Senior Newbie




*** Visualise The Imagination ***


« Reply #6 - Posted 2006-05-30 13:56:56 »

Dear Riven,

The information i added were not available to me at the time of my first post. Athough you really misunderstood me, i apologise for my "attitude", i just wanted to be specific and our discussion not to take irrelevant extensions. Sory again Riven and CaptainJester.

As far as the information you gave me is concerned, i have already done port forwarding and nothing changed. Both from the sides of client and server. Have you checked the code Riven?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2006-05-30 14:21:17 »

As you never seem to call

   public int channel(int k)

anywhere, it doesn't make any sense it even works on localhost/LAN.


And if this is yet another-way-too-easy-to-spot-bug-that-has-long-been-fixed... you might want to give more recent code.



Last but not least, you say you wait for 6 minutes in your code, but actually wait 6000ms = 6 seconds. By the time you launch your client the 6 seconds might already have passed.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline adamadas

Senior Newbie




*** Visualise The Imagination ***


« Reply #8 - Posted 2006-05-30 14:27:26 »

Riven it is just a class. It does not have a main. I have another class that calls it. Anyway...

As for the waiting time i fixed it because one minute was a lot of time and i forgot to erase the comment.

The problem is not there i think... I really do not know...

Thanks for your response
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2006-05-30 14:29:46 »

If we don't get to see all relevant code, how could we help you?

Goodluck with it...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!