当前位置: 首页>>代码示例>>Java>>正文


Java ServerSocket.setReuseAddress方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:dryganets,项目名称:vogar,代码行数:18,代码来源:TargetMonitor.java

示例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);
        }
    }
 
开发者ID:lemonJun,项目名称:TakinRPC,代码行数:18,代码来源:NioServer.java

示例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();
    }
}
 
开发者ID:devinbrown7,项目名称:streaminglib,代码行数:23,代码来源:RtspServer.java

示例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();
}
 
开发者ID:team8,项目名称:FRC-2017-Public,代码行数:29,代码来源:AndroidServerSocket.java

示例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);
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:16,代码来源:Monitor.java

示例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;
    }
}
 
开发者ID:deB4SH,项目名称:Byter,代码行数:19,代码来源:NetworkManager.java

示例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();
}
 
开发者ID:team8,项目名称:FRC-2017-Public,代码行数:31,代码来源:MJPEGServerSocket.java

示例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();
    }
 
开发者ID:veritasware,项目名称:neto,代码行数:34,代码来源:ChannelServerStarter.java

示例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;
    }
}
 
开发者ID:cpppwner,项目名称:NoRiskNoFun,代码行数:37,代码来源:TCPClientSocketImplTests.java

示例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;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:34,代码来源:NioSocketAcceptor.java

示例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;
}
 
开发者ID:mware-solutions,项目名称:sether,代码行数:8,代码来源:SocketFactory.java

示例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());
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:12,代码来源:SocketProperties.java

示例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();
   }
 
开发者ID:team8,项目名称:FRC-2017-Public,代码行数:32,代码来源:DataServerThread.java

示例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;
}
 
开发者ID:aws,项目名称:aws-sdk-java-v2,代码行数:11,代码来源:SocketUtils.java

示例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;
    }
}
 
开发者ID:shlee89,项目名称:athena,代码行数:12,代码来源:TestTools.java


注:本文中的java.net.ServerSocket.setReuseAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。