Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:31:37+00:00 2026-06-14T19:31:37+00:00

I’m trying to write an android application that communicates with a java PC server

  • 0

I’m trying to write an android application that communicates with a java PC server via secure connection (SSL/TLS) i hav eread several tutorials and followed them and the following code works when used on PC to PC , but when i but the client code on an android (3.2) device it stalls on the SSL or TLS Handshake.
the following is the client communication code:

public class ClientCommunicationManager implements Runnable{
    /**
     * Application context
    */
    private Context context;
    /**
     * Host string
    */
    private String host;
    /**
    * port number
    */
    private int port;
      /**
       * Report Recieved Listeners vector
       */
      private Vector<ReportRecievedListener> rrListeners;
      /**
       * Connection to the client
       */
      private DataInputStream din;

      /**
       * Connection to the client
       */
      private DataOutputStream dout;

      /**
       * KeyStore for storing our public/private key pair
       */
      private KeyStore clientKeyStore;

      /**
       * KeyStore for storing the server's public key
       */
      private KeyStore serverKeyStore;

      /**
       * Used to generate a SocketFactory
       */
      private SSLContext sslContext;

      /**
       * A list of visible postings
       */
      private Set postings = new HashSet();

      /**
       * Passphrase for accessing our authentication keystore
       */
      static private final String passphrase = "a1n2d3r4o5i6d";

      /**
       * A source of secure random numbers
       */
      static private SecureRandom secureRandom;

      public ClientCommunicationManager( String host, int port,Context context ) {
          this.context = context;
          this.host = host;
          this.port = port;

          rrListeners = new Vector<ReportRecievedListener>();

          secureRandom = new SecureRandom();
          secureRandom.nextInt();      




      }

      public void registerRRListener(ReportRecievedListener listener){
          rrListeners.add(listener);
      }

      private void setupServerKeystore() throws GeneralSecurityException, IOException {
        serverKeyStore = KeyStore.getInstance( "BKS" );

        serverKeyStore.load( context.getResources().openRawResource(R.raw.serverbks), 
                            "public".toCharArray() );
      }

      private void setupClientKeyStore() throws GeneralSecurityException, IOException {
        clientKeyStore = KeyStore.getInstance( "BKS" );
        clientKeyStore.load( context.getResources().openRawResource(R.raw.clientbks),
                           passphrase.toCharArray() );
      }

      private void setupSSLContext() throws GeneralSecurityException, IOException {
        TrustManagerFactory tmf =       TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init( serverKeyStore );

        KeyManagerFactory kmf =     KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init( clientKeyStore, passphrase.toCharArray() );

        sslContext = SSLContext.getInstance( "SSL" );
        sslContext.init( kmf.getKeyManagers(),
                         tmf.getTrustManagers(),
                         secureRandom );
      }

      public void connect() {
         new Thread(new Runnable() {

            public void run() {
                try {
                    setupServerKeystore();
                    setupClientKeyStore();
                    setupSSLContext();
                } catch (GeneralSecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }




               SSLSocketFactory sf = sslContext.getSocketFactory();


               SSLSocket socket= null;
               InputStream in = null;
               OutputStream out = null;

            try {
                 socket = (SSLSocket)sf.createSocket( host, port );
                 socket.setUseClientMode(true); 

                 socket.startHandshake();

                 in = socket.getInputStream();
                 out = socket.getOutputStream();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



               din = new DataInputStream( in );
               dout = new DataOutputStream( out );

               if(socket!=null)
                  new Thread( this ).start();

            }
        }).start();

      }



      public void run() {
          boolean isConnected = true;
        try {
          while (isConnected) {
            String msg = din.readUTF();
            if(msg.equals("END"))
                isConnected = false;
            else
                fireReportRecievedEvent(new ReportRecievedEvent(msg));


          }
        } catch( IOException ie ) {
          ie.printStackTrace();
        }
      }

     private void fireReportRecievedEvent(ReportRecievedEvent event){
         for(ReportRecievedListener listener : rrListeners){
             listener.onReportRecieved(event);
         }
     }


}

following is the server code:

public class CommunicationManager implements Runnable{

