Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4162466

deadlock if create ObjectInputStream before ObjectOutputStream using socket/pipe

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • core-libs



      Name: clC74495 Date: 07/31/98


      In the following source code, there are a "new ObjectOutputStream"
      and a "new ObjectInputStream" statement. If you change their order,
      the program will block!

      (Further instructions after this code sample.)

      import java.io.*;
      import java.math.*;
      import java.net.*;
      import java.security.*;
      import java.security.interfaces.*;

      import ubilab.krypto.Protocol2withDSA;


      public
      class TestKeyExchangeServer
      {
      public
      static
      void
      main( String[] args )
      {
      InputStream inputStream;
      OutputStream outputStream;
      ObjectInputStream oInputStream;
      ObjectOutputStream oOutputStream;

      int code;
      int port = 1500;
      ServerSocket serverSocket;
      Socket connection;

      KeyPairGenerator keyGen;
      KeyPair keyPair;
      DSAPrivateKey privateKey;
      DSAPublicKey publicKey;

      byte[] id;

      Protocol2withDSA keyExchange;


      try
      {
      keyGen = KeyPairGenerator.getInstance( "DSA" );
      keyGen.initialize( 1024, new SecureRandom() );

      keyPair = keyGen.generateKeyPair();

      privateKey = (DSAPrivateKey)keyPair.getPrivate();
      publicKey = (DSAPublicKey)keyPair.getPublic();
      System.out.println( "Keys generated" );
      id = ( publicKey.getY() ).toByteArray();

      serverSocket = new ServerSocket( port );
      connection = serverSocket.accept();
      System.out.println( "Connection established" );

      inputStream = connection.getInputStream();
      outputStream = connection.getOutputStream();
      System.out.println( "Streams established" );

      oOutputStream = new ObjectOutputStream( outputStream );
      System.out.println( "Object output stream established" );
      oInputStream = new ObjectInputStream( inputStream );
      System.out.println( "Object input stream established" );

      // in practice, a CA already has the public key
      oOutputStream.writeObject( publicKey );
      oOutputStream.flush();
      System.out.println( "Public key written" );

      // in practice, you get the public key from a CA
      publicKey = (DSAPublicKey)oInputStream.readObject();
      System.out.println( "Public key read" );

      keyExchange = new Protocol2withDSA( false, inputStream,
      outputStream, privateKey, publicKey,
      id, ( publicKey.getY() ).toByteArray() );
      System.out.println( "KeyExchange instance created" );
      keyExchange.performProtocol();

      System.out.println( new BigInteger(
      keyExchange.toByteArray() ) );


      connection.close();
      }
      catch ( Exception e )
      {
      System.err.println( e );
      }
      }
      }

      -----

      Put all the files (attachments) that don't start with "Test" in a folder called
      krypto within a folder called ubilab, and compile them. Next, follow these
      steps:

      1. java TestKeyExchangeServer &
      2. java TestKeyExchangeClient

      Then it doesn't continue after the connection provided the input and the
      output stream.

      3. Change the instantiation order in both ther server and the client to
      oOutputStream = new ObjectOutputStream( outputStream );
      oInputStream = new ObjectInputStream( inputStream );
      4. Suddenly it works - at least that part :)

      (Review ID: 36112)
      ======================================================================

            jfialli Joe Fialli
            clucasius Carlos Lucasius (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: