aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes does a nio socket buffer data internally too? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "does a nio socket buffer data internally too?" Watch "does a nio socket buffer data internally too?" New topic
Author

does a nio socket buffer data internally too?

Jonathan Doe
Greenhorn

Joined: Oct 23, 2006
Posts: 4
hy people

i try to write something like a portmapper



the code is mostly from example code
it works as following:
1. an NIOProxy object is created
2. the constructor connects to tcp addresses
3. it sends a connection string with a sessionid to one of the 2 hosts
4. it creates 2 threads both just reading data and forward it to the other socket

the problem i have:
the portmapper above is a workaround for some firewall rules on the remote host
everything works great, but after some bytes of data (i think it may be around 2kb) it get's stuck
the two apps which want to communicate through this port mapper, send some data to each other

what i think the problem is:
my question is, if nio sockets buffer data internally?
cause i think i read and write the data correctly, BUT the data isn't sent if it was too few data to send
so is it possible to flush() the sockets?
maybe the code is not right?
the interesting thing about it is, none of the connections get closed
it seems that both applications (local and remote) are waiting for some data
and i think the data hangs in an internal buffer in the socket api
but that's hard to verify
but maybe i got the threads wrong? can there be a problem in my code?

the output looks like this:
Server Found
Server Found

i got 264bytes something from remote.host/192.168.11.61:23453 to localhost/127.0.0.1:10001
i got 422bytes something from localhost/127.0.0.1:10001 to remote.host/192.168.11.61:23453
i got 1232bytes something from remote.host/192.168.11.61:23453 to localhost/127.0.0.1:10001
i got 486bytes something from localhost/127.0.0.1:10001 to remote.host/192.168.11.61:23453
i got 1264bytes something from remote.host/192.168.11.61:23453 to localhost/127.0.0.1:10001
i got 3624bytes something from remote.host/192.168.11.61:23453 to localhost/127.0.0.1:10001
i got 502bytes something from localhost/127.0.0.1:10001 to remote.host/192.168.11.61:23453
i got 4120bytes something from remote.host/192.168.11.61:23453 to localhost/127.0.0.1:10001

many thanks

[ October 23, 2006: Message edited by: Jonathan Doe ]
[EDIT: altered code and striped output]

[...and I inserted some line breaks into the print statement, so it doesn't force the whole window to be unnecessarily wide - Jim]
[ October 23, 2006: Message edited by: Jim Yingst ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The output from your code doesn't match the print statement you show. Can you provide a version of code and output that go together? You can edit your post using the icon, so we don't see a second copy of all that stuff.

How do you know that something is stuck? Looks like there's something remote that has written about 4 kb to you, and something local that has written about 502 b to the remote machine. Do you know if the data has been received at the other end? Has either end attempted to write more than has been sent? Also you say you think something happens around 2 kb, but I don't see anything special in the output there. What's special about 2 kb?


"I'm not back." - Bill Harding, Twister
Jonathan Doe
Greenhorn

Joined: Oct 23, 2006
Posts: 4
The output from your code doesn't match the print statement you show

changed code to fit your needs

How do you know that something is stuck?

i don't know 100%
that's why i'm asking ;-)
but the application's don't continue with their work, seems that both remote and win app are waiting for something


Looks like there's something remote that has written about 4 kb to you, and something local that has written about 502 b to the remote machine. Do you know if the data has been received at the other end?

i'm sure the data is received by the remote server and my host receives sent data
but this doesn't mean my port mapper receives/sends it what comes from the application on my windows box

Also you say you think something happens around 2 kb, but I don't see anything special in the output there. What's special about 2 kb?

afaik 2kb (i think it was 1500 incl. packet overhead) is standard ethernet MTU on unix systems
that's why i think it could have something to do with 2k (maybe it's the same on win32 box)

maybe my questions weren't clear
Q1: do nio sockets buffer internally?
Q2: is there a buggier bug in my code than missing correct System.out.println's?
i'm not asking for a code correction, but maybe someone is a better java hacker than me and spot's a problem within minutes (something like not flipping ByteBuffer)
would be great
Q3: any tips or hints (maybe there is an java open source port mapper, altough i didn't find one)

thanks sheriff
[ October 23, 2006: Message edited by: Jonathan Doe ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jonathan]: Q1: do nio sockets buffer internally?

That, I don't know. Though even if there's some buffering, I would think that it shouldn't cause the communications to just grind to a halt. Maybe I'm just being naive. But I'm not seeing any flush() methods that you should've called, so I would have to think it's the channel's responsibility to

[Jonathan]: Q2: is there a buggier bug in my code than missing correct System.out.println's?
i'm not asking for a code correction, but maybe someone is a better java hacker than me and spot's a problem within minutes (something like not flipping ByteBuffer)
would be great


Not that I've found. Though it's hard to tell; it's been awhile since I used NIO, and it would be easy for a bug to slip by me in this class.

[Jonathan]: Q3: any tips or hints (maybe there is an java open source port mapper, altough i didn't find one)

One thing I noticed is that it looks like each source/destination pair has its own Thread to handle the transfer. Which means you aren't really getting any benefit from using a Selector here, I think. Those are more useful for managing a large number of connections with a single thread - or maybe with a single "main" thread, and a pool of worker threads that tasks are handed off to. If you're going to have one full thread per connection, I think you might as well use blocking IO and forget the Selector, since blocking IO is probly simpler. That doesn't directly solve our problems, except that a simpler API would be harder for bugs to hide in.

Sorry I don't have a more direct answer for the problem you're having.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: does a nio socket buffer data internally too?