本文整理汇总了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"));
}
}
示例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;
}
示例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);
}
}
示例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);
}
示例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();
}
}
示例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;
}
}
示例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;
}
示例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");
}
示例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));
}
示例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;
}
示例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() };
}
示例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...");
}
示例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);
}
}
示例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);
}
示例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();
}