android socket eofexception,尝试从SSLSocket读取时获取EOFException

我正在开发一个SSL客户端,它将向SSL服务器发出一个简单的请求并等待响应。 SSL握手和写入正常但我无法从套接字读取数据。我打开了java.net.ssl的调试并获得了以下内容:

[..]

main, READ: TLSv1 Change Cipher Spec, length = 1

[Raw read]: length = 5 0000: 16 03 01 00 20 ....

[Raw read]: length = 32 [..]

main, READ: TLSv1 Handshake, length = 32 Padded plaintext after DECRYPTION: len = 32

[..]

*** Finished verify_data: { 29, 1, 139, 226, 25, 1, 96, 254, 176, 51, 206, 35 }

*** %% Didn't cache non-resumable client session: [Session-1, SSL_RSA_WITH_RC4_128_MD5] [read] MD5 and SHA1 hashes: len = 16 0000: 14 00 00 0C 1D 01 8B E2 19 01 60 FE B0 33 CE 23 ..........`..3.# Padded plaintext before ENCRYPTION: len = 70 [..] a.j.y.

main, WRITE: TLSv1 Application Data, length = 70

[Raw write]: length = 75

[..]

Padded plaintext before ENCRYPTION: len = 70

[..]

main, WRITE: TLSv1 Application Data, length = 70

[Raw write]: length = 75

[..]

main, received EOFException: ignored main, called closeInternal(false) main, SEND TLSv1 ALERT: warning, description = close_notify Padded plaintext before ENCRYPTION: len = 18 [..]

ain, WRITE: TLSv1 Alert, length = 18 [Raw write]: length = 23

[..] main, called close()

main, called closeInternal(true)

main, called close()

main, called closeInternal(true)

[..]是证书链。

这是一段代码:

try {

System.setProperty("javax.net.debug","all");

/*

* Set up a key manager for client authentication

* if asked by the server. Use the implementation's

* default TrustStore and secureRandom routines.

*/

SSLSocketFactory factory = null;

try {

SSLContext ctx;

KeyManagerFactory kmf;

KeyStore ks;

char[] passphrase = "importkey".toCharArray();

ctx = SSLContext.getInstance("TLS");

kmf = KeyManagerFactory.getInstance("SunX509");

ks = KeyStore.getInstance("JKS");

ks.load(new FileInputStream("keystore.jks"), passphrase);

kmf.init(ks, passphrase);

ctx.init(kmf.getKeyManagers(), null, null);

factory = ctx.getSocketFactory();

} catch (Exception e) {

throw new IOException(e.getMessage());

}

SSLSocket socket = (SSLSocket)factory.createSocket("server ip", 9999);

/*

* send http request

*

* See SSLSocketClient.java for more information about why

* there is a forced handshake here when using PrintWriters.

*/

SSLSession session = socket.getSession();

[build query]

byte[] buff = query.toWire();

out.write(buff);

out.flush();

InputStream input = socket.getInputStream();

int readBytes = -1;

int randomLength = 1024;

byte[] buffer = new byte[randomLength];

while((readBytes = input.read(buffer, 0, randomLength)) != -1) {

LOG.debug("Read: " + new String(buffer));

}

input.close();

socket.close();

} catch (Exception e) {

e.printStackTrace();

}

我可以写多次,我没有得到任何错误,但EOFException发生在第一次读取。

我是在使用套接字还是使用SSL身份验证做错了?

谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值