c# socket .... ssl stream recieving 0 bytes before getting websocket headers

تعرفه تبلیغات در سایت
عنوان عکس
عنوان عکس
عنوان عکس
عنوان عکس
عنوان عکس

جستجوگر

یافته ها در جستجو

    امکانات وب

    آرشیو مطالب

    برچسب ها

    Vote count: 0

    I am really confused and would like some help clarifying on what is happening.

    So I have a c# socket server program that accepts websockets.

    Everything works fine when I use "new WebSocket(ws://localhost:100,'chat')" to open a new connection.

    Normaly Program will listen for new socket and accept socket asynchronously. After accepting socket I can recieve data using BegineRecieve and send handshake.

    However, when I implement sslstream and use "new WebSocket(wss://localhost:100,'chat')" to start websocket connection. The program behaves differently.

    After my program accepts the socket I receive 0 bytes using BegineRead. After which a I have to listen and accept another socket and call BegineRead again to get the websocket headers only then I can send the handshake.

    Is this by design or am I missing something?

    Below is the relavent code.

    private static void listen_new(Socket sock)
            {
    
                try
                {
                    Console.WriteLine("Listening Incoming Connnection...");
                    sock.Listen(5);
                    sock.BeginAccept(new AsyncCallback(start_accept_new), sock);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("Socket Error Occured (Listen New):" + e.Message);
                }
            }
    
            private static void start_accept_new(IAsyncResult AR)
            {
                try
                {
                    Console.WriteLine("Accepting New Connection....");
                    Socket sock = ((Socket)AR.AsyncState).EndAccept(AR);
                    listen_new((Socket)AR.AsyncState);
                    NetworkStream myNetworkStream = new NetworkStream(sock, true);
                    SslStream sslStream = new SslStream(myNetworkStream, false);
    
                    try
                    {
                        sslStream.AuthenticateAsServer(serverCertificate,
                        false, SslProtocols.Tls12, true);
                        Console.WriteLine("IsAuthenticated: {0}", sslStream.IsAuthenticated);
                        sslStream.BeginRead(_buffer, 0, _buffer.Length, ar => { begin_handshake(ar, sock); }, sslStream);
                    }
                    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.");
                        sslStream.Close();
                        sock.Close();
                        return;
    
                    }
    
                }
                catch (SocketException e)
                {
                    Console.WriteLine("Socket Error Occured(Start Accept New):" + e.Message);
                }
    
            }
            private static void begin_handshake(IAsyncResult AR, Socket sock)
            {
                Console.WriteLine("Shaking Hands");
                SslStream sock_stream = ((SslStream)AR.AsyncState);
                int Data = sock_stream.EndRead(AR);
                Console.WriteLine("Data Recieved"+ Data);
                if (Data != 0)
                {
                    byte[] databyte = new byte[Data];
                    Array.Copy(_buffer, databyte, Data);
    
                    String text = Encoding.ASCII.GetString(databyte);
                    List headers = retriveheaders(text);
                    bool headercheck = authenticate_socket(headers);
    
                    if (headercheck == true)
                    {
                        acceptuser(headers, sock, sock_stream);
    
                    }
                    else
                    {
                        sock_stream.Close();
                        sock.Close();
                        sock.Dispose();
                    }
                }
                else {
                    sock.Shutdown(SocketShutdown.Both);
                    sock_stream.Close();
                    sock.Close();
                    sock.Dispose();
                }
    
    
            }
    

    Currently this code discards the socket if data received is 0 bytes and processes if data received is not 0.

    asked 2 mins ago
    user2288650

    نویسنده : استخدام کار بازدید : 2 تاريخ : سه شنبه 24 بهمن 1396 ساعت: 8:50
    برچسب‌ها :
    اخبار و رسانه هاهنر و ادبیاترایانه و اینترنتعلم و فن آوریتجارت و اقتصاداندیشه و مذهبفوتو بلاگوبلاگ و وبلاگ نویسیفرهنگ و تاریخجامعه و سیاستورزشسرگرمی و طنزشخصیخانواده و زندگیسفر و توریسمفارسی زبان در دیگر کشورها