Package java.net

Examples of java.net.Socket


    /**
     * Attempt to send args via socket connection.
     * @return true if successful, false if connection attempt failed
     */
    public boolean sendArgs() {
      Socket sck = null;
      PrintWriter pw = null;
      try {
        String msg = "StartSocket: passing startup args to already-running Azureus java process listening on [127.0.0.1: 6880]";
       
          // DON'T USE LOGGER here as we DON't want to initialise all the logger stuff
          // and in particular AEDiagnostics config dirty stuff!!!!
       
        System.out.println( msg );
        
        sck = new Socket("127.0.0.1", 6880);
        
          // NOTE - this formatting is also used by AzureusCoreSingleInstanceClient and other org.gudy.azureus2.ui.common.Main.StartSocket
       
        pw = new PrintWriter(new OutputStreamWriter(sck.getOutputStream(),Constants.DEFAULT_ENCODING));
        
        StringBuffer buffer = new StringBuffer(AzureusCoreSingleInstanceClient.ACCESS_STRING + ";args;");
        
        for(int i = 0 ; i < args.length ; i++) {
          String arg = args[i].replaceAll("&","&&").replaceAll(";","&;");
          buffer.append(arg);
          buffer.append(';');
        }
        
        pw.println(buffer.toString());
        pw.flush();
       
        return true;
      }
      catch(Exception e) {
        e.printStackTrace();
        Debug.printStackTrace( e );
        return false//there was a problem connecting to the socket
      }
      finally {
        try {
          if (pw != nullpw.close();
        }
        catch (Exception e) {}
       
        try {
          if (sck != null)   sck.close();
        }
        catch (Exception e) {}
      }
    }
View Full Code Here


  public void run() {
    bContinue = true;
    while (bContinue) {
      BufferedReader br = null;
      try {
        Socket sck = socket.accept();
        String address = sck.getInetAddress().getHostAddress();
        if (address.equals("localhost") || address.equals("127.0.0.1")) {
          br = new BufferedReader(new InputStreamReader(sck.getInputStream()));
          String line = br.readLine();
          //System.out.println("received : " + line);
          if (line != null) {
            //            main.showMainWindow();
            StringTokenizer st = new StringTokenizer(line, ";");
            List argsList = new ArrayList();
            while( st.hasMoreElements() )
              argsList.add(st.nextToken().replaceAll("&;", ";").replaceAll("&&", "&"));
            if (argsList.size() > 1 )
            {
              String checker = (String) argsList.remove(0);
              if (checker.equals(AzureusCoreSingleInstanceClient.ACCESS_STRING)) {
                if (argsList.get(0).equals("args")) {
                  argsList.remove(0);                 
                  String newargs[] = new String[argsList.size()];
                  argsList.toArray(newargs);
                  Main.processArgs(newargs, null, null);
                } else {
                  Logger.getLogger("azureus2").error("Something strange was sent to the StartServer: " + line);
                }
              } else {
        Logger.getLogger("azureus2").error("StartServer: Wrong access token.");
              }
            }
          }
        }
        sck.close();

      } catch (Exception e) {
        if (!(e instanceof SocketException))
          e.printStackTrace();
        bContinue = false;
View Full Code Here

      }
    }.start();
   
    while( true ) {
      try {
        Socket socket = serverSocket.accept();
       
        InetSocketAddress addr = (InetSocketAddress) socket.getRemoteSocketAddress();
       
        if( addr.isUnresolved() || ! isAllowed(addr) ) {
          System.out.println("TelnetUI: rejecting connection from: " + addr + " as address is not allowed");
          socket.close();
        }
        else {
          System.out.println("TelnetUI: accepting connection from: " + addr);
          int loginAttempts = 0;
         
          while( true ) {
            // TODO: might want to put this in another thread so the port doesnt block while the user logs in
           
            //System.out.println("TelnetUI: starting login" );
           
            UserProfile profile = login( socket.getInputStream(), socket.getOutputStream() );
           
            //System.out.println("TelnetUI: login profile obtained" );
           
            if( profile != null ) {
             
              //System.out.println("TelnetUI: creating console input" );
             
              ui.createNewConsoleInput("Telnet Console " + threadNum++, socket.getInputStream(), new PrintStream(socket.getOutputStream()), profile);
              break;
            }
           
            //System.out.println("TelnetUI: failed to obtain login profile" );
           
            loginAttempts++;
           
            if( loginAttempts >= maxLoginAttempts ) {
              System.out.println("TelnetUI: rejecting connection from: " + addr + " as number of failed connections > max login attempts (" + maxLoginAttempts + ")");
              socket.close();
              break;
            }
          }
        }
      }
View Full Code Here

    }
  }
 
  public static class StartSocket {
    public StartSocket(String args[]) {
      Socket sck = null;
      PrintWriter pw = null;
      try {
        System.out.println("StartSocket: passing startup args to already-running process.");
       
    // NOTE - this formatting is also used by AzureusCoreSingleInstanceClient and other org.gudy.azureus2.ui.swt.StartSocket
       
        sck = new Socket("127.0.0.1",6880);
        pw = new PrintWriter(new OutputStreamWriter(sck.getOutputStream()));
        StringBuffer buffer = new StringBuffer(AzureusCoreSingleInstanceClient.ACCESS_STRING+";args;");
        for(int i = 0 ; i < args.length ; i++) {
          String arg = args[i].replaceAll("&","&&").replaceAll(";","&;");
          buffer.append(arg);
          buffer.append(';');
        }
        pw.println(buffer.toString());
        pw.flush();
      } catch(Exception e) {
        e.printStackTrace();
      } finally {
        try {
          if (pw != null)
            pw.close();
        } catch (Exception e) {
        }
        try {
          if (sck != null)
            sck.close();
        } catch (Exception e) {
        }
      }
    }
View Full Code Here

  void notifyConnected () {
    if (INFO) {
      SocketChannel socketChannel = tcp.socketChannel;
      if (socketChannel != null) {
        Socket socket = tcp.socketChannel.socket();
        if (socket != null) {
          InetSocketAddress remoteSocketAddress = (InetSocketAddress)socket.getRemoteSocketAddress();
          if (remoteSocketAddress != null) info("kryonet", this + " connected: " + remoteSocketAddress.getAddress());
        }
      }
    }
    Listener[] listeners = this.listeners;
View Full Code Here

   * Returns the IP address and port of the remote end of the TCP connection, or null if this connection is not connected.
   */
  public InetSocketAddress getRemoteAddressTCP () {
    SocketChannel socketChannel = tcp.socketChannel;
    if (socketChannel != null) {
      Socket socket = tcp.socketChannel.socket();
      if (socket != null) {
        return (InetSocketAddress)socket.getRemoteSocketAddress();
      }
    }
    return null;
  }
View Full Code Here

            public void
            run()
          {
                while ( true ){
                 
                  Socket socket      = null;
                  ObjectInputStream  ois  = null;
                 
                  try{
                    socket = server_socket.accept();
                   
                    String address = socket.getInetAddress().getHostAddress();
                   
                    if ( !( address.equals("localhost") || address.equals("127.0.0.1"))){
                     
                      socket.close();
                     
                      continue;
                    }
                   
                    ois = new ObjectInputStream( socket.getInputStream());
                   
                    ois.readInt()// version
                   
                    String  header = (String)ois.readObject();
                   
                    if ( !header.equals( getHeader())){
                     
                      log.messageLogged(
                          LoggerChannel.LT_ERROR,
                          "SingleInstanceHandler: invalid header - " + header );
                     
                      continue;
                    }

                    String[]  args = (String[])ois.readObject();
                   
                    handler.processArguments( args );
                   
                  }catch( Throwable e ){
                   
                    log.messageLogged( "SingleInstanceHandler: receive error", e );

                  }finally{
                   
                    if ( ois != null ){
                      try{
                        ois.close();
                       
                      }catch( Throwable e ){
                      }
                    }
                   
                    if ( socket != null ){
                      try{
                        socket.close();
                       
                      }catch( Throwable e ){
                      } 
                    }
                  }
View Full Code Here

  protected static void
  sendArguments(
    LoggerChannelListener  log,
    String[]        args )
  {
    Socket  socket = null;
 
    try{
      socket = new Socket( "127.0.0.1", port );
          
      ObjectOutputStream  oos = new ObjectOutputStream( socket.getOutputStream());
     
      oos.writeInt( 0 );
     
      oos.writeObject( getHeader());
     
      oos.writeObject( args );
     
      log.messageLogged( LoggerChannel.LT_INFORMATION, "SingleInstanceHandler: arguments passed to existing process" );
     
      }catch( Throwable e ){
       
        log.messageLogged( "SingleInstanceHandler: send error", e );

      }finally{
       
        if ( socket != null ){
          try{
            socket.close();
           
          }catch( Throwable e ){
          } 
        } 
      }
View Full Code Here

  public SelectionKey accept (Selector selector, SocketChannel socketChannel) throws IOException {
    try {
      this.socketChannel = socketChannel;
      socketChannel.configureBlocking(false);
      Socket socket = socketChannel.socket();
      socket.setTcpNoDelay(true);

      selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);

      if (DEBUG) {
        debug("kryonet", "Port " + socketChannel.socket().getLocalPort() + "/TCP connected to: "
View Full Code Here

    writeBuffer.clear();
    readBuffer.clear();
    readBuffer.flip();
    try {
      SocketChannel socketChannel = selector.provider().openSocketChannel();
      Socket socket = socketChannel.socket();
      socket.setTcpNoDelay(true);
      socket.setTrafficClass(IPTOS_LOWDELAY);
      socket.connect(remoteAddress, timeout); // Connect using blocking mode for simplicity.
      socketChannel.configureBlocking(false);
      this.socketChannel = socketChannel;

      selectionKey = socketChannel.register(selector, SelectionKey.OP_READ);
      selectionKey.attach(this);
View Full Code Here

TOP

Related Classes of java.net.Socket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.