Java Code Examples for java.net.ServerSocket#getInetAddress()

The following examples show how to use java.net.ServerSocket#getInetAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerSocketTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testTimeoutAfterAccept() throws Exception {
    final ServerSocket ss = new ServerSocket(0);
    ss.setReuseAddress(true);
    // On Unix, the receive timeout is inherited by the result of accept(2).
    // Java specifies that it should always be 0 instead.
    ss.setSoTimeout(1234);
    final Socket[] result = new Socket[1];
    Thread t = new Thread(new Runnable() {
        public void run() {
            try {
                result[0] = ss.accept();
            } catch (IOException ex) {
                ex.printStackTrace();
                fail();
            }
        }
    });
    t.start();
    new Socket(ss.getInetAddress(), ss.getLocalPort());
    t.join();
    assertEquals(0, result[0].getSoTimeout());
}
 
Example 2
Source File: SSLSocketTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_SSLSocket_setSoTimeout_basic() throws Exception {
    ServerSocket listening = new ServerSocket(0);

    Socket underlying = new Socket(listening.getInetAddress(), listening.getLocalPort());
    assertEquals(0, underlying.getSoTimeout());

    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket wrapping = sf.createSocket(underlying, null, -1, false);
    assertEquals(0, wrapping.getSoTimeout());

    // setting wrapper sets underlying and ...
    int expectedTimeoutMillis = 1000;  // 10 was too small because it was affected by rounding
    wrapping.setSoTimeout(expectedTimeoutMillis);
    // The kernel can round the requested value based on the HZ setting. We allow up to 10ms.
    assertTrue(Math.abs(expectedTimeoutMillis - wrapping.getSoTimeout()) <= 10);
    assertTrue(Math.abs(expectedTimeoutMillis - underlying.getSoTimeout()) <= 10);

    // ... getting wrapper inspects underlying
    underlying.setSoTimeout(0);
    assertEquals(0, wrapping.getSoTimeout());
    assertEquals(0, underlying.getSoTimeout());
}
 
Example 3
Source File: SSLSocketTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_SSLSocket_setSoTimeout_wrapper() throws Exception {
    if (StandardNames.IS_RI) {
        // RI cannot handle this case
        return;
    }
    ServerSocket listening = new ServerSocket(0);

    // setSoTimeout applies to read, not connect, so connect first
    Socket underlying = new Socket(listening.getInetAddress(), listening.getLocalPort());
    Socket server = listening.accept();

    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket clientWrapping = sf.createSocket(underlying, null, -1, false);

    underlying.setSoTimeout(1);
    try {
        clientWrapping.getInputStream().read();
        fail();
    } catch (SocketTimeoutException expected) {
    }

    clientWrapping.close();
    server.close();
    underlying.close();
    listening.close();
}
 
Example 4
Source File: EvilInstrument.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public EvilTransformer() throws Exception {
    scratch = createScratchFile();
    ss = new ServerSocket(0);
    new SocketEcho().start();
    s = new Socket(ss.getInetAddress(), ss.getLocalPort());
    socketEchoReady.await();
    inited = true;
}
 
Example 5
Source File: EvilInstrument.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public EvilTransformer() throws Exception {
    scratch = createScratchFile();
    ss = new ServerSocket(0);
    new SocketEcho().start();
    s = new Socket(ss.getInetAddress(), ss.getLocalPort());
    socketEchoReady.await();
    inited = true;
}
 
Example 6
Source File: EvilInstrument.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public EvilTransformer() throws Exception {
    scratch = createScratchFile();
    ss = new ServerSocket(0);
    new SocketEcho().start();
    s = new Socket(ss.getInetAddress(), ss.getLocalPort());
    socketEchoReady.await();
    inited = true;
}
 
