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


Java ServerSocket.bind方法代码示例

本文整理汇总了Java中java.net.ServerSocket.bind方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocket.bind方法的具体用法?Java ServerSocket.bind怎么用?Java ServerSocket.bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.ServerSocket的用法示例。


在下文中一共展示了ServerSocket.bind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: printSockets

import java.net.ServerSocket; //导入方法依赖的package包/类
@Test
public void printSockets() throws IOException {
	
	ServerSocket ss = new ServerSocket();
	ss.bind(sock(5000));
	
	Socket s1 = new Socket();
	Socket s2 = new Socket();
	
	s1.connect(sock(5000));
	s2.connect(sock(5000));
	
	ss.close();
	s1.close();
	// s2 remains unclosed
	
	HeapImage hi = HeapUnit.captureHeap();
	
	for(HeapInstance i: hi.instances(SocketImpl.class)) {
		// fd field in SocketImpl class is nullified when socket gets closed
		boolean open = i.value("fd") != null;
		System.out.println(i.rehydrate() + (open ? " - open" : " - closed"));
	}
}
 
开发者ID:aragozin,项目名称:heapunit,代码行数:25,代码来源:DumpSocketsExample.java

示例2: getAvailablePort

import java.net.ServerSocket; //导入方法依赖的package包/类
private int getAvailablePort(int fromPort, int toPort) {
	if (toPort <= fromPort) return fromPort;
	int count = 50;
	int step = (toPort - fromPort) / count;
	while (count > 0) {
		int port = fromPort + (50-count) * step + random.nextInt(step);
		try {
		    ServerSocket s = new ServerSocket();
		    s.bind(new InetSocketAddress("localhost", port));
		    s.close();			    
		} catch (IOException ex) {
			--count;
		    continue;
		}
		return port;
	}
	return random.nextInt((toPort-fromPort)) + fromPort;
	
}
 
开发者ID:kefik,项目名称:Pogamut3,代码行数:20,代码来源:UT2004TCServer.java

示例3: listen

import java.net.ServerSocket; //导入方法依赖的package包/类
private void listen() throws IOException {
	ServerSocket ss = new ServerSocket();
	ss.bind(listenAddress);
	TokenBucket bucket = new TokenBucket();
	bucket.start();
	try {
		while (true) {
			Socket s = ss.accept();
			System.out.println("Incoming connection");
			bucket.waitForToken();
			new ReportSaver(s).start();
		}
	} catch (InterruptedException e) {
		System.err.println("Interrupted while listening");
	} finally {
		tryToClose(ss);
	}
}
 
开发者ID:rafjordao,项目名称:Nird2,代码行数:19,代码来源:DevReportServer.java

示例4: openServerSocket

