You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Yigal Rachman <yi...@uvic.ca> on 2007/02/13 23:49:08 UTC

How do I limit a MINA server to accept just one connection?

Hi, Folks:

I have just started using MINA, and already love it!

My first application is for an instrument simulator that must accept 
just one connection at a time.  I have hacked it by immediately closing 
a new session if one is already open.  This works ok, but the client 
gets a "Connection closed by foreign host" message instead of the 
"connection refused" message I would prefer to see.  I would be grateful 
for any suggestions on how to achieve this.

Thank you,
Yigal Rachman
DMAS Data Acquisition Developer
NEPTUNE Canada
University of Victoria,
Victoria, BC, Canada
www.neptunecanada.ca

Re: How do I limit a MINA server to accept just one connection? - solution looks promising...

Posted by Yigal Rachman <yi...@uvic.ca>.
Wow - what a fast and informative bunch of responses! 

I have built a quick prototype incorporating all your suggestions and it 
seems to work the way I want it to.

Thank you all,
Yigal


Re: How do I limit a MINA server to accept just one connection?

Posted by Maarten Bosteels <mb...@gmail.com>.
Hello Nicola

On 5/3/07, nicola buso <ni...@eng.it> wrote:
>
>
> Hi Maarten,
>
> are you using this solution to limit connection? I tried it, but I can not
> manage situation where the client close unexpectedly che connection.
> The socket is in TIME_WAIT status and mina obtain an exception
> "java.net.BindException: Address already in use" when try to bind.



I am not using this solution at all, but I just tried it out and it seems to
work.
Remember to add these lines to your boostrap code:

    SocketAcceptor acceptor = new SocketAcceptor();
    SocketSessionConfig config =(SocketSessionConfig)
acceptor.getSessionConfig();
    config.setReuseAddress(true);
    acceptor.setReuseAddress(true);

Hope that helps,
Maarten

nicola.
>
>
> Maarten Bosteels-4 wrote:
> >
> > On 2/14/07, Stefano Bagnara <ap...@bago.org> wrote:
> >>
> >> I'm not a MINA expert, but I guess you also have to add:
> >> socketAcceptorConfig.setBacklog(1);
> >> otherwise if multiple connections are made before you are able to
> really
> >> accept the socket and unbind you will accept multiple connections.
> >
> >
> > I agree. Thanks for pointing that out.
> >
> >
> > Stefano
> >>
> >> Maarten Bosteels ha scritto:
> >> > If you want just one connection in total, you could do
> >> >
> >> > public void sessionCreated(IoSession session) throws Exception {
> >> >  acceptor.setDisconnectOnUnbind(false);
> >> >  acceptor.unbind();
> >> > }
> >> >
> >> > public void sessionClosed(IoSession session) throws Exception {
> >> >  acceptor.bind();
> >> > }
> >> >
> >> > Then your server would only be listening when there is no connection
> >> > established.
> >> > (you probably need a heartbeat mechanism to ensure that the one and
> >> only
> >> > connection is still alive)
> >> >
> >> > Maarten
> >>
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/How-do-I-limit-a-MINA-server-to-accept-just-one-connection--tf3224401.html#a10302356
> Sent from the mina dev mailing list archive at Nabble.com.
>
>

Re: How do I limit a MINA server to accept just one connection?

Posted by nicola buso <ni...@eng.it>.
Hi Maarten,

are you using this solution to limit connection? I tried it, but I can not
manage situation where the client close unexpectedly che connection.
The socket is in TIME_WAIT status and mina obtain an exception
"java.net.BindException: Address already in use" when try to bind.

nicola.


