2016-04-11 1 views
0

次のクライアントSSLSocketFactoryコードを使用してテキストを送信していますが、最終的にはリッスンしているサーバーに.wavファイルを送信していますが、このコードでは2つのエラーが発生します。SSLSocketFactoryコンパイルエラー

この2行は私にエラーを与えている:これはSSLSocketFactoryは抽象的で、インスタンス化できないと言っている

SSLSocketFactory socketFactory = new SSLSocketFactory(ks); 

。このため

socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

エラーは、それがメソッドsetHostnameVerifierを解決できないと、シンボルALLOW_ALL_HOSTNAME_VERIFIERを解決することはできませんです。

全体としてコード:

Button send; 
    EditText textSend; 
    private String ip_address = "192.168.10.103"; 
    private int port = 5000; 
    private SSLSocket socket = null; 
    private BufferedWriter out = null; 
    private BufferedReader in = null; 
    private final String TAG = "TAG"; 
    private char keystorepass[] = "....".toCharArray(); 
    private char keypassword[] = "....".toCharArray(); 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_send_screen); 
     send = (Button) findViewById(R.id.send); 
     textSend = (EditText) findViewById(R.id.textsend); 

     send.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String send = textSend.getText().toString(); 
       if(send.isEmpty()){ 
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(sendScreen.this); 
        dialogBuilder.setMessage("Enter Text!"); 
        dialogBuilder.setTitle("No TEXT"); 
        dialogBuilder.setPositiveButton("OK...", null); 
        dialogBuilder.show(); 
       }else{ 
        Log.i(TAG,"makes it to here"); 
        try{ 

         KeyStore ks = KeyStore.getInstance("BKS"); 
         InputStream keyin = v.getResources().openRawResource(R.raw.androidKey); 
         ks.load(keyin,keystorepass); 
         SSLSocketFactory socketFactory = new SSLSocketFactory(ks); 
         socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

         socket = (SSLSocket) 
           socketFactory.createSocket(new Socket(ip_address,port), ip_address, port, false); 
         socket.startHandshake(); 

         printServerCertificate(socket); 
         printSocketInfo(socket); 

         out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 
         in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
         chat(send); 
        } catch (UnknownHostException e) { 
         Toast.makeText(v.getContext(), "Unknown host", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"Unknown host"); 
         //System.exit(1); 
        } catch (IOException e) { 
         Toast.makeText(v.getContext(), "No I/O", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"No I/O"); 
         e.printStackTrace(); 
         //System.exit(1); 
        } catch (KeyStoreException e) { 
         Toast.makeText(v.getContext(), "Keystore ks error", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"Keystore ks error"); 
         //System.exit(-1); 
        } catch (NoSuchAlgorithmException e) { 
         Toast.makeText(v.getContext(), "No such algorithm for ks.load", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"No such algorithm for ks.load"); 
         e.printStackTrace(); 
         //System.exit(-1); 
        } catch (CertificateException e) { 
         Toast.makeText(v.getContext(), "certificate missing", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"certificate missing"); 
         e.printStackTrace(); 
         //System.exit(-1); 
        } catch (UnrecoverableKeyException e) { 
         Toast.makeText(v.getContext(), "UnrecoverableKeyException", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"unrecoverableKeyException"); 
         e.printStackTrace(); 
         //System.exit(-1); 
        } catch (KeyManagementException e) { 
         Toast.makeText(v.getContext(), "KeyManagementException", Toast.LENGTH_SHORT).show(); 
         Log.i(TAG,"key management exception"); 
         e.printStackTrace(); 
         //System.exit(-1); 
        } 
       } 
      } 
     }); 


    } 
    private void printServerCertificate(SSLSocket socket) { 
     try { 
      Certificate[] serverCerts = 
        socket.getSession().getPeerCertificates(); 
      for (int i = 0; i < serverCerts.length; i++) { 
       Certificate myCert = serverCerts[i]; 
       Log.i(TAG,"====Certificate:" + (i+1) + "===="); 
       Log.i(TAG,"-Public Key-\n" + myCert.getPublicKey()); 
       Log.i(TAG,"-Certificate Type-\n " + myCert.getType()); 

       System.out.println(); 
      } 
     } catch (SSLPeerUnverifiedException e) { 
      Log.i(TAG,"Could not verify peer"); 
      e.printStackTrace(); 
      System.exit(-1); 
     } 
    } 
    private void printSocketInfo(SSLSocket s) { 
     Log.i(TAG,"Socket class: "+s.getClass()); 
     Log.i(TAG," Remote address = " 
       +s.getInetAddress().toString()); 
     Log.i(TAG," Remote port = "+s.getPort()); 
     Log.i(TAG," Local socket address = " 
       +s.getLocalSocketAddress().toString()); 
     Log.i(TAG," Local address = " 
       +s.getLocalAddress().toString()); 
     Log.i(TAG," Local port = "+s.getLocalPort()); 
     Log.i(TAG," Need client authentication = " 
       +s.getNeedClientAuth()); 
     SSLSession ss = s.getSession(); 
     Log.i(TAG," Cipher suite = "+ss.getCipherSuite()); 
     Log.i(TAG," Protocol = "+ss.getProtocol()); 
    } 

    public void chat(String temp){ 
     String message = temp; 
     String line = ""; 
     // send id of the device to match with the image 
     try { 
      out.write(message+"\n"); 
      out.flush(); 
     } catch (IOException e2) { 
      Log.i(TAG,"Read failed"); 
      System.exit(1); 
     } 
     // receive a ready command from the server 
//  try { 
//   line = in.readLine(); 
//   mResponse.setText("SERVER SAID: "+line); 
//   //Log.i(TAG,line); 
//  } catch (IOException e1) { 
//   Log.i(TAG,"Read failed"); 
//   System.exit(1); 
//  } 
    } 