import java.net.ServerSocket; //导入方法依赖的package包/类
public final void openServerSocket(InetAddress address, int tcpPort) throws IOException
{
	final ServerSocketChannel selectable = ServerSocketChannel.open();
	selectable.configureBlocking(false);
	
	final ServerSocket ss = selectable.socket();
	
	if (address != null)
	{
		ss.bind(new InetSocketAddress(address, tcpPort));
	}
	else
	{
		ss.bind(new InetSocketAddress(tcpPort));
	}
	
	selectable.register(_selector, SelectionKey.OP_ACCEPT);
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:19,代码来源:SelectorThread.java

示例5: testBind

import java.net.ServerSocket; //导入方法依赖的package包/类
@Test
public void testBind() throws Exception {
  Configuration conf = new Configuration();
  ServerSocket socket = new ServerSocket();
  InetSocketAddress address = new InetSocketAddress("0.0.0.0",0);
  socket.bind(address);
  try {
    int min = socket.getLocalPort();
    int max = min + 100;
    conf.set("TestRange", min+"-"+max);
    

    ServerSocket socket2 = new ServerSocket();
    InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
    Server.bind(socket2, address2, 10, conf, "TestRange");
    try {
      assertTrue(socket2.isBound());
      assertTrue(socket2.getLocalPort() > min);
      assertTrue(socket2.getLocalPort() <= max);
    } finally {
      socket2.close();
    }
  } finally {
    socket.close();
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:27,代码来源:TestServer.java

示例6: GroupOwnerSocketHandler

import java.net.ServerSocket; //导入方法依赖的package包/类
public GroupOwnerSocketHandler(Handler handler) throws IOException {
    try {
        server_socket = new ServerSocket();
        server_socket.setReuseAddress(true);
        server_socket.bind(new InetSocketAddress(WiFiDirectCommunicator.PORT));
        this.handler = handler;
        Log.d(TAG, "Socket Started");
    } catch (IOException e) {
        e.printStackTrace();
        pool.shutdownNow();
        throw e;
    }

}
 
开发者ID:jphacks,项目名称:TK_1701,代码行数:15,代码来源:GroupOwnerSocketHandler.java

示例7: getSocketPair

import java.net.ServerSocket; //导入方法依赖的package包/类
private static Socket getSocketPair() throws IOException {
    ServerSocket ss = new ServerSocket();
    ss.bind(null);
    InetSocketAddress endPoint = (InetSocketAddress) ss
            .getLocalSocketAddress();
    Socket s = new Socket(endPoint.getAddress(), endPoint.getPort());
    s.setSoTimeout(5000);
    return s;
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:10,代码来源:QuorumCnxManagerTest.java

示例8: ConnectionListener

import java.net.ServerSocket; //导入方法依赖的package包/类
public ConnectionListener() throws IOException
{
	socket = new ServerSocket();
	socket.bind(null);

	group = new ThreadGroup("Connections");
	setDaemon(true);
	setName("Connection Listener");
}
 
开发者ID:nickbattle,项目名称:FJ-VDMJ,代码行数:10,代码来源:ConnectionListener.java

示例9: testWildcardAddressBound

import java.net.ServerSocket; //导入方法依赖的package包/类
@Test
public void testWildcardAddressBound() throws IOException {
  // assumeFalse(SystemUtils.isWindows()); // See bug #39368
  socket = new ServerSocket();
  int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
  socket.bind(new InetSocketAddress((InetAddress) null, port));
  System.out.println(
      "bind addr=" + System.getProperty(DistributionConfig.GEMFIRE_PREFIX + "bind-address"));
  assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET));
}
 
开发者ID:ampool,项目名称:monarch,代码行数:11,代码来源:AvailablePortJUnitTest.java

示例10: createServerSocket

import java.net.ServerSocket; //导入方法依赖的package包/类
protected ServerSocket createServerSocket() throws IOException {
    ServerSocket ss = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress(InetAddress
            .getByName("localhost"), port);
    ss.bind(address);
    return ss;
}
 
开发者ID:Vitaliy-Yakovchuk,项目名称:ramus,代码行数:8,代码来源:Server.java

示例11: getSocketPair

import java.net.ServerSocket; //导入方法依赖的package包/类
static Socket[] getSocketPair() throws IOException {
    ServerSocket ss = new ServerSocket();
    ss.bind(null);
    InetSocketAddress endPoint = (InetSocketAddress) ss.getLocalSocketAddress();
    Socket s = new Socket(endPoint.getAddress(), endPoint.getPort());
    return new Socket[] { s, ss.accept() };
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:8,代码来源:Zab1_0Test.java

示例12: SocketRunnable

import java.net.ServerSocket; //导入方法依赖的package包/类
public SocketRunnable(int port, String pluginName, File pluginsFolder) {
	this.pluginName = pluginName;
	this.pluginsFolder = pluginsFolder;
	try  {
		serverSocket = new ServerSocket();
		serverSocket.bind(new InetSocketAddress("localhost", port));
	} catch (IOException e) {
		e.printStackTrace();
	}
	Bukkit.getLogger().info("Waiting...");
}
 
开发者ID:syuchan1005,项目名称:MCPluginDebuggerforMC,代码行数:12,代码来源:SocketRunnable.java

示例13: testFollowerConversation

import java.net.ServerSocket; //导入方法依赖的package包/类
public void testFollowerConversation(FollowerConversation conversation) throws Exception {
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    Thread followerThread = null;
    ConversableFollower follower = null;
    QuorumPeer peer = null;
    try {
        peer = createQuorumPeer(tmpDir);
        follower = createFollower(tmpDir, peer);
        peer.follower = follower;
        
        ServerSocket ss = new ServerSocket();
        ss.bind(null);
        QuorumServer leaderQS = new QuorumServer(1,
                (InetSocketAddress) ss.getLocalSocketAddress());
        follower.setLeaderQuorumServer(leaderQS);
        final Follower followerForThread = follower;
        
        followerThread = new Thread() {
            public void run() {
                try {
                    followerForThread.followLeader();
                } catch (InterruptedException e) {
                    LOG.info("Follower thread interrupted", e);
                } catch (Exception e) {
                    LOG.warn("Unexpected exception in follower thread", e);
                }
            }
        };
        followerThread.start();
        Socket leaderSocket = ss.accept();
        
        InputArchive ia = BinaryInputArchive.getArchive(leaderSocket
                .getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket
                .getOutputStream());

        conversation.converseWithFollower(ia, oa, follower);
    } finally {
        if (follower != null) {
            follower.shutdown();
        }
        if (followerThread != null) {
            followerThread.interrupt();
            followerThread.join();
        }
        if (peer != null) {
            peer.shutdown();
        }
        recursiveDelete(tmpDir);
    }
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:54,代码来源:Zab1_0Test.java

示例14: ServerWorker

import java.net.ServerSocket; //导入方法依赖的package包/类
/**
 * Opens {@link ServerWorker#serverSocket}.
 * 
 * @throws IOException
 */
public ServerWorker() throws IOException {
	serverSocket = new ServerSocket();
	serverSocket.bind (new InetSocketAddress (0));
    //serverSocket.configureBlocking(true);
}
 
开发者ID:kefik,项目名称:Pogamut3,代码行数:11,代码来源:NetworkLogManager.java

示例15: start

import java.net.ServerSocket; //导入方法依赖的package包/类
/**
 * Starts the server
 * <p/>
 * Throws an IOException if the socket is already in use
 */
public void start() throws IOException {
    Log.i("NanoHTTPD", "server start");
    myServerSocket = new ServerSocket();
    myServerSocket.bind((hostname != null) ? new InetSocketAddress(
            hostname, myPort) : new InetSocketAddress(myPort));

    myThread = new Thread(new Runnable() {
        @Override
        public void run() {
            do {
                try {
                    final Socket finalAccept = myServerSocket.accept();
                    Log.i("NanoHTTPD",
                            "accept request from "
                                    + finalAccept.getInetAddress());
                    InputStream inputStream = finalAccept.getInputStream();
                    OutputStream outputStream = finalAccept
                            .getOutputStream();
                    TempFileManager tempFileManager = tempFileManagerFactory
                            .create();
                    final HTTPSession session = new HTTPSession(
                            tempFileManager, inputStream, outputStream);
                    asyncRunner.exec(new Runnable() {
                        @Override
                        public void run() {
                            session.run();
                            if (finalAccept != null) {
                                try {
                                    finalAccept.close();
                                } catch (IOException ignored) {
                                }
                            }
                        }
                    });
                } catch (IOException e) {
                }
            } while (!myServerSocket.isClosed());
        }
    });
    myThread.setDaemon(true);
    myThread.setName("NanoHttpd Main Listener");
    myThread.start();
}
 
开发者ID:ynztlxdeai,项目名称:TextReader,代码行数:49,代码来源:NanoHTTPD.java


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