本文整理汇总了Java中java.net.ServerSocket.setReuseAddress方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocket.setReuseAddress方法的具体用法?Java ServerSocket.setReuseAddress怎么用?Java ServerSocket.setReuseAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.ServerSocket
的用法示例。
在下文中一共展示了ServerSocket.setReuseAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: await
import java.net.ServerSocket; //导入方法依赖的package包/类
public static TargetMonitor await(int port) {
try {
final ServerSocket serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(ACCEPT_TIMEOUT_MILLIS);
serverSocket.setReuseAddress(true);
final Socket socket = serverSocket.accept();
return new TargetMonitor(new PrintStream(socket.getOutputStream())) {
@Override public void close() throws IOException {
socket.close();
serverSocket.close();
}
};
} catch (IOException e) {
throw new RuntimeException("Failed to accept a monitor on localhost:" + port, e);
}
}
示例2: init
import java.net.ServerSocket; //导入方法依赖的package包/类
private void init() {
ServerSocketChannel socketChannel = processor.javaChannel();
ServerSocket javaSocket = socketChannel.socket();
try {
if (serverConfig.getReceiveBufferSize() != null) {
javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize());
}
if (serverConfig.getReuseAddress() != null) {
javaSocket.setReuseAddress(serverConfig.getReuseAddress());
}
} catch (SocketException e) {
throw new NioException("config channel error:" + e.getMessage(), e);
}
}
示例3: listen
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Listen for client connections
*
* @throws IOException server socket cannot be created on port
*/
private void listen() throws IOException {
try {
Log.d(TAG, "listen: Create server socket");
ServerSocket rtspServerSocket = new ServerSocket();
rtspServerSocket.setReuseAddress(true);
rtspServerSocket.bind(new InetSocketAddress(port));
while (!Thread.interrupted()) {
Log.d(TAG, "listening");
// Blocking
EventBus.getDefault().post(new RtspServerEvent.Connection(this, rtspServerSocket.accept()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例4: start
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Starts the AndroidServerSocket thread
* <br>Created server socket opens on given port
*/
public void start(){
if(!m_androidServerState.equals(AndroidServerState.PREINIT)){ // This should never happen
System.out.println("Error: in AndroidServerSocket.start(), " +
"socket is already initialized");
}
if(m_running){ // This should never happen
System.out.println("Error: in AndroidServerSocket.start(), " +
"thread is already running");
}
try {
m_server = new ServerSocket(m_port);
m_server.setReuseAddress(true);
} catch (IOException e) {
e.printStackTrace();
}
this.SetState(AndroidServerState.CONNECTING);
m_running = true;
System.out.println("Starting Thread: AndroidServerSocket on port "+m_port);
(new Thread(this, "AndroidServerSocket")).start();
}
示例5: Monitor
import java.net.ServerSocket; //导入方法依赖的package包/类
public Monitor(int port, String key, Server server) throws IOException {
this.server = server;
if (port <= 0) {
throw new IllegalStateException("Bad stop port");
}
if (key == null) {
throw new IllegalStateException("Bad stop key");
}
this.key = key;
setDaemon(true);
setName("StopJettyPluginMonitor");
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
serverSocket.setReuseAddress(true);
}
示例6: startServerSocket
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Creates a ServerSocket if there isn't any service running.
* @return opened ServerSocket
* @throws IOException IO Exception if something weird is happening
*/
private ServerSocket startServerSocket(final int port) throws IOException {
if(!this.serverSocketStarted){
log.log(Level.WARNING, "Trying to start a ServerSocket on port: " + port);
final ServerSocket socket = new ServerSocket(port);
socket.setReuseAddress(true);
this.serverSocketStarted = true;
this.serverSocketKeepAlive = true;
return socket;
}else{
log.log(Level.WARNING,"There is currently a ServerSocket running on port: " + port);
return null;
}
}
示例7: start
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Starts the DataServerThread thread
* <br>Created server socket opens on given port
*/
public void start(){
if(!m_MJPEGServerState.equals(MJPEGServerState.PREINIT)){ // This should never happen
System.out.println("Error: in DataServerThread.start(), " +
"socket is already initialized");
}
if(m_running){ // This should never happen
System.out.println("Error: in DataServerThread.start(), " +
"thread is already running");
}
try {
m_server = new ServerSocket(m_port);
m_server.setReuseAddress(true);
System.out.println("Vision stream server started");
} catch (IOException e) {
e.printStackTrace();
}
this.SetState(MJPEGServerState.CONNECTING);
m_running = true;
System.out.println("Starting Thread: MJPEGServerThread on port "+m_port);
(new Thread(this, "MJPEGServerThread")).start();
}
示例8: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext context = SpringApplication.run(ChannelServerStarter.class, args);
// 현재 실행된 서버의 공인아이피 조회
String hostCheckUrl = context.getEnvironment().getProperty("channelServer.hostCheckUrl");
// 서버 설정 application.yml에서 로드
SocketServerProperty socketServerProperty = context.getBean(SocketServerProperty.class);
String serverHost;
try {
serverHost = new RestTemplate().getForObject(hostCheckUrl, String.class);
socketServerProperty.setHostIp(serverHost);
} catch (Exception e) {
logger.error("Not Found Host : check url - {}", hostCheckUrl);
System.exit(-1);
}
// 랜덤포트로 설정 되어있다면 가용한 포트를 할당
if (socketServerProperty.isBindPortRandom()) {
ServerSocket s = new ServerSocket(0);
s.setReuseAddress(true);
socketServerProperty.setBindPort(s.getLocalPort());
s.close();
} else {
socketServerProperty.setBindPort(socketServerProperty.getBindPort());
}
SocketChannelServer socketChannelServer = context.getBean(SocketChannelServer.class);
socketChannelServer.run();
}
示例9: serveOne
import java.net.ServerSocket; //导入方法依赖的package包/类
private void serveOne() {
try {
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.setSoTimeout(SERVER_SOCKET_TIMEOUT_IN_MILLISECONDS);
serverSocket.bind(new InetSocketAddress(HOST, PORT));
acceptLatch.countDown();
// accept a new client
Socket clientSocket = serverSocket.accept();
if (config.closeConnectionAfterAccept) {
clientSocket.close();
serverSocket.close();
return;
}
// read data from client
byte[] dataReceived = new byte[config.messageSentToServer.getBytes().length];
int read = 0;
do {
read += clientSocket.getInputStream().read(dataReceived, read, dataReceived.length - read);
} while (read < dataReceived.length);
messageReceivedByServer = dataReceived;
// write data back to client
clientSocket.getOutputStream().write(dataReceived);
clientSocket.close();
serverSocket.close();
} catch(IOException e) {
exceptionEncounteredDuringServe = e;
}
}
示例10: open
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected ServerSocketChannel open(SocketAddress localAddress) throws Exception {
// Creates the listening ServerSocket
ServerSocketChannel channel = ServerSocketChannel.open();
boolean success = false;
try {
// This is a non blocking socket channel
channel.configureBlocking(false);
// Configure the server socket,
ServerSocket socket = channel.socket();
// Set the reuseAddress flag accordingly with the setting
socket.setReuseAddress(isReuseAddress());
// and bind.
socket.bind(localAddress, getBacklog());
// Register the channel within the selector for ACCEPT event
channel.register(selector, SelectionKey.OP_ACCEPT);
success = true;
} finally {
if (!success) {
close(channel);
}
}
return channel;
}
示例11: createServerSocket
import java.net.ServerSocket; //导入方法依赖的package包/类
@Override
public ServerSocket createServerSocket(int p) throws IOException {
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(host, childPort), Integer.MAX_VALUE);
return serverSocket;
}
示例12: setProperties
import java.net.ServerSocket; //导入方法依赖的package包/类
public void setProperties(ServerSocket socket) throws SocketException {
if (rxBufSize != null)
socket.setReceiveBufferSize(rxBufSize.intValue());
if (performanceConnectionTime != null && performanceLatency != null && performanceBandwidth != null)
socket.setPerformancePreferences(performanceConnectionTime.intValue(), performanceLatency.intValue(),
performanceBandwidth.intValue());
if (soReuseAddress != null)
socket.setReuseAddress(soReuseAddress.booleanValue());
if (soTimeout != null && soTimeout.intValue() >= 0)
socket.setSoTimeout(soTimeout.intValue());
}
示例13: start
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Starts the DataServerThread thread
* <br>Created server socket opens on given port
* @param port Port to start Server on
*/
public void start(int port){
if(!m_socketState.equals(SocketState.PREINIT)){ // This should never happen
System.out.println("Error: in DataServerThread.start(), " +
"socket is already initialized");
}
if(m_running){ // This should never happen
System.out.println("Error: in DataServerThread.start(), " +
"thread is already running");
}
m_port = port;
try {
m_server = new ServerSocket(m_port);
m_server.setReuseAddress(true);
} catch (IOException e) {
e.printStackTrace();
}
this.SetState(SocketState.IDLE);
m_running = true;
System.out.println("Starting Thread: DataServerThread on port "+port);
(new Thread(this, "DataServerThread")).start();
}
示例14: getUnusedPort
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Returns an unused port in the localhost.
*/
public static int getUnusedPort() throws IOException {
ServerSocket socket = new ServerSocket(0);
socket.setReuseAddress(true);
int port = socket.getLocalPort();
socket.close();
return port;
}
示例15: findAvailablePort
import java.net.ServerSocket; //导入方法依赖的package包/类
public static int findAvailablePort(int defaultPort) {
try {
ServerSocket socket = new ServerSocket(0);
socket.setReuseAddress(true);
int port = socket.getLocalPort();
socket.close();
return port;
} catch (IOException ex) {
return defaultPort;
}
}