Example 7
Source File: ServerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    clearWorkDir();
    ServerSocket srv = new ServerSocket(0, 1, InetAddress.getLoopbackAddress());
    serverThread = new Thread(() -> {
        try {
            Socket server = srv.accept();

            Path tempDir = Files.createTempDirectory("lsp-server");
            File userdir = tempDir.resolve("scratch-user").toFile();
            File cachedir = tempDir.resolve("scratch-cache").toFile();
            System.setProperty("netbeans.user", userdir.getAbsolutePath());
            File varLog = new File(new File(userdir, "var"), "log");
            varLog.mkdirs();
            System.setProperty("jdk.home", System.getProperty("java.home")); //for j2seplatform
            Class<?> main = Class.forName("org.netbeans.core.startup.Main");
            main.getDeclaredMethod("initializeURLFactory").invoke(null);
            new File(cachedir, "index").mkdirs();
            Class jsClass = JavaSource.class;
            File javaCluster = Utilities.toFile(jsClass.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile().getParentFile();
            System.setProperty("netbeans.dirs", javaCluster.getAbsolutePath());
            CacheFolderProvider.getCacheFolderForRoot(Places.getUserDirectory().toURI().toURL(), EnumSet.noneOf(CacheFolderProvider.Kind.class), CacheFolderProvider.Mode.EXISTENT);

            Lookup.getDefault().lookup(ModuleInfo.class); //start the module system

            CommandLine.getDefault().process(new String[] {"--start-java-language-server"}, server.getInputStream(), server.getOutputStream(), System.err, getWorkDir());
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    });
    serverThread.start();
    client = new Socket(srv.getInetAddress(), srv.getLocalPort());
}
 
Example 8
Source File: HttpServer.java    From java-android-websocket-client with Apache License 2.0 5 votes vote down vote up
public InetAddress getInetAddress() {
    final ServerSocket localSocket = this.serverSocket;
    if (localSocket != null) {
        return localSocket.getInetAddress();
    } else {
        return null;
    }
}
 
Example 9
Source File: SSLSocketTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void test_SSLSocket_interrupt_case(boolean readUnderlying, boolean closeUnderlying)
        throws Exception {

    ServerSocket listening = new ServerSocket(0);

    Socket underlying = new Socket(listening.getInetAddress(), listening.getLocalPort());
    Socket server = listening.accept();

    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket clientWrapping = sf.createSocket(underlying, null, -1, true);

    final Socket toRead = (readUnderlying) ? underlying : clientWrapping;
    final Socket toClose = (closeUnderlying) ? underlying : clientWrapping;

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {
        @Override public Void call() throws Exception {
            Thread.sleep(1 * 1000);
            toClose.close();
            return null;
        }
    });
    executor.shutdown();
    try {
        toRead.setSoTimeout(5 * 1000);
        toRead.getInputStream().read();
        fail();
    } catch (SocketTimeoutException e) {
        throw e;
    } catch (SocketException expected) {
    }
    future.get();

    server.close();
    underlying.close();
    listening.close();
}
 
Example 10
Source File: QoSService.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param executor
 * @param socket
 */
public QoSService(ExecutorService executor, ServerSocket socket, SSLContext sslContext) {
	this.executor = executor;
	this.socket = socket;
	this.sslContext = sslContext;
	this.name = "[QoSService " +  socket.getInetAddress() + ":" + socket.getLocalPort() +"]: ";
}
 
Example 11
Source File: ProxyServer.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void onBind(ProxyMessage msg) throws IOException{
   ProxyMessage response = null;

   if(proxy == null)
     ss = new ServerSocket(0);
   else
     ss = new SocksServerSocket(proxy, msg.ip, msg.port);

   ss.setSoTimeout(acceptTimeout);

   log("Trying accept on "+ss.getInetAddress()+":"+ss.getLocalPort());

   if(msg.version == 5)
      response = new Socks5Message(Proxy.SOCKS_SUCCESS,ss.getInetAddress(),
                                                       ss.getLocalPort());
   else
      response = new Socks4Message(Socks4Message.REPLY_OK,
                                   ss.getInetAddress(),
                                   ss.getLocalPort());
   response.write(out);

   mode = ACCEPT_MODE;

   pipe_thread1 = Thread.currentThread();
   pipe_thread2 = new Thread(this);
   pipe_thread2.start();

   //Make timeout infinit.
   sock.setSoTimeout(0);
   int eof=0;

   try{
     while((eof=in.read())>=0){
       if(mode != ACCEPT_MODE){
         if(mode != PIPE_MODE) return;//Accept failed

         remote_out.write(eof);
         break;
       }
     }
   }catch(EOFException eofe){
     //System.out.println("EOF exception");
     return;//Connection closed while we were trying to accept.
   }catch(InterruptedIOException iioe){ 
     //Accept thread interrupted us.
     //System.out.println("Interrupted");
     if(mode != PIPE_MODE)
       return;//If accept thread was not successfull return.
   }finally{
     //System.out.println("Finnaly!");
   }

   if(eof < 0)//Connection closed while we were trying to accept;
     return;
   
   //Do not restore timeout, instead timeout is set on the
   //remote socket. It does not make any difference.

   pipe(in,remote_out);
}