gpt4 book ai didi

Java SSL 连接被拒绝

转载 作者:行者123 更新时间:2023-12-02 03:54:06 24 4
gpt4 key购买 nike

我在连接 ssl 套接字代码时遇到问题,其中 java 客户端只是向正在监听的 python 服务器发送消息。当我作为服务器和客户端的本地主机运行代码时,它工作得很好。如果我在嵌入式板上使用 python 服务器以便 java 客户端可以向其发送数据,则 java 会拒绝连接。我使用嵌入式板的 IP 创建了自分配认证

openssl req -x509 -nodes -days 730 -newkey rsa:2048 -keyout privKey.key -out server.pem -subj /CN=XXX.XX.XX.XXX

我使用以下方法创建了客户端认证:

keytool -keystore clientCert -importcert -alias tS -file client.pem

但是还是不行。

我不认为我的代码有问题,因为当我尝试使用与本地主机相同的服务器名称(主机)代码时,连接发生得很好。

这是 Python 服务器代码:

import time , glob, os, sys

def deal_with_client(connstream):
timestr = time.strftime("%Y-%m-%d_%H-%M-%S")
outfile = open((timestr) + ".wav", 'ab')
data = connstream.recv(1024)
print data
sys.exit(1)
outfile.write(data )
# null data means the client is finished with us
while data:
if not data:
print 'no more data being sent'
break
data = connstream.recv(1024)
outfile.write(data)
# finished with client
def get_IP_Edison(value=basestring):
IP = os.popen(value).read()
tag1 , tag2= IP.find(":") , IP.find("n")
return IP[tag1+1:tag2]

def start_Server():
import ssl,socket

context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) # calling the client context to loaded with
# configs
key , cert = "/home/root/Coding/certificates/server/privKey.key", \
"/home/root/Coding/certificates/server/server.pem" # Key and cert created with OPENSSL
context.load_cert_chain(certfile=cert, keyfile=key) # Load the certifications
value = '''ifconfig | grep "inet " | grep -v 127.0.0.1 | grep -v 192.* | awk '{print $2}' '''
HOST, PORT = get_IP_Edison(value), 50007 # calling the port and host / needs to be of the edison
print "IP of the Edison : " + HOST
bindsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a normal socket
bindsocket.bind((HOST, PORT)) # Bind the socket or create it
bindsocket.listen(5) # make the socket listen on five connections
print 'Listening..'
while True:
newsocket, fromaddr = bindsocket.accept() # accept the connection
print 'accepted connection from' + str(fromaddr)
connstream = context.wrap_socket(newsocket, server_side=True,do_handshake_on_connect=True) # wrap it in socket but make sure keys match
try:
deal_with_client(connstream) # call the receive function to receive file (more details above )
finally:
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()
print 'socket was closed'

def wavFinder():
newest = max(glob.iglob('*.[Ww][Aa][Vv]'), key=os.path.getctime)
return newest


if __name__=='__main__':
start_Server()
wavFileNew = wavFinder()
print wavFileNew

这是我的java代码:

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;

/**
* Created by admirmonteiro on 2/18/16.
*/
public class testCertEdison {
/** TrustStores which is for the client use**/


final String theServerName = "XXX.XX.XX.XXX";
final int theServerPort = 50007;
// final static String pathToStores = "../../Desktop/SSL_KEY/x509_Java/keyStore/ex/";
final static String pathToStores = "/Users/admirmonteiro/Desktop/SSL_KEY/x509_Java/client/tS";
final static String trustStoreFile = "clientCert" ; // filename
final static String passwd = "admir2006";

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

String trustFileName = pathToStores + "/" + trustStoreFile;
System.setProperty("javax.net.ssl.trustStore",trustFileName);
System.setProperty("javax.net.ssl.trustStorePassword",passwd);

new test_certificate().doClient();

}

void doClient() throws Exception{
SSLSocketFactory sslsf = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(theServerName,theServerPort);

OutputStream sslOS = sslSocket.getOutputStream();
sslOS.write("Connected to the stupid thing".getBytes());
sslOS.flush();
sslSocket.close();

}
}

请帮忙,我已经从事这个工作有一段时间了。

最佳答案

“连接被拒绝”意味着您尝试连接的 IP:端口没有任何监听。

它与信任库、 keystore 、证书、SSL、Python、Java 或 OpenSSL 无关。

关于Java SSL 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639384/

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