You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Namaste Reid <na...@opsysinc.com> on 2011/08/15 22:57:30 UTC

two way communication with TCP server using Netty

I have a program running on a remote host to which I need to connect,
handshake, and then listen for messages. I have setup the following camel
route:

    <route>
       <from
uri="netty:tcp://localhost:50001?decoders=#decoders&amp;sync=false" />
       <bean ref="TransformMessage" method="inboundDecoder" />
       <to uri="eventadmin:messages/aacus/inbound" />
    </route>

    <route>
       <from uri="eventadmin:messages/aacus/outbound" />
       <bean ref="TransformMessage" method="outboundEncoder" />
       <to
uri="netty:tcp://192.168.0.111:50001?allowDefaultCodec=false&amp;sync=false"
/>
    </route>

My question is how do I make this work? If I establish the route using

`<from
uri="netty:tcp://192.168.0.111:50001?decoders=#decoders&amp;sync=false" />`

it fails with a binding error.

How can I setup the connection to respond on a specific port without
modifying the server?

--
View this message in context: http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4702360.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: two way communication with TCP server using Netty

Posted by Tereza Nedelescu <tn...@tagged.com>.
I have a similar question. See
http://camel.465427.n5.nabble.com/Routing-socket-input-to-file-output-doesn-t-work-td4702177.html

--
View this message in context: http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4702437.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: two way communication with TCP server using Netty

Posted by Namaste Reid <na...@opsysinc.com>.
I do not believe the port is already taken as I am able to connect and
communicate at this address and on this port using java.net.Socket. I have
worked around this issue by implement a custom Camel component that uses
java.net.Socket and bypasses Netty completely. I believe this is a
limitation of camel-netty, documented here:
https://issues.apache.org/jira/browse/CAMEL-1077

Namaste

On Mon, Aug 22, 2011 at 6:57 AM, Ashwin Karpe [via Camel] <
ml-node+4723317-1672245996-242140@n5.nabble.com> wrote:

> Hi,
>
> Based on the error, it seems that the port in question is already taken by
> the another process and hence you cannot seem to set up a netty tcp consumer
> against the given port.
>
> Can you try another port.
>
> Cheers,
>
> Ashwin...
> ---------------------------------------------------------
> Ashwin Karpe
> Apache Camel Committer & Sr Principal Consultant
> FUSESource (a Progress Software Corporation subsidiary)
> http://fusesource.com
>
> Blog:http://opensourceknowledge.blogspot.com
> ---------------------------------------------------------
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4723317.html
>  To unsubscribe from two way communication with TCP server using Netty, click
> here<http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4702360&code=bmFtYXN0ZS5yZWlkQG9wc3lzaW5jLmNvbXw0NzAyMzYwfDMzMjY2NTQzMA==>.
>
>


--
View this message in context: http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4723395.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: two way communication with TCP server using Netty

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

Based on the error, it seems that the port in question is already taken by
the another process and hence you cannot seem to set up a netty tcp consumer
against the given port.

Can you try another port.

Cheers,

Ashwin...

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4723317.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: two way communication with TCP server using Netty

Posted by Namaste Reid <na...@opsysinc.com>.
Hi Ashwin, thank you for your response.

Can you please post the detailed error message.


Here is the BindException I receive for the following route:

<camel:camelContext id="snapCamelContext" xmlns="
http://camel.apache.org/schema/spring">

<route>
<from uri="netty:tcp://192.168.0.111:50001?decoders=#decoders&amp;sync=false"
/>
 <bean ref="TransformMessage" method="inboundDecoder" />
<to uri="eventadmin:messages/aacus/inbound" />
 </route>

<route>
<from uri="eventadmin:messages/aacus/outbound" />
 <bean ref="TransformMessage" method="outboundEncoder" />
<to uri="netty:tcp://
192.168.0.111:50001?allowDefaultCodec=false&amp;sync=false" />
 </route>

</camel:camelContext>

---------------------------------------------

Exception in thread "SpringOsgiExtenderThread-10"
org.apache.camel.RuntimeCamelException:
org.jboss.netty.channel.ChannelException: Failed to bind to: /
192.168.0.111:50001
        at
org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1139)
        at
