java socket 握手,Java SSLSocket握手失败

I'm trying to find a way to establish a connection beetwen a Java client and a C server using SSL.

This is the java client:

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import javax.net.ssl.SSLSocket;

import javax.net.ssl.SSLSocketFactory;

public class Main {

public static void main(String[] args) throws IOException {

SSLSocketFactory sslsockfact =

(SSLSocketFactory) SSLSocketFactory.getDefault();

SSLSocket sslsocket = (SSLSocket) sslsockfact.createSocket(

args[0], Integer.parseInt(args[1]));

sslsocket.startHandshake();

System.in.read();

}

}

It's only a few functions to establish connection and perform handshake, but I'm getting this error:

Exception in thread "main" javax.net.ssl.SSLHandshakeException:

Received fatal alert: handshake_failure

at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)

at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(

SSLSocketImpl.java:1657)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(

SSLSocketImpl.java:932)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(

SSLSocketImpl.java:1096)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(

SSLSocketImpl.java:1123)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(

SSLSocketImpl.java:1107)

at posslu.Main.main(Main.java:22)

Java Result: 1

Unfortunately I don't have any sources of the server program - I only know the protocol. Is it even possible to connect java and C++ using ssl? AFAIK server is written using openssl.

Any help?

EDIT:

I'm connecting from Windows to Linux and server's using posix sockets.

解决方案

Yes, it's definitely possible to communicate via SSL between Java and C.

The Java client code may be failing because it doesn't recognise the server certificate being sent.

You need to make sure that the server's certificate is present in the Java client's trust store.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值