私が固定されているが、私は、このエラー出力持っています上記の二つの問題:あなたはそののいずれかを使用する必要がありますので

FATAL EXCEPTION: main 


Process: com.example.admirmonteiro.testclient, PID: 13735 


android.os.NetworkOnMainThreadException 
                         `at` `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)` 
+0

これは '問題を送る 'ではありません。これらはコンパイルエラーです。 [tag:ssl-certificate]のいずれとも関係ありません。 – EJP

+0

'NetworkOnMainThreadException'あなたのコードをAsyncTaskの中に置くか、' Volley'、 'OkHttp'、' Retrofit'などいくつかのライブラリを使う必要があります... – BNK

+0

どうすればよいか分かりません... – Aboogie

答えて

0

SSLSocketFactoryは抽象的です子供。 SSLSocketFactory.getDefault()を使用して、デフォルトの実装を取得できます。

setHostnameVerifier()は、HttpsURLConnectionの方法ではなく、SSLSocketFactoryです。あなたはコードのその部分でそれを使うべきです。

あなたがHttpsURLConnectionを使用していない場合、あなたはこのようにURLを検証する必要がありますすることができます

// Open SSLSocket directly to gmail.com 
SocketFactory sf = SSLSocketFactory.getDefault(); 
SSLSocket socket = (SSLSocket) sf.createSocket("gmail.com", 443); 
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); 
SSLSession s = socket.getSession(); 

// Verify that the certicate hostname is for mail.google.com 
// This is due to lack of SNI support in the current SSLSocket. 
if (!hv.verify("mail.google.com", s)) { 
    throw new SSLHandshakeException("Expected mail.google.com, " 
            "found " + s.getPeerPrincipal()); 
} 

// At this point SSLSocket performed certificate verificaiton and 
// we have performed hostname verification, so it is safe to proceed. 

// ... use socket ... 
socket.close(); 

あなたはAndroidのドキュメントのいくつかの現実の世界の例を見ることができます:

http://developer.android.com/training/articles/security-ssl.html

0

あなたは」コンストラクタnew SSLSocketFactory(KeyStore)とメソッドSSSLSocketFactory.setHostnameVerifier()を作成したか、または間違ったSSLSocketFactoryをインポートしただけです。

+0

誤ったものや正しいものを輸入しましたか?私はこれをインポートした:import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; – Aboogie

+0

これらのインポートを使用する代わりに、私はimport org.apache.http.conn.ssl.SSLSocketFactoryを使用しました。しかし、私はテキストを送信しようとすると、エラーが発生します:W/System.err:java.net.SocketException:ソケットが失敗しました:EACCES(許可が拒否されました) – Aboogie

+0

あなたが意図したもの輸入する。あなただけがそれに答えることができます。インポートしたものには、そのコンストラクタまたはそのメソッドがありません。 – EJP

関連する問題