Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Detecting connection request

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to build TCP proxy functionality. I am trying to use NIO classes.
Generally this is what I want to do:
1. Source device tries to connect to the proxy port A
2. the proxy tries to connect the destination device on port B
3. if the proxy succeeds establishing a connection, then the proxy accepts the source connection otherwise rejects it.

Well , I tried this:
InetSocketAddress addr1 = new InetSocketAddress(RPORT);
ServerSocketChannel sch1 = ServerSocketChannel.open();
sch1.configureBlocking(false);
sch1.socket().bind(addr1);

Selector selector = Selector.open();
sch1.register(selector, SelectionKey.OP_ACCEPT);
sch2.register(selector, SelectionKey.OP_ACCEPT);
while (selector.select() > 0) {
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
while (i.hasNext()) {
SelectionKey key = (SelectionKey)i.next();
ServerSocketChannel sch = (ServerSocketChannel)key.channel();//<-- connection already accepted

SocketChannel ch = sch.accept();

i.remove();
}
}

The problem is in a fact that sch accepts connection before the code perfoms
sch.accept();

In this moment the source device dumps data to the proxy even the destination connection has not been established.
I wanted accept() to be delayed until it is programatically executed. How do I do this?

Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic