java.net.ServerSocket.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(174)

本文整理了Java中java.net.ServerSocket.<init>()方法的一些代码示例,展示了ServerSocket.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServerSocket.<init>()方法的具体详情如下:
包路径:java.net.ServerSocket
类名称:ServerSocket
方法名:<init>

ServerSocket.<init>介绍

[英]Constructs a new unbound ServerSocket.
[中]构造一个新的未绑定ServerSocket。

代码示例

代码示例来源:origin: stackoverflow.com

ServerSocket s = new ServerSocket(0);
System.out.println("listening on port: " + s.getLocalPort());

代码示例来源:origin: apache/zeppelin

public static int findRandomAvailablePortOnAllLocalInterfaces() throws IOException {
 int port;
 try (ServerSocket socket = new ServerSocket(0);) {
  port = socket.getLocalPort();
  socket.close();
 }
 return port;
}

代码示例来源:origin: stackoverflow.com

import java.io.*;
import java.net.*;

public class SelectTest {
 public static void main(String[] args) throws Exception {
  // Use 1024 file descriptors. There'll already be some in use, obviously, but this guarantees the problem will occur
  for(int i = 0; i < 1024; i++) {
   new FileInputStream("/dev/null");
  }
  ServerSocket socket = new ServerSocket(8080);
  socket.accept();
 }
}

代码示例来源:origin: stackoverflow.com

ServerSocket serverSocket = new ServerSocket(4444);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
 out.println("output");
 if (out.checkError()) System.out.println("ERROR writing data to socket !!!");
 System.out.println(clientSocket.isConnected());
 System.out.println(clientSocket.getInputStream().read());
    // thread sleep ...
 // break condition , close sockets and the like ...
}

代码示例来源:origin: stackoverflow.com

public class ThreadedEchoServer {

  static final int PORT = 1978;

