c 中服务器多次接受消息,总是分两部分接收来自ssl套接字的消息(c客户端、python3服务器)...

我用c#(windows)编写ssl客户机,用python3(linux)编写ssl服务器。我的问题是,我发这个(c#):byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.");

byte[] empty = Encoding.UTF8.GetBytes("Baa");

sslStream.Write(empty);

sslStream.Write(messsage);

sslStream.Flush();

我在python服务器上看到了这个:

^{pr2}$

我的消息被分成两部分,第一部分总是有1个字节,第二部分是其余部分。在

问题是只有当我尝试从客户机发送到服务器时。当我用openssl(openssl s_client -connect 172.22.22.1:10443和openssl s_server -accept 10443 -cert server.cert -key server.key)测试它时,它会打印正确的消息。在

我的ssl服务器:#!/usr/bin/python

import socket, ssl

HOST, PORT, CERT = '0.0.0.0', 10443, 'server.pem'

def handle(conn):

while True:

data = conn.recv()

if not data:

break

else:

print(data)

def main():

sock = socket.socket()

sock.bind((HOST, PORT))

sock.listen(5)

context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)

context.load_cert_chain(certfile=CERT)

while True:

conn = None

ssock, addr = sock.accept()

try:

conn = context.wrap_socket(ssock, server_side=True)

handle(conn)

except ssl.SSLError as e:

print(e)

finally:

if conn:

conn.close()

if __name__ == '__main__':

main()

以及我的ssl客户端:class Program

{

public static bool ValidateServerCertificate(

object sender,

X509Certificate certificate,

X509Chain chain,

SslPolicyErrors sslPolicyErrors)

{

if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)

return true;

Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

return false;

}

public static void RunClient(string machineName, string serverName)

{

TcpClient client = new TcpClient(machineName, 10443);

Console.WriteLine("Client connected.");

SslStream sslStream = new SslStream(

client.GetStream(),

false,

new RemoteCertificateValidationCallback(ValidateServerCertificate),

null

);

try

{

sslStream.AuthenticateAsClient(serverName, new X509CertificateCollection(), SslProtocols.Tls, true);

}

catch (AuthenticationException e)

{

Console.WriteLine("Exception: {0}", e.Message);

if (e.InnerException != null)

{

Console.WriteLine("Inner exception: {0}", e.InnerException.Message);

}

Console.WriteLine("Authentication failed - closing the connection.");

client.Close();

return;

}

byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.");

byte[] empty = Encoding.UTF8.GetBytes("Baa");

sslStream.Write(empty);

sslStream.Write(messsage);

sslStream.Flush();

client.Close();

Console.WriteLine("Client closed.");

}

public static int Main(string[] args)

{

string serverCertificateName = "test";

string machineName = "172.22.22.1";

RunClient(machineName, serverCertificateName);

return 0;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值