gpt4 book ai didi

java - IPhone App 与 Java ServerSocket 之间的 SSL 连接

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:48 26 4
gpt4 key购买 nike

我正在尝试在 iPhone 应用程序和 Java SSLServerSocket 之间建立 SSL 连接。我的 Java 服务器看起来像这样:

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();
}

在 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;
}

在我得到的 Iphone 应用程序中

CFNetwork SSLHandshake failed (-9807)

知道问题出在哪里吗?

最佳答案

您可能想使用 NSURLConnection 进行调查。它有一个可用于身份验证挑战的委托(delegate)。

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

关于java - IPhone App 与 Java ServerSocket 之间的 SSL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com