Maarten Bosteels-4 wrote:
> 
> On 2/14/07, Stefano Bagnara <ap...@bago.org> wrote:
>>
>> I'm not a MINA expert, but I guess you also have to add:
>> socketAcceptorConfig.setBacklog(1);
>> otherwise if multiple connections are made before you are able to really
>> accept the socket and unbind you will accept multiple connections.
> 
> 
> I agree. Thanks for pointing that out.
> 
> 
> Stefano
>>
>> Maarten Bosteels ha scritto:
>> > If you want just one connection in total, you could do
>> >
>> > public void sessionCreated(IoSession session) throws Exception {
>> >  acceptor.setDisconnectOnUnbind(false);
>> >  acceptor.unbind();
>> > }
>> >
>> > public void sessionClosed(IoSession session) throws Exception {
>> >  acceptor.bind();
>> > }
>> >
>> > Then your server would only be listening when there is no connection
>> > established.
>> > (you probably need a heartbeat mechanism to ensure that the one and
>> only
>> > connection is still alive)
>> >
>> > Maarten
>>
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/How-do-I-limit-a-MINA-server-to-accept-just-one-connection--tf3224401.html#a10302356
Sent from the mina dev mailing list archive at Nabble.com.


Re: How do I limit a MINA server to accept just one connection?

Posted by Maarten Bosteels <mb...@gmail.com>.
On 2/14/07, Stefano Bagnara <ap...@bago.org> wrote:
>
> I'm not a MINA expert, but I guess you also have to add:
> socketAcceptorConfig.setBacklog(1);
> otherwise if multiple connections are made before you are able to really
> accept the socket and unbind you will accept multiple connections.


I agree. Thanks for pointing that out.


Stefano
>
> Maarten Bosteels ha scritto:
> > If you want just one connection in total, you could do
> >
> > public void sessionCreated(IoSession session) throws Exception {
> >  acceptor.setDisconnectOnUnbind(false);
> >  acceptor.unbind();
> > }
> >
> > public void sessionClosed(IoSession session) throws Exception {
> >  acceptor.bind();
> > }
> >
> > Then your server would only be listening when there is no connection
> > established.
> > (you probably need a heartbeat mechanism to ensure that the one and only
> > connection is still alive)
> >
> > Maarten
>
>
>

Re: How do I limit a MINA server to accept just one connection?

Posted by Stefano Bagnara <ap...@bago.org>.
I'm not a MINA expert, but I guess you also have to add:
socketAcceptorConfig.setBacklog(1);
otherwise if multiple connections are made before you are able to really 
accept the socket and unbind you will accept multiple connections.

Stefano

Maarten Bosteels ha scritto:
> If you want just one connection in total, you could do
> 
> public void sessionCreated(IoSession session) throws Exception {
>  acceptor.setDisconnectOnUnbind(false);
>  acceptor.unbind();
> }
> 
> public void sessionClosed(IoSession session) throws Exception {
>  acceptor.bind();
> }
> 
> Then your server would only be listening when there is no connection
> established.
> (you probably need a heartbeat mechanism to ensure that the one and only
> connection is still alive)
> 
> Maarten



Re: How do I limit a MINA server to accept just one connection?

Posted by Maarten Bosteels <mb...@gmail.com>.
If you want just one connection in total, you could do

public void sessionCreated(IoSession session) throws Exception {
  acceptor.setDisconnectOnUnbind(false);
  acceptor.unbind();
}

public void sessionClosed(IoSession session) throws Exception {
  acceptor.bind();
}

Then your server would only be listening when there is no connection
established.
(you probably need a heartbeat mechanism to ensure that the one and only
connection is still alive)

Maarten

