我有一个使用 Netty 进行套接字 I/O 的服务器任务。它绑定到端口 MY_PORT 并从客户端接收 UDP 消息。它响应这些客户端,将消息发送回目标端口为 MY_PORT 的客户端。使用wireshark,我看到从我的服务器传出的数据包也有一个源端口MY_PORT。这一切都很好。
负责服务器和客户端之间网络的人员在使用负载平衡器时遇到了一些问题。他们说,如果我的服务器发送给客户端的 UDP 消息的源端口与用于目标的端口不同,这将对他们有所帮助。
我查看了 Netty API,但我不确定如何做到这一点。似乎因为我已经绑定到本地端口,所以我也必须将它用于传出数据包??这是我的代码的精简版本。
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
public class UdpServer {
private final int port;
private Channel serverChannel;
public UdpServer( int port ) {
super();
this.port = port;
}
public void start() {
NioDatagramChannelFactory serverChannelFactory =
new NioDatagramChannelFactory( Executors.newCachedThreadPool(), 1 );
ConnectionlessBootstrap serverBootstrap =
new ConnectionlessBootstrap( serverChannelFactory );
serverBootstrap.setPipelineFactory( new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() {
return Channels.pipeline( new SimpleChannelHandler() {
@Override
public void messageReceived( ChannelHandlerContext ctx,
MessageEvent e ) {
// TODO, handle message from client
}
} );
}
} );
serverBootstrap.setOption( "reuseAddress", Boolean.TRUE );
final InetSocketAddress trafficAddress = new InetSocketAddress( port );
serverChannel = serverBootstrap.bind( trafficAddress );
}
public void sendMessage( byte[] message, String clientIp )
throws UnknownHostException {
// TODO, how do I control the source port of this packet??
SocketAddress address =
new InetSocketAddress( InetAddress.getByName( clientIp ), port );
ChannelBuffer buffer = ChannelBuffers.wrappedBuffer( message );
serverChannel.write( buffer, address );
}
}