  public static void main(String args[]) {
    ServerSocket serverSocket = null;
    Socket socket = null;

    try {
      serverSocket = new ServerSocket(PORT);
    } catch (IOException e) {
      e.printStackTrace();

    }
    while (true) {
      try {
        socket = serverSocket.accept();
      } catch (IOException e) {
        System.out.println("I/O error: " + e);
      }
      // new threa for a client
      new EchoThread(socket).start();
    }
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Application method to run the server runs in an infinite loop
 * listening on port 9898.  When a connection is requested, it
 * spawns a new thread to do the servicing and immediately returns
 * to listening.  The server keeps a unique client number for each
 * client that connects just to show interesting logging
 * messages.  It is certainly not necessary to do this.
 */
public static void main(String[] args) throws IOException {
 System.out.println("The modeling server is running.");
 int clientNumber = 0;
 ServerSocket listener = new ServerSocket(9898);
 try {
  while (true) {
   new PerformActionUpdateModel(listener.accept(), clientNumber++).start();
  }
 } finally {
  listener.close();
 }
}

代码示例来源:origin: stackoverflow.com

public void run() {
  try {
    ServerSocket serverSocket = new ServerSocket(8000);
    System.out.println("Waiting for clients to connect...");
    while (true) {
      Socket clientSocket = serverSocket.accept();
      clientProcessingPool.submit(new ClientTask(clientSocket));
    System.err.println("Unable to process client request");
    e.printStackTrace();
System.out.println("Got a client !");
  clientSocket.close();
} catch (IOException e) {
  e.printStackTrace();

代码示例来源:origin: apache/incubator-druid

final ServerSocket serverSocket = new ServerSocket(0);
exec.submit(
  new Runnable()
   .setRequestTimeout(2000)
   .execute();
 System.out.println("created future in: " + (System.currentTimeMillis() - requestStart));
 future.get(3000, TimeUnit.MILLISECONDS);
 Assert.fail("Expected timeout");
 serverSocket.close();

代码示例来源:origin: androidannotations/androidannotations

/**
 * Main server loop.
 */
@Override
public void run() {
  try {
    mServer = new ServerSocket(mPort, VIEW_SERVER_MAX_CONNECTIONS, InetAddress.getLocalHost());
  } catch (Exception e) {
    Log.w(LOG_TAG, "Starting ServerSocket error: ", e);
  }
  while (mServer != null && Thread.currentThread() == mThread) {
    // Any uncaught exception will crash the system process
    try {
      Socket client = mServer.accept();
      if (mThreadPool != null) {
        mThreadPool.submit(new ViewServerWorker(client));
      } else {
        try {
          client.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    } catch (Exception e) {
      Log.w(LOG_TAG, "Connection error: ", e);
    }
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Override
public InputStream call() throws Exception {
  server = new ServerSocket(SOCKET_PORT);
  client = server.accept();
  return client.getInputStream();
}

代码示例来源:origin: plantuml/plantuml

private void listPassif() throws UnknownHostException, IOException {
  myOut("150 Opening ASCII mode data");
  final ServerSocket ss = new ServerSocket(port);
  final Socket incoming = ss.accept();
  list(incoming);
  ss.close();
}

代码示例来源:origin: stackoverflow.com

String msg_received;

ServerSocket socket = new ServerSocket(1755);
Socket clientSocket = socket.accept();       //This is blocking. It will wait.
DataInputStream DIS = new DataInputStream(clientSocket.getInputStream());
msg_received = DIS.readUTF();
clientSocket.close();
socket.close();

代码示例来源:origin: plantuml/plantuml

public void go() throws IOException {
  final ServerSocket s = new ServerSocket(listenPort);
  final ExecutorService exe = Executors.newCachedThreadPool();
  while (true) {
    final Socket incoming = s.accept();
    ip = incoming.getLocalAddress().getHostAddress();
    System.out.println("New Client Connected from " + incoming.getInetAddress().getHostName() + "... ");
    exe.submit(new FtpLoop(incoming, this));
  }
}

代码示例来源:origin: stackoverflow.com

public class Server {

  public static void main(String[] args) throws IOException {

    ServerSocket serverSocket = null;

    boolean listeningSocket = true;
    try {
      serverSocket = new ServerSocket(2343);
    } catch (IOException e) {
      System.err.println("Could not listen on port: 2343");
    }

    while(listeningSocket){
      Socket clientSocket = serverSocket.accept();
      MiniServer mini = new MiniServer(clientSocket);
      mini.start();
    }
    serverSocket.close();       
  }

}

代码示例来源:origin: stackoverflow.com

private static final int PORT = 9999;
private static ServerSocket socket;    

private static void checkIfRunning() {
 try {
  //Bind to localhost adapter with a zero connection queue 
  socket = new ServerSocket(PORT,0,InetAddress.getByAddress(new byte[] {127,0,0,1}));
 }
 catch (BindException e) {
  System.err.println("Already running.");
  System.exit(1);
 }
 catch (IOException e) {
  System.err.println("Unexpected error.");
  e.printStackTrace();
  System.exit(2);
 }
}

代码示例来源:origin: apache/incubator-druid

@VisibleForTesting
boolean canBind(int portNum)
{
 try {
  new ServerSocket(portNum).close();
  return true;
 }
 catch (SocketException se) {
  return false;
 }
 catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: plantuml/plantuml

public static void main(String args[]) throws Exception {
  final ServerSocket server = new ServerSocket(4242);
  while (true) {
    final Socket socket = server.accept();
    new AcceptTelnetClient(socket);
  }
}

代码示例来源:origin: amitshekhariitbhu/Android-Debug-Database

@Override
public void run() {
  try {
    mServerSocket = new ServerSocket(mPort);
    while (mIsRunning) {
      Socket socket = mServerSocket.accept();
      mRequestHandler.handle(socket);
      socket.close();
    }
  } catch (SocketException e) {
    // The server was stopped; ignore.
  } catch (IOException e) {
    Log.e(TAG, "Web server error.", e);
  } catch (Exception ignore) {
    Log.e(TAG, "Exception.", ignore);
  }
}

代码示例来源:origin: apache/ignite

@Nullable @Override public Object call() throws Exception {
    ServerSocket srvSock = null;
    Socket sock = null;
    try {
      srvSock = new ServerSocket(60000, 0, addr);
      sock = srvSock.accept();
      X.println("Socket [timeout=" + sock.getSoTimeout() + ", linger=" + sock.getSoLinger() +
        ", sndBuf=" + sock.getSendBufferSize() + ", sndBuf=" + sock.getSendBufferSize() + ']');
      sock.setKeepAlive(true);
      sock.setSoTimeout(2000);
      sock.setSendBufferSize(256 * 1024);
      X.println("Socket [timeout=" + sock.getSoTimeout() + ", linger=" + sock.getSoLinger() +
        ", sndBuf=" + sock.getSendBufferSize() + ", rcvBuf=" + sock.getReceiveBufferSize() + ']');
      while (!done.get())
        X.println("Read from socket: " + sock.getInputStream().read());
      return null;
    }
    finally {
      U.closeQuiet(srvSock);
      U.closeQuiet(sock);
    }
  }
},

代码示例来源:origin: plantuml/plantuml

private void storPassif(final String s) throws IOException {
  final String fileName = removeStartingsSlash(s.substring("STOR ".length()));
  myOut("150 FILE: " + fileName);
  final ServerSocket ss = new ServerSocket(port);
  final Socket incoming = ss.accept();
  stor(fileName, incoming);
  ss.close();
}

相关文章