Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  NIO: Client thinks it is connected, Server hasn't accept()ed yet  (Read 1261 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2006-03-26 11:34:46 »

Hi all,

I'm posting this in Newless Clubies, as I'm obviously making a trival mistake.


I have a non-blocking ServerSocketChannel that binds to local_ip@4455, do NOT register it for OP_ACCEPT. Then I create a SocketChannel, that connects to local_ip@4455, register for OP_READ and OP_WRITE. The client gets OP_WRITE and even manages to send bytes to the server (socketChannel.write() returns >0). The server does not receive any bytes (as it didn't even register for OP_ACCEPT, so doesn't know about the client).

So the client thinks it is connect to the server, yet the server did never accept() any socket.



So, what on earth am I missing?

Thank you!


Program output:
1  
2  
3  
4  
5  
6  
launching server.. done
launching client... done
[NOTI] ClientLogger.clientConnected(Client[192.168.123.150:1532 -> 192.168.123.150:4555])
client writes 4 bytes
client writes 2 bytes
[NOTI] ClientLogger.clientSentPacket(Client[192.168.123.150:1532 -> 192.168.123.150:4555])

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-03-26 11:37:23 »

Self-contained source-code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
System.out.println("local_ip = " + InetAddress.getLocalHost());

         // server
         final Selector serverSelector = Selector.open();
         ServerSocketChannel ssc = ServerSocketChannel.open();
         ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 4555));
         ssc.configureBlocking(false);
         ssc.register(serverSelector, SelectionKey.OP_ACCEPT);

         // client
         final Selector clientSelector = Selector.open();
         SocketChannel sc = SocketChannel.open();
         sc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
         sc.socket().connect(new InetSocketAddress(InetAddress.getLocalHost(), 4555));
         sc.configureBlocking(false);
         sc.register(clientSelector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);

         Runnable serverLoop = new Runnable()
         {
            public void run()
            {
               try
               {
                  while (true)
                  {
                     serverSelector.select();
                     Iterator<SelectionKey> selectedKeys = serverSelector.selectedKeys().iterator();

                     while (selectedKeys.hasNext())
                     {
                        SelectionKey key = selectedKeys.next();
                        selectedKeys.remove();

                        if (key.isAcceptable())
                           System.out.println("server: socket available, but not accepting it!");
                     }

                     Thread.sleep(1000);
                  }
               }
               catch (Exception exc)
               {
                  exc.printStackTrace();
               }
            }
         };

         Runnable clientLoop = new Runnable()
         {
            public void run()
            {
               try
               {
                  while (true)
                  {
                     clientSelector.select();
                     Iterator<SelectionKey> selectedKeys = clientSelector.selectedKeys().iterator();

                     while (selectedKeys.hasNext())
                     {
                        SelectionKey key = selectedKeys.next();
                        selectedKeys.remove();

                        if (key.isReadable())
                           System.out.println("client: can read bytes");

                        if (key.isWritable())
                        {
                           System.out.println("client: can write bytes");
                           ByteBuffer bb = ByteBuffer.wrap(new byte[64]);
                           System.out.println("  client writes " + ((SocketChannel) key.channel()).write(bb) + " bytes");
                        }
                     }

                     Thread.sleep(1000);
                  }
               }
               catch (Exception exc)
               {
                  exc.printStackTrace();
               }
            }
         };

         new Thread(serverLoop).start();

         try
         {
            Thread.sleep(1000);
         }
         catch (Exception exc)
         {
            //
         }

         new Thread(clientLoop).start();



Output:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
local_ip = riven/192.168.123.150

server: socket available, but not accepting it!
client: can write bytes
  client writes 64 bytes

server: socket available, but not accepting it!
client: can write bytes
  client writes 64 bytes

etc etc etc

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2006-03-26 11:52:01 »

Java 1.5.0_06 (Sun, client-vm)
Windows XP

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2006-03-26 19:36:13 »

Could anybody at least run this code and post the results?

Thank you very much!

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

Senior Duke





« Reply #4 - Posted 2006-03-26 22:55:21 »

at first glance: what do you mean it dosen't register for accept? I see
1  
ssc.register(serverSelector, SelectionKey.OP_ACCEPT);

in 10th line from your code
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2006-03-26 23:31:04 »

Yes, I posted the 2nd version of my test-case. Whether or not i register makes no diff, same output. Undecided

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

Senior Duke





« Reply #6 - Posted 2006-03-27 22:20:14 »

you helped me out so I'll at least run the code shortly... been busy lately
Offline Kova

Senior Duke





« Reply #7 - Posted 2006-03-27 22:39:39 »

Really strange... when you bind server to the port it can automaticly receive data. Maybe that's normal behaviour? Server dosen't even need to start, just binds the port and it can receive data. I commented his thread .... works. Commented bind method.... dosen't work. I guess when something binds port it makes it his and can receive everything.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2006-03-27 23:49:09 »

Thank you for running my code,

appearantly, this is a rarely encountered (yet serious) bug in NIO.

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

Senior Duke





« Reply #9 - Posted 2006-03-28 09:48:16 »

Do you have link to bug report?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2006-03-28 11:33:34 »

I already submitted the bug, will have to wait about three weeks before they respond, according to the website... Wink

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

Senior Duke





« Reply #11 - Posted 2006-03-28 13:50:38 »

yeah... I meant link to description of bug, not bug reporting system itself. Of course you'll fill in bug report as it's your discovery Smiley
Post it when it gets there... I'm courius about this.
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 (38 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (78 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!