简体   繁体   English

iPhone App和Java ServerSocket之间的SSL连接

[英]SSL Connection between IPhone App and Java ServerSocket

I'm trying to establish a SSL Connection between an IPhone App and an Java SSLServerSocket. 我正在尝试在iPhone应用程序和Java SSLServerSocket之间建立SSL连接。 My Java Server looks like that: 我的Java Server看起来像这样:

SSLServerSocketFactory ssf = null;
    try {
        // set up key manager to do server authentication
        SSLContext ctx;
        KeyManagerFactory kmf;
        KeyStore ks;
        char[] passphrase = "passphrase".toCharArray();

        ctx = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance("SunX509");
        ks = KeyStore.getInstance("JKS");

        ks.load(new FileInputStream("fsKeystore"), passphrase);
        kmf.init(ks, passphrase);
        ctx.init(kmf.getKeyManagers(), null, null);

        ssf = ctx.getServerSocketFactory();

        System.out.println("Waiting for Connection");
        SSLServerSocket sslsocketServer = (SSLServerSocket) ssf.createServerSocket(9999);
        SSLSocket sslsocket = (SSLSocket) sslsocketServer.accept();
        sslsocket.addHandshakeCompletedListener(new HandshakeCompletedListener() {

            @Override
            public void handshakeCompleted(HandshakeCompletedEvent arg0) {
                System.out.println("Handshake finished");
            }
        });
        InputStream inputstream = sslsocket.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader;
        bufferedreader = new BufferedReader(inputstreamreader);

        String string = "";
        while ((string = bufferedreader.readLine()) != null) {
            string = bufferedreader.readLine();
            System.out.println(System.currentTimeMillis());
            System.out.println(string);
            System.out.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

In Objective-c I have implemented this solution: 在Objective-c中,我实现了以下解决方案:

-(id) initWithUrl: (NSString*) host onPort: (NSInteger) port withDelegate:(id<TCPDelegate>) delegate{
self = [super init];
if(self){

    //initTimer
    sendBuffer=[[NSMutableArray alloc]init];
    NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addTimer:timer forMode:NSDefaultRunLoopMode];

    del=delegate;
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL,(__bridge CFStringRef)host, port,&readStream,&writeStream);

    iStream = (__bridge_transfer NSInputStream *)readStream;
    oStream = (__bridge_transfer NSOutputStream *)writeStream;

    [iStream setDelegate:self];
    [oStream setDelegate:self];

    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];


    [iStream open];
    [oStream open];

    [iStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                   forKey:NSStreamSocketSecurityLevelKey];
    [oStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                    forKey:NSStreamSocketSecurityLevelKey];
     line = @"";
}
return self;
}

in the Iphone App I get 在Iphone App中,我得到了

CFNetwork SSLHandshake failed (-9807) CFNetwork SSLHandshake失败(-9807)

Any Idea what the problem might be? 任何想法可能是什么问题?

You may want to investigate using NSURLConnection. 您可能要使用NSURLConnection进行调查。 It has a delegate that you can use for authentication challenges. 它具有可用于身份验证质询的委托。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW3 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM