Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待\n而不是EOF C#SslStream_Java_C#_Ssl_Printwriter_Sslstream - Fatal编程技术网

Java 等待\n而不是EOF C#SslStream

Java 等待\n而不是EOF C#SslStream,java,c#,ssl,printwriter,sslstream,Java,C#,Ssl,Printwriter,Sslstream,我正在使用SslStream来接受客户。 这段代码从套接字读取2048个字节,并将它们作为字符串返回。当我尝试使用我用Java编写的客户机进行连接时,服务器没有响应,并且卡在这条线上: bytes=sslStream.Read(buffer,0,buffer.Length) 通过谷歌搜索,我发现服务器正在等待EOF完成消息。这非常烦人,因为我构建的协议需要在消息末尾等待\n 有没有办法等待我选择的角色完成消息? 我试图在客户端发送的消息末尾添加EOF,但没有成功。我通过PrintWriter发送

我正在使用SslStream来接受客户。 这段代码从套接字读取2048个字节,并将它们作为字符串返回。当我尝试使用我用Java编写的客户机进行连接时,服务器没有响应,并且卡在这条线上:

bytes=sslStream.Read(buffer,0,buffer.Length)


通过谷歌搜索,我发现服务器正在等待EOF完成消息。这非常烦人,因为我构建的协议需要在消息末尾等待
\n

有没有办法等待我选择的角色完成消息? 我试图在客户端发送的消息末尾添加EOF,但没有成功。我通过
PrintWriter
发送了消息。我用以下语句(Java)创建了它:

socket
SSLSocket
。 我在网上找到的答案有点混乱,并没有解决我的问题

服务器:

static string ReadMessage(SslStream sslStream) 
{
    // Read the  message sent by the client.
    // The client signals the end of the message using the
    // "<EOF>" marker.
    byte[] buffer = new byte[2048];
    StringBuilder messageData = new StringBuilder();
    int bytes = -1;
    do 
    {
        // Read the client's test message.
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
        decoder.GetChars(buffer, 0, bytes, chars, 0);
        messageData.Append(chars);

        // Check for EOF or an empty message.
        if (messageData.ToString().IndexOf("<EOF>") != -1)
        {
            break;
        }
    } while (bytes != 0);

    return messageData.ToString();
}
此外,我尝试了以下方法:

out.print(message + "\r\n\r");

通过谷歌搜索,我发现为了发送最后的信号,我需要使用
out.flush()

刷新套接字,您可以控制
do{}while()
,那么是什么阻止您在阅读
NL
后中断它呢?您所指的套接字是java客户端套接字,对吗?另外,您可能需要在上面的代码中处理一些事情。1.SslStream.Read可以返回0字节,请确保在这种情况下中断。2.我强烈建议使用异步版本的Read(ReadAsync),尤其是在客户端和服务器交互时。它确实是一个客户端套接字,我将检查异步函数。非常感谢。
out.print(message + (char)-1);
out.print(message + "\r\n\r");