Home All Groups Group Topic Archive Search About

NetworkStream with CryptoStream help

Author
22 Mar 2006 12:41 AM
Andre Azevedo
Hi all,

I've some problems using NetworkStream with CryptoStream. I create a simple
server/client projects and I think the problem is in
cryptoStream.

If the server sends data using networkStream, the data is sended. But if the
server sends data with cryptoStream the send process stops because it needs
to feel all the client read buffer. In this way, only closing the socket the
message is sent. If the client buffer is small the server sends the message.

The same problem happens in client. If client read using networkStream, no
matter what stream server is using to send or the client buffer size, the
client receives the message. But if client uses cryptoStream, it blocks.

Thanks,

--
Andre Azevedo

ClientCode:
-------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;
using System.IO.Compression;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {

            Int32 port = 8080;
            TcpClient client = new TcpClient("127.0.0.1", port);

            NetworkStream n = client.GetStream();
            Stream write = new CryptoStream(n, new ToBase64Transform(),
CryptoStreamMode.Write);
            Stream read = new CryptoStream(n, new FromBase64Transform(),
CryptoStreamMode.Read);

            while (true)
            {

                //byte[] data = new Byte[1024];
                byte[] data = new Byte[5];

                Console.WriteLine("Receiving!");
                read.BeginRead(data, 0, data.Length, new
AsyncCallback(ReadCallback), read);
                //n.BeginRead(data, 0, data.Length, new
AsyncCallback(ReadCallback), n);
                Thread.Sleep(3000);

            }

            read.Close();
            client.Close();

        }

        private static void ReadCallback(IAsyncResult ar)
        {
            try
            {
                Stream read = (Stream) ar.AsyncState;
                int bytes = read.EndRead(ar);

                Console.WriteLine("Received " + bytes + " bytes");
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}

ServerCode:
-----------------

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Security.Cryptography;
using System.IO.Compression;
using System.Threading;

class MyTcpListener
{
    public static void Main()
    {

        TcpListener server = null;

        Int32 port = 8080;
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");

        server = new TcpListener(localAddr, port);
        server.Start();

        while (true)
        {

            Console.Write("Waiting for a connection... ");

            Socket client = server.AcceptSocket();
            Console.WriteLine("Connected!");

            NetworkStream n = new NetworkStream(client);

            Stream write = new CryptoStream(n,  new ToBase64Transform(),
CryptoStreamMode.Write);
            Stream read = new CryptoStream(n, new FromBase64Transform(),
CryptoStreamMode.Read);

            String data = null;
            //data = "013456789013456789013456789013456789013456789" //50
bytes!
            data = "0134567890"; // 10 bytes

            byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

            //n.BeginWrite(msg, 0, msg.Length, new
AsyncCallback(SendCallback), n);
            write.BeginWrite(msg, 0, msg.Length, new
AsyncCallback(SendCallback), write);

            Thread.Sleep(2000);

            write.Close();
            client.Close();
        }
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
            {
                Stream write = (Stream) ar.AsyncState;
                write.EndWrite(ar);
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
    }
}

AddThis Social Bookmark Button