  private int port = Constants.SERVER_PORT;
  private HashSet<ConnectionProcessor> connections = new     HashSet<ConnectionProcessor>();
  private KeyStore clientKeyStore;
  private KeyStore serverKeyStore;
  private SSLContext sslContext;
  static private SecureRandom secureRandom;
  private boolean running;
  private SSLServerSocket ss=null;

  private static CommunicationManager instance=null;



private CommunicationManager(){

}

public static CommunicationManager getInstance(){
    if(instance == null){
        instance = new CommunicationManager();
    }

    return instance;
}


public int getSecureRandom(){
    SecureRandom securerandom = new SecureRandom();
    return securerandom.nextInt();

}



public boolean isRunning() {
    return running;
}



public void setRunning(boolean running) {
    this.running = running;
}

public void startServer(){
    if(!isRunning()){
        setRunning(true);
        new Thread(this).start();
    }

}

public void stopServer(){
    setRunning(false);
    for(ConnectionProcessor cp : connections){
        cp.close();
    }

    try {
        if(ss!=null)
            ss.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}



private void setupClientKeyStore() throws GeneralSecurityException, IOException {
    clientKeyStore = KeyStore.getInstance( "JKS" );
    clientKeyStore.load( new FileInputStream( "resources/client.public" ),
                       "public".toCharArray() );
  }

  private void setupServerKeystore() throws GeneralSecurityException, IOException {
    serverKeyStore = KeyStore.getInstance( "JKS" );
    serverKeyStore.load( new FileInputStream( "resources/server.private" ),
                        Constants.SERVER_KEYSTORE_PWD.toCharArray() );
  }

  private void setupSSLContext() throws GeneralSecurityException, IOException {
    TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
    tmf.init( clientKeyStore );

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm() );
    kmf.init( serverKeyStore, Constants.SERVER_KEYSTORE_PWD.toCharArray() );

    sslContext = SSLContext.getInstance( "TLS" );
    sslContext.init( kmf.getKeyManagers(),
                     tmf.getTrustManagers(),
                     secureRandom );
  }



  @Override
  public void run() {


      try {
          setupClientKeyStore();
          setupServerKeystore();  
          setupSSLContext();
    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
    }

      SSLServerSocketFactory sf = sslContext.getServerSocketFactory();

      try {
        ss = (SSLServerSocket)sf.createServerSocket( port );
    } catch (IOException e) {

        e.printStackTrace();
    }

      // Require client authorization
      ss.setNeedClientAuth( true );


      System.out.println( "Listening on port "+port+"..." );
      while (isRunning()) {
        Socket socket=null;
        try {
            socket = ss.accept();
        } catch (IOException e) {

            if(e.getClass() == SocketException.class){
                System.out.print("accept() threw SocketException because of close\n");
            }
        }
        if(socket!=null && socket.isConnected()){
            System.out.println( "Got connection from "+socket );

            ConnectionProcessor cp = new ConnectionProcessor( this, socket );
            connections.add( cp );
        }

      }






     closeServer();
  }

  private void closeServer() {
    for(ConnectionProcessor cp : connections){
        cp.close();
    }

    try {
        ss.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

/**
   * Remove a connection that has been closed from our set
   * of open connections
   */
  public void removeConnection( ConnectionProcessor cp ) {
    connections.remove( cp );
  }

  /**
   * Return an iteration over open connections
   */
  public Iterator getConnections() {
    return connections.iterator();
  }
  /**
   * Broadcast a report over all connections
   */
  public void broadcastReport(String report){
      for(ConnectionProcessor cp : connections){
          cp.send(report);
      }
  }
  /**
   * Broadcast a report over all connections using a new thread
   */

public void broadcastOnThread(final String report) {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            if(isRunning())
                broadcastReport(report);    

        }
    }) ;

    t.start();
}

 /**
   * recieve a new report to be broadcasted
   */
public void acceptReport(String report_string) {
    broadcastOnThread(report_string);

}





}

Any help or insight would be very much appreciated. thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:31:38+00:00Added an answer on June 14, 2026 at 7:31 pm

    The problem was no data was being exchanged (yet) so the handshake never finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.