简体   繁体   English

C#SSL客户端消息未在服务器上打印

[英]C# SSL Client Message Not Printing on Server

I am writing a C# client that sends a Protobuf-net encrypted message to a server. 我正在编写一个将Protobuf-net加密消息发送到服务器的C#客户端。 Part of the client code reads as follows: 客户端代码的部分内容如下:

public static void RunClient() {
    TcpClient client = new TcpClient("168.72.70.62", 8443);
    Console.WriteLine("Client connected.");
    var stream = new MemoryStream();
    Serializer.Serialize<String>(stream, "Our client sends.");
    byte[] messsage = stream.ToArray();
    SslStream sslStream = new SslStream(client.GetStream(), false, 
            new RemoteCertificateValidationCallback(ValidateServerCertificate), 
            null);
    try {  // Server name must match name on server certificate
        sslStream.AuthenticateAsClient("168.72.70.62");
        sslStream.Write(messsage);
        sslStream.Flush();
        Console.WriteLine("Message sent.");
    } catch (AuthenticationException e) {
    ....
    }
    client.Close();
    Console.WriteLine("Client closed.");
}

This code is based on what I saw here: http://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx 该代码基于我在这里看到的内容: http : //msdn.microsoft.com/zh-cn/library/system.net.security.sslstream(v=vs.110).aspx

The server is designed to register that a message event occurs and print out the message. 该服务器旨在注册发生消息事件并打印出消息。 It works for other types of clients. 它适用于其他类型的客户。 Both the server and the other clients are in Java and are based on Netty's SecureChatServer: https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler.java . 服务器和其他客户端均使用Java并基于Netty的SecureChatServer: https : //github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler .java For this client, the server registers that a message is sent but does not print out the message. 对于此客户端,服务器注册发送了一条消息,但未打印出该消息。

How can I get the server to print out the message? 如何获得服务器以打印出消息?

EDIT: the actual solution 编辑:实际的解决方案

Netty expects a final \\n character to be appended to the string. Netty希望最后一个\\n字符附加到字符串中。 Using Write() does not append that final character, thus the string never flushes on the server. 使用Write()不会追加最后一个字符,因此该字符串永远不会在服务器上刷新。 Pay attention: Environment.NewLine is platform dependent and may append \\r\\n if the message is sent from Windows clients. 请注意: Environment.NewLine依赖于平台,并且如果消息是从Windows客户端发送的,则可能会附加\\r\\n Netty, instead, honors a communication protocol which requires a Linux compliant newline. 相反,Netty遵循需要Linux兼容换行符的通信协议。 To comply to Netty's protocol append a literal \\n manually to the string before serializing it! 为了遵守Netty的协议,在序列化之前,请在字符串后手动添加文字\\n


Are you sure you correctly validate the server? 您确定正确验证服务器吗?

new RemoteCertificateValidationCallback(ValidateServerCertificate)

Try to remove this line. 尝试删除此行。 You will have the client to connect even on non-certified servers, but the rest should remain intact. 即使没有经过认证的服务器上,您也可以让客户端进行连接,但其余部分应保持不变。

If the server now receives the message correctly, then it is the client that cannot validate the server correctly. 如果服务器现在正确接收到消息,则是客户端无法正确验证服务器。

This may depend on many defects. 这可能取决于许多缺陷。 Often it is the CA or the certificate. 通常是CA或证书。 You can attach a custom validation handler if the server fails to be certificated. 如果服务器未通过认证,则可以附加自定义验证处理程序。 Use that and see what error you get. 使用它,看看会遇到什么错误。

Pointer: validation callback 指针: 验证回调

BTW: are you sure you have to certify the server? 顺便说一句:您确定必须认证服务器吗? You can have encryption without certifying the client or server. 您可以进行加密而无需认证客户端或服务器。 Those are all different security features of SSL and not always do you need all of them. 这些都是SSL的所有不同安全功能,并非总是需要所有这些功能。

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

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