Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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
C# 为什么此TLS代码仅在调试模式下运行?_C#_Sockets_Exception Handling_Tcpclient_Ssl - Fatal编程技术网

C# 为什么此TLS代码仅在调试模式下运行?

C# 为什么此TLS代码仅在调试模式下运行?,c#,sockets,exception-handling,tcpclient,ssl,C#,Sockets,Exception Handling,Tcpclient,Ssl,我得到一个奇怪的错误: 由于意外的数据包格式,握手失败 如果我直接运行代码而不进行调试 如果我设置一个断点,然后逐行调试代码,它就可以正常工作 这里是详细的例外 The handshake failed due to an unexpected packet format. at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncReq

我得到一个奇怪的错误:

由于意外的数据包格式,握手失败

如果我直接运行代码而不进行调试

如果我设置一个断点,然后逐行调试代码,它就可以正常工作

这里是详细的例外

The handshake failed due to an unexpected packet format.

   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost)
   at Searock_IM.Facebook.LoginForm.StartTls() in D:\Projects\Searock IM\Facebook\LoginForm.cs:line 456
   at Searock_IM.Facebook.LoginForm.button2_Click(Object sender, EventArgs e) in D:\Projects\Searock IM\Facebook\LoginForm.cs:line 137
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Searock_IM.Program.Main() in D:\Projects\Searock IM\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
代码如下:

class Connection : IDisposable
{
    private readonly Socket _socket;
    private NetworkStream _networkStream;
    private SslStream _sslStream;

    private int _bufferStartIndex;
    private readonly byte[] _buffer = new byte[0x8000];
    private StringBuilder _recievedText = new StringBuilder();

        public StringBuilder Debug { get; private set; }

    public bool UseSecure { get; set;  }

    public void StartTls()
    {
        UseSecure = true;

        if (_networkStream == null)
            _networkStream = new NetworkStream( _socket );

        if (_sslStream != null) 
            return;

        _sslStream = new SslStream(_networkStream);
        _sslStream.AuthenticateAsClient("chat.facebook.com");

        lock (Debug)
            Debug.AppendFormat( "** Started TLS **<br/>" );
    }

    public Connection()
    {
        Debug = new StringBuilder();

        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        _socket.Connect("chat.facebook.com", 5222);
    }

    public void Dispose()
    {
        if (_sslStream != null)
            _sslStream.Dispose();

        if (_networkStream != null)
            _networkStream.Dispose();
    }

    public void Send(string text)
    {
        if (String.IsNullOrEmpty( text ))
            return;

        lock (Debug)
            Debug.AppendFormat( "Out: {0} <br/>", HttpUtility.HtmlEncode(text) );

        byte[] outp = Encoding.UTF8.GetBytes(text);

        if (UseSecure)
            _sslStream.Write(outp);
        else
            _socket.Send(outp);
    }

    public string Recieve()
    {
        if (_socket.Available == 0)
            return null;

        int bytesRead = 0;
        if (UseSecure)
            bytesRead = _sslStream.Read(_buffer, _bufferStartIndex, _buffer.Length - _bufferStartIndex);
        else
            bytesRead = _socket.Receive(_buffer, _bufferStartIndex, _buffer.Length - _bufferStartIndex, SocketFlags.None);

        ReadBytes( bytesRead );

        var incomming = ClearStringBuffer();

        lock (Debug)
            Debug.AppendFormat( "In: {0}<br/> ", HttpUtility.HtmlEncode( incomming ));

        return incomming;
    }

    private void ReadBytes( int bytesRead )
    {
        // http://en.wikipedia.org/wiki/UTF-8#Design
        // Top 2 bits are either;
        //   00xx xxxx => 6 bit ASCII mapped character
        //   11xx xxxx => multi byte chatacter Start
        //   10xx xxxx => multi byte chatacter Middle of definition
        // So while last character in buffer is 'middle of definition' rewind end buffer pointer
        int endOfBuffer = bytesRead + _bufferStartIndex;
        if (endOfBuffer == 0)
            return;

        int end = endOfBuffer;
        while ((_buffer[end - 1] & 0xC0) == 0x80) --end;

        string part = Encoding.UTF8.GetString( _buffer, 0, end ).TrimEnd( '\0' );

        if (end != endOfBuffer)
        {
            _bufferStartIndex = endOfBuffer - end;
            for (int i = 0; i < _bufferStartIndex; i++)
                _buffer[i] = _buffer[i + end];
        }

        lock (_recievedText)
            _recievedText.Append( part );
    }


    private string ClearStringBuffer()
    {
        string result;

        lock (_recievedText)
        {
            result = _recievedText.ToString();
            _recievedText = new StringBuilder();
        }

        return result;
    }
}
我正在从windows窗体调用该类,并且在此窗体中未使用任何线程

有人能给我指出一个正确的方向吗?谢谢。

是否可能不需要安全连接?看

如果您将telnet发送到该地址并发送一些虚拟数据,您将得到一个xml响应,如下所示:

<?xml version="1.0"?>
<stream:stream id="none" from="chat.facebook.com" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">
<stream:error>
    <xml-not-well-formed xmlns="urn:ietf:params:xml:ns:xmpp-streams"/>
</stream:error>


在他们最近的文档()中,他们指出通信应通过TLS进行。奇怪的是,如果我进入代码,它工作得很好,但是如果我正常运行它,它就会失败。否则,不要相信来自Facebook的文档;可能都关了。了解如何实现它,但这并不是一件小事——你可以花3个月以上的时间来实现整个堆栈,真的,只需使用jabber net即可。@Jonathan Dickinson,用于学习目的。这并不是说我的代码根本不起作用,而是一个奇怪的错误。很可能是与START-TLS相关的,用谷歌搜索。@Jonathan Dickinson你是说START-TLS节吗?
<?xml version="1.0"?>
<stream:stream id="none" from="chat.facebook.com" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">
<stream:error>
    <xml-not-well-formed xmlns="urn:ietf:params:xml:ns:xmpp-streams"/>
</stream:error>