Reputation: 432
I have the following files:
https://gist.github.com/anonymous/58c7cf4341acfe83f279
I am aware I can greatly simplify this, I am simply doing this for conceptual reasons.
When I run this, only the UDP connection works. When I comment out the UDP, the TCP works fine.
Why are both sockets not working at the same time? I feel like it's something to do with threading but if I understand correctly they are both using different thread pools so I am completely at a loss.
All I want to do currently is have something to listen/write to one socket for TCP, and one socket for UDP. (Possibly have a UDP write as a 3rd socket).
Any advice?
Upvotes: 0
Views: 1388
Reputation: 353
Your servers are executed in a sequential way. Only when your UDP server is closed, then your TCP is executed.
One solution is to modify the UDP server to avoid blocking at the end. Change:
b.bind(port).sync().channel().closeFuture().await();
to:
b.bind(port);
And drop the group.shutdownGracefully() in the finally (you will have to do at another place anyway.)
Another (maybe better) way: The code could be adapted to be executed in two threads in order to allow concurrent execution. Add "implmement Runnable" to your UdpServer and TcpServer, and drop the throws Exception by catching it. Next run two threads from main:
new Thread(new UdpServer(9094)).start();
new Thread(new TcpServer(9093)).start();
Upvotes: 1