On 2/14/07, James Ladd <ja...@hotmail.com> wrote:
>
> Would it be possible to limit the connections in the accept code by
> counting
> the
> accepted connections and then just not accepting anymore until the count
> went
> down? The count would go down when a connection closed.
>
> This way, the client would get a connection refused.
>
> I haven't used MINA long enough to tell you if this is possible but
> logically from an
> NIO perspective it is.
>
> Rgs, James.
>
>
>
>
> >From: Trustin Lee <tr...@gmail.com>
> >Reply-To: dev@mina.apache.org
> >To: dev@mina.apache.org
> >Subject: Re: How do I limit a MINA server to accept just one connection?
> >Date: Wed, 14 Feb 2007 10:39:35 +0900
> >
> >Hi Yigal,
> >
> >2007-02-13 (화), 14:49 -0800, Yigal Rachman 쓰시길:
> > > Hi, Folks:
> > >
> > > I have just started using MINA, and already love it!
> >
> >Great!  Please let us know if you have any idea on improving MINA.
> >
> > > My first application is for an instrument simulator that must accept
> > > just one connection at a time.  I have hacked it by immediately
> closing
> > > a new session if one is already open.  This works ok, but the client
> > > gets a "Connection closed by foreign host" message instead of the
> > > "connection refused" message I would prefer to see.  I would be
> grateful
> > > for any suggestions on how to achieve this.
> >
> >You did the right thing actually.  :)  There's no other way to limiting
> >the total number of connection AFAIK.
> >
> >HTH,
> >Trustin
> >--
> >what we call human nature is actually human habit
> >--
> >http://gleamynode.net/
> >--
> >PGP Key ID: 0x0255ECA6
>
>
> ><< signature.asc >>
>
> _________________________________________________________________
> Advertisement: Fresh jobs daily. Stop waiting for the newspaper. Search
> Now!
> www.seek.com.au
>
> http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Eseek%2Ecom%2Eau&_t=757263760&_r=Hotmail_EndText_Dec06&_m=EXT
>
>

Re: How do I limit a MINA server to accept just one connection?

Posted by James Ladd <ja...@hotmail.com>.
Would it be possible to limit the connections in the accept code by counting 
the
accepted connections and then just not accepting anymore until the count 
went
down? The count would go down when a connection closed.

This way, the client would get a connection refused.

I haven't used MINA long enough to tell you if this is possible but 
logically from an
NIO perspective it is.

Rgs, James.




>From: Trustin Lee <tr...@gmail.com>
>Reply-To: dev@mina.apache.org
>To: dev@mina.apache.org
>Subject: Re: How do I limit a MINA server to accept just one connection?
>Date: Wed, 14 Feb 2007 10:39:35 +0900
>
>Hi Yigal,
>
>2007-02-13 (화), 14:49 -0800, Yigal Rachman 쓰시길:
> > Hi, Folks:
> >
> > I have just started using MINA, and already love it!
>
>Great!  Please let us know if you have any idea on improving MINA.
>
> > My first application is for an instrument simulator that must accept
> > just one connection at a time.  I have hacked it by immediately closing
> > a new session if one is already open.  This works ok, but the client
> > gets a "Connection closed by foreign host" message instead of the
> > "connection refused" message I would prefer to see.  I would be grateful
> > for any suggestions on how to achieve this.
>
>You did the right thing actually.  :)  There's no other way to limiting
>the total number of connection AFAIK.
>
>HTH,
>Trustin
>--
>what we call human nature is actually human habit
>--
>http://gleamynode.net/
>--
>PGP Key ID: 0x0255ECA6


><< signature.asc >>

_________________________________________________________________
Advertisement: Fresh jobs daily. Stop waiting for the newspaper. Search Now! 
www.seek.com.au 
http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Eseek%2Ecom%2Eau&_t=757263760&_r=Hotmail_EndText_Dec06&_m=EXT


Re: How do I limit a MINA server to accept just one connection?

Posted by Trustin Lee <tr...@gmail.com>.
Hi Yigal,

2007-02-13 (화), 14:49 -0800, Yigal Rachman 쓰시길:
> Hi, Folks:
> 
> I have just started using MINA, and already love it!

Great!  Please let us know if you have any idea on improving MINA.

> My first application is for an instrument simulator that must accept 
> just one connection at a time.  I have hacked it by immediately closing 
> a new session if one is already open.  This works ok, but the client 
> gets a "Connection closed by foreign host" message instead of the 
> "connection refused" message I would prefer to see.  I would be grateful 
> for any suggestions on how to achieve this.

You did the right thing actually.  :)  There's no other way to limiting
the total number of connection AFAIK.

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6