gpt4 book ai didi

Java Server SSL Socket 和 Python Client SSL socket - 服务器发送消息时出现问题

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

我正在尝试与服务器 SSL 套接字 Java 和客户端 SSL 套接字 Python 进行通信。发送的第一条消息没问题,但是当服务器发送另一条消息时,客户端收到分成两部分的消息。例如:如果服务器发送消息“abcdefghij”,客户端首先收到“a”,然后收到“bcdefghij”。

有人知道为什么第一次收到消息后会分成两部分吗?问候。

客户端代码:

import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ssl_sock = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_NONE)
ssl_sock.connect(('localhost', 7000))
pprint.pprint(ssl_sock.getpeercert())

while(1):
print "Waiting"
data = ssl_sock.recv()
print "Received:", data
data = ""

ssl_sock.close()

服务器代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;


public class SslReverseEchoer {

public static void main(String[] args) {
char ksPass[] = "123456".toCharArray();
char ctPass[] = "123456".toCharArray();

try {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("keystore.jks"), ksPass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ctPass);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), null, null);
SSLServerSocketFactory ssf = sc.getServerSocketFactory();
SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(7000);
printServerSocketInfo(s);
SSLSocket c = (SSLSocket) s.accept();
printSocketInfo(c);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));
//1th time
String m = "abcdefghj1234567890";
w.write(m, 0, m.length());
w.newLine();
w.flush();
//2th time
String m2 = "#abcdefghj1234567890";
w.write(m2, 0, m2.length());
w.newLine();
w.flush();
//3th time
String m3 = "?abcdefghj1234567890";
w.write(m3, 0, m3.length());
w.newLine();
w.flush();
while ((m = r.readLine()) != null) {
if (m.equals("."))
break;
char[] a = m.toCharArray();
int n = a.length;
for (int i = 0; i < n / 2; i++) {
char t = a[i];
a[i] = a[n - 1 - i];
a[n - i - 1] = t;
}
w.write(a, 0, n);
w.newLine();
w.flush();
}
w.close();
r.close();
c.close();
s.close();
} catch (Exception e) {
System.err.println(e.toString());
}
}

private static void printSocketInfo(SSLSocket s) {
System.out.println("Socket class: " + s.getClass());
System.out.println(" Remote address = " + s.getInetAddress().toString());
System.out.println(" Remote port = " + s.getPort());
System.out.println(" Local socket address = " + s.getLocalSocketAddress().toString());
System.out.println(" Local address = " + s.getLocalAddress().toString());
System.out.println(" Local port = " + s.getLocalPort());
System.out.println(" Need client authentication = " + s.getNeedClientAuth());
SSLSession ss = s.getSession();
System.out.println(" Cipher suite = " + ss.getCipherSuite());
System.out.println(" Protocol = " + ss.getProtocol());
}

private static void printServerSocketInfo(SSLServerSocket s) {
System.out.println("Server socket class: " + s.getClass());
System.out.println(" Socker address = " + s.getInetAddress().toString());
System.out.println(" Socker port = " + s.getLocalPort());
System.out.println(" Need client authentication = " + s.getNeedClientAuth());
System.out.println(" Want client authentication = " + s.getWantClientAuth());
System.out.println(" Use client mode = " + s.getUseClientMode());
}
}

最佳答案

似乎您希望始终能够从另一侧读取您在一个 block 中发送的任何数量的数据。

这是一个常见错误,并非特定于 SSL/TLS,也与普通 TCP 通信有关。

您应该始终循环阅读您想要阅读的任何内容。您还应该定义您的协议(protocol)(或使用现有协议(protocol))以考虑命令和请求/响应终止符。

例如,HTTP 使用空行来告知 header 的结尾,并使用 Content-Length header 或分块传输编码来告知接收者何时停止阅读正文。

SMTP 使用行分隔命令和单个 . 作为消息的结尾。

关于Java Server SSL Socket 和 Python Client SSL socket - 服务器发送消息时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11787467/

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