当我尝试连接到代理服务器的本地端口时,我收到绑定错误

时间:2014-07-01 16:10:47

标签: java netty

我使用Netty创建了一个代理服务器。代理服务器是在示例之后建模的。如果我的关机方法正确吗?

我正在使用servlet接收请求,解析标头信息并将请求发送到代理服务器。我第一次运行jMeter测试时绑定正确。第二次运行jMeter测试时出现此错误:

The server encountered an internal error () that prevented it 
from fulfilling this              
request.org.jboss.netty.channel.ChannelException:
Failed to bind to: /0.0.0.0:2011
org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:272)

这是我在服务器中的run()方法和服务器中的shutdown方法:

public void run() {
logger.debug("starting the run() method in XxxxxxxxxServer");

// Configure the bootstrap
Executor executor   =   Executors.newCachedThreadPool();
ServerBootstrap serverBootstrap =   
new ServerBootstrap( new     NioServerSocketChannelFactory(executor, executor));

// set up the message pipeline
ClientSocketChannelFactory clientFactory    =
new NioClientSocketChannelFactory(executor, executor);

serverBootstrap.setPipelineFactory(new XxxxxxxxxPipelineFactory(clientFactory, VRSserver, remotePort));

String binding = "0.0.0.0";
// start the server, creating the new local port for the device
channel = serverBootstrap.bind(new InetSocketAddress(binding ,localPort));
}


public void shutdown() {
logger.debug("starting the shutdown() method in AccugradeServer");

// unbind all channels created by the factory
if (serverBootstrap != null) {
    serverBootstrap.getFactory().releaseExternalResources();
    serverBootstrap.getFactory().shutdown();
}

// close all child channels accepted by the unbound channels
if (channel != null) {
    channel.unbind();
    channel.close().awaitUninterruptibly();
}

// releasing resources
if (serverBootstrap != null) {
    serverBootstrap.releaseExternalResources();
    serverBootstrap.shutdown();
}

}

1 个答案:

答案 0 :(得分:1)

你应该绑定到本地ip,即:

String binding = "127.0.0.1";

并检查您在端口2011上没有任何正在运行的服务:

netstat -antp | grep 2011

应该不返回任何行