org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:103)
        at
org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:237)
        at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
        at
org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:303)
        at
org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:911)
        at
org.springframework.osgi.context.support.AbstractOsgiBundleApplicationContext.finishRefresh(AbstractOsgiBundleApplicationContext.java:235)
        at
org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:358)
        at
org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
        at
org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)
        at
org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:136)
        at java.lang.Thread.run(Thread.java:662)
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: /
192.168.0.111:50001
        at
org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:303)
        at
org.apache.camel.component.netty.NettyConsumer.initializeTCPServerSocketCommunicationLayer(NettyConsumer.java:168)
        at
org.apache.camel.component.netty.NettyConsumer.doStart(NettyConsumer.java:69)
        at
org.apache.camel.impl.ServiceSupport.start(ServiceSupport.java:65)
        at
org.apache.camel.impl.ServiceSupport.start(ServiceSupport.java:52)
        at
org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:52)
        at
org.apache.camel.impl.DefaultCamelContext.startServices(DefaultCamelContext.java:1593)
        at
org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:1859)
        at
org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:1812)
        at
org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:1738)
        at
org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:1528)
        at
org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:1420)
        at
org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:1321)
        at
org.apache.camel.spring.SpringCamelContext.doStart(SpringCamelContext.java:164)
        at
org.apache.camel.impl.ServiceSupport.start(ServiceSupport.java:65)
        at
org.apache.camel.impl.ServiceSupport.start(ServiceSupport.java:52)
        at
org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:1299)
        at
org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:203)
        at
org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:101)
        ... 10 more
Caused by: java.net.BindException: Cannot assign requested address: bind
        at sun.nio.ch.Net.bind(Native Method)
        at
sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:126)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
        at
org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.bind(NioServerSocketPipelineSink.java:148)
        at
org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleServerSocket(NioServerSocketPipelineSink.java:100)
        at
org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:74)
        at org.jboss.netty.channel.Channels.bind(Channels.java:468)
        at
org.jboss.netty.channel.AbstractChannel.bind(AbstractChannel.java:200)
        at
org.jboss.netty.bootstrap.ServerBootstrap$Binder.channelOpen(ServerBootstrap.java:348)
        at
org.jboss.netty.channel.Channels.fireChannelOpen(Channels.java:176)
        at
org.jboss.netty.channel.socket.nio.NioServerSocketChannel.<init>(NioServerSocketChannel.java:85)
        at
org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory.newChannel(NioServerSocketChannelFactory.java:142)
        at
org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory.newChannel(NioServerSocketChannelFactory.java:90)
        at
org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:282)
        ... 28 more

------------------------------------

Also, please be aware that the notion of callbacks on a different port are
> not possible using a netty consumer. This is due to the way the TCP
> connection object is used and works. You can however use keepalives and
> communicate back and forth on the 2 TCP ports that are bound to the TCP
> connection.


 This sounds like what I am already doing using standard Java Socket:

Socket socket = new Socket(addr, port);
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
int r = bis.read();
BufferedOutputStream bos = new
BufferedOutputStream(socket.getOutputStream());
etc...

What I want is to do the same thing with Camel-Netty. Are you telling me
that this is possible? The solution I am working on right now is a custom
component as the producer and consumer endpoints and bypassing Netty
altogether. I expect this to work, but if I can do it with Netty, it would
be better.

You could however sent a port number in the message and have the server set
> up a route that dispatches to a port using a ProducerTemplate.


The server is implemented in C++, cannot be modified, and is not setup for
dispatch port configuration.


Thanks,

Namaste


--
View this message in context: http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4704615.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: two way communication with TCP server using Netty

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

Can you please post the detailed error message. 

Also, please be aware that the notion of callbacks on a different port are
not possible using a netty consumer. This is due to the way the TCP
connection object is used and works. You can however use keepalives and
communicate back and forth on the 2 TCP ports that are bound to the TCP
connection.

You could however sent a port number in the message and have the server set
up a route that dispatches to a port using a ProducerTemplate.

Cheers,

Ashwin...

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/two-way-communication-with-TCP-server-using-Netty-tp4702360p4704343.html
Sent from the Camel - Users mailing list archive at Nabble.com.