Examples of Socket


Examples of java.net.Socket

   * 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

Examples of java.net.Socket

            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

Examples of java.net.Socket

  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

Examples of java.net.Socket

  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

Examples of java.net.Socket

    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

Examples of java.net.Socket

            final ServerSocket accept = serverSocket;
            Thread thread = new Thread() {
                public void run() {
                    try {
                        System.out.println("server accepting");
                        Socket s = accept.accept();
                        Thread.sleep(100);
                        System.out.println("server accepted:" + s);
                        System.out.println("server read:" + s.getInputStream().read());
                        Thread.sleep(200);
                        s.getOutputStream().write(234);
                        Thread.sleep(100);
                        System.out.println("server closing");
                        s.close();
                        System.out.println("server done");
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            };
            thread.start();
            Thread.sleep(1000);
            Socket socket = new Socket();
            socket.setSoTimeout(2000);
            InetSocketAddress socketAddress = new InetSocketAddress(address, port);
            System.out.println("client:" + socketAddress);
            try {
                socket.connect(socketAddress, 2000);
                Thread.sleep(200);
                System.out.println("client:" + socket.toString());
                socket.getOutputStream().write(123);
                Thread.sleep(100);
                System.out.println("client read:" + socket.getInputStream().read());
                socket.close();
            } catch (Throwable t) {
                t.printStackTrace();
            }
            thread.join(5000);
            if (thread.isAlive()) {
View Full Code Here

Examples of java.net.Socket

    }

    public void listen() {
        try {
            while (serverSocket != null) {
                Socket s = serverSocket.accept();
                boolean stop;
                synchronized (this) {
                    openConnectionCount++;
                    stop = openConnectionCount > maxConnectionCount;
                }
View Full Code Here

Examples of java.net.Socket

    public boolean isRunning(boolean traceError) {
        if (serverSocket == null) {
            return false;
        }
        try {
            Socket s = NetUtils.createLoopbackSocket(port, false);
            s.close();
            return true;
        } catch (IOException e) {
            if (traceError) {
                traceError(e);
            }
View Full Code Here

Examples of java.net.Socket

    return createSocket(null, address.getHostName(), port, true);
  }
 
  public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException,UnknownHostException
  {
    Socket tunnel;
    try
    {
      tunnel = this.info.getConnection(host, port).getSocket();
    }
    catch (IllegalStateException e)
View Full Code Here

Examples of java.net.Socket

                portServer = Integer.parseInt(args[++i]);
            }
        }
        ServerSocket listener = new ServerSocket(portClient);
        while (true) {
            Socket client = listener.accept();
            Socket server = new Socket("localhost", portServer);
            TcpRedirectThread c = new TcpRedirectThread(client, server, true);
            TcpRedirectThread s = new TcpRedirectThread(server, client, false);
            new Thread(c).start();
            new Thread(s).start();
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.