Package java.net

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


    }

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

    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

    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

                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

    }

    public void run() {
        try {
            synchronized (this) {
                Socket s = serverSocket.accept();
                if (s.getInetAddress().equals(address)) {
                    server.trace("Data connected:" + s.getInetAddress() + " expected:" + address);
                    socket = s;
                    notifyAll();
                } else {
                    server.trace("Data REJECTED:" + s.getInetAddress() + " expected:" + address);
                    close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
View Full Code Here

        }
    }

    private void connect() throws IOException {
        if (active) {
            socket = new Socket(address, port);
        } else {
            waitUntilConnected();
        }
    }
View Full Code Here

      super(name);
      this.tcpServer = tcpServer;
    }

    public final void run() {
      Socket socket;
      try {
        loop:
          while (running) {
            canStop = true;
            try {
              ServerSocket listen = tcpServer.getListen();
              if (listen != null) {
                socket = listen.accept();
                canStop = false;
              } else {
                break loop;
              }
            } catch (IOException exc) {
              if (running) {
                Trace.logger.log(
                                 BasicLevel.ERROR,
                                 this.getName() +
                                 ", error during accept", exc);
                try {
                  Thread.sleep(1000);
                } catch (InterruptedException ie) {}
                continue loop;
              }
              break loop;
            }

            if (! running) break loop;

            if (Trace.logger.isLoggable(BasicLevel.DEBUG)) {
              Trace.logger.log(
                               BasicLevel.DEBUG,
                               this.getName() + ", connection from " +
                               socket.getInetAddress() + ':' +
                               socket.getPort());
            }

            try {
              IOControl ioCtrl = new IOControl(socket);
              int rid = ioCtrl.readInt();
View Full Code Here

      ServerAddress sa = (ServerAddress)addresses.elementAt(0);
      if (Trace.logger.isLoggable(BasicLevel.DEBUG))
        Trace.logger.log(BasicLevel.DEBUG,
                         " -> try connection " + sa);
      try {
        Socket socket = new Socket(sa.hostName, sa.port);
        ioCtrl = new IOControl(socket);
        return;
      } catch (IOException exc) {
        if (Trace.logger.isLoggable(BasicLevel.WARN))
          Trace.logger.log(BasicLevel.WARN, "NamingConnection.open()", exc);
View Full Code Here

    if (Trace.logger.isLoggable(BasicLevel.DEBUG))
      Trace.logger.log(BasicLevel.DEBUG,
                       "SimpleNamingConnection.open()");
    try {
      InetAddress addr = InetAddress.getByName(hostName);
      Socket socket = socketFactory.createSocket(addr, port, connectTimeout);
      ioCtrl = new IOControl(socket);
    } catch (IOException exc) {
      if (Trace.logger.isLoggable(BasicLevel.DEBUG))
        Trace.logger.log(BasicLevel.DEBUG,
                         "SimpleNamingConnection.open()", exc);
View Full Code Here

TOP

Related Classes of java.net.Socket

Copyright © 2015 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.