You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Fedechicco <fe...@gmail.com> on 2007/05/07 11:30:30 UTC

UDP bind problem

Hi everybody, it's really nice to write you again.

I'm developing a brand new RoboCup Simulation agent (client), with MINA
obviously, and I've got some tricky problems because I don't know anything
about UDP.
This protocol oblige me to make more or less this anomalous procedure,
everything in UDP:

Client <localIP:localport>  ----->   Server <serverIP:serverPort_1>
Server <serverIP:serverPort_2>  ------>   Client <localIP:localport>
Client <localIP:localport>    ----------->   Server <serverIP:serverPort_2>
etc etc...

the client sends an handshake message to the server:port 1, and the server
uses another port (port2) to talk to the client, sending messages to
localIP:localPort.
I tried to implement that with an Acceptor in bind on local port and _after_
a connector, but mina doesn't like it very much: in any case I'm obliged to
close every connection that uses the local port _before_ i try to bind it
with an acceptor, otherwise I get a bind exception.
So now i'm in this situation:

create Connector
connect
write
close & join close
create acceptor
bind

But the server usually sends me the first message before i succeeded in
binding: therefore I lose the message.

I'm stuck, i'll really appreciate any help. I'm using MINA 1.0.2 with java5,
i'm ok with upgrading or downgrading to any other version if this can help.

Thanks!

Federico Bonelli

Re: UDP bind problem

Posted by Fedechicco <fe...@gmail.com>.
Problem solved, I knew MINA never fail.

In the datagram acceptor there is the method "newSession()" that allow me to
do like this:

new acceptor
acceptor.bind( localAddr)
acceptor.newSession( remoteAddr, localAddr)

so I can initiate the whole process after the binding is completed.

Mina is so coooool!!

thank you all

Fed

Re: UDP bind problem

Posted by Fedechicco <fe...@gmail.com>.
I surely do have the code:

this is the linearized code (without methods):

connector = new DatagramConnector();
acceptor = new DatagramAcceptor();

// Test for free ports
InetSocketAddress localaddr = new InetSocketAddress( startPort );
boolean flag = false;
        while( !flag ){
            flag = true;
            try {
                acceptor.bind( localaddr , handler );
                acceptor.unbind( localaddr );
            } catch (IOException ex) {
                flag = false;
                localaddr = new InetSocketAddress( localaddr.getPort() + 1
);
            }
        }

// connecting & waiting
ConnectFuture fut = connector.connect( remoteaddr , getLocalAddr(), handler
);
fut.join();

// writing client -> server handshake
IoSession outputIoSession = fut.getSession();
outputIoSession.write(buf);

// closing first IoSession
CloseFuture fut = outputIoSession.close();
fut.join();

//binding & check if the address is already free (it should with the
previous join)
boolean flag = false;
        while( !flag ){
            flag = true;
            try {
                acceptor.bind( localaddr , handler );
            } catch (IOException ex) {
                flag = false;
                try {
                    Thread.currentThread().sleep(50);
                } catch (InterruptedException ex2) {
                    ex2.printStackTrace();
                }
            }
        }


Then I tipically wait for the server to initiate the final IoSession with
the second message.

The problem is that the server sends the message during the time I'm not
bind yet and it receives an ICMP unreachable port message.
This is so, i can modify the code to allow connector & acceptor to share a
local port, giving to the connector session only the messages he wants, and
to the acceptor all the others, but please consider that usually my code
sucks. 8)
Which code should I modify?

Thx to everybody.

Fed

Re: UDP bind problem

Posted by Mark Webb <el...@gmail.com>.
Federico,

Do you have any code that you could provide us so that we can better
help you out?


-- 
..Cheers
Mark


On 5/7/07, peter royal <pr...@apache.org> wrote:
> On May 7, 2007, at 11:30 AM, Fedechicco wrote:
> > Client <localIP:localport>  ----->   Server <serverIP:serverPort_1>
> > Server <serverIP:serverPort_2>  ------>   Client <localIP:localport>
> > Client <localIP:localport>    ----------->   Server
> > <serverIP:serverPort_2>
> > etc etc...
>
> > I'm stuck, i'll really appreciate any help. I'm using MINA 1.0.2
> > with java5,
> > i'm ok with upgrading or downgrading to any other version if this
> > can help.
>
> alas, you can't easily do this with MINA right now, not without
> tweaking the UDP Acceptor.
>
> Since UDP is stateless, we might be able to offer a method on the
> DatagramAcceptor that lets you do the Server -> Client communication
> on serverPort_2. This would be a great addition to the UDP support if
> you want to put it together. We'll certainly help ya :)
>
> -pete
>
>
> --
> proyal@apache.org - http://fotap.org/~osi
>
>
>
>
>

Re: UDP bind problem

Posted by peter royal <pr...@apache.org>.
On May 7, 2007, at 11:30 AM, Fedechicco wrote:
> Client <localIP:localport>  ----->   Server <serverIP:serverPort_1>
> Server <serverIP:serverPort_2>  ------>   Client <localIP:localport>
> Client <localIP:localport>    ----------->   Server  
> <serverIP:serverPort_2>
> etc etc...

> I'm stuck, i'll really appreciate any help. I'm using MINA 1.0.2  
> with java5,
> i'm ok with upgrading or downgrading to any other version if this  
> can help.

alas, you can't easily do this with MINA right now, not without  
tweaking the UDP Acceptor.

Since UDP is stateless, we might be able to offer a method on the  
DatagramAcceptor that lets you do the Server -> Client communication  
on serverPort_2. This would be a great addition to the UDP support if  
you want to put it together. We'll certainly help ya :)

-pete


-- 
proyal@apache.org - http://fotap.org/~osi