Home All Groups Group Topic Archive Search About

Network and Cyrpto Stream help

Author
18 Mar 2006 5:14 PM
Andre Azevedo
Hi all,

I'm using async NetworkStream methods in my client/server socket app and
it's working fine.
But after using CryptoStream (base64) in NetworkStream it doesn't work. The
following code runs on both client and server:

---
tcpclass.networkStream = new NetworkStream(tcpclass.Socket);

tcpclass.sendStream = tcpclass.networkStream;
tcpclass.receiveStream = tcpclass.networkStream;

tcpclass.sendStream = new CryptoStream(tcpclass.sendStream, new
ToBase64Transform(), CryptStreamMode.Write);

tcpclass.receiveStream = new CryptoStream(tcpclass.receiveStream, new
FromBase64Transform(), CryptStreamMode.Read);
----
tcpclass.sendStream.BeginWrite(outBuffer,....)
---
tcpclass.receiveStream.BeginRead(inBuffer...)
---

If the server uses the CryptoStream and the client only uses NetworkStream,
the client can receive the encoded message. But if client also uses
CryptoStream, it can't read anything.

Any help will be apreciated!

Thanks,

--
Andre Azevedo

Author
18 Mar 2006 6:22 PM
Jon Skeet [C# MVP]
Andre Azevedo <x***@xpto.com> wrote:
Show quote
> I'm using async NetworkStream methods in my client/server socket app and
> it's working fine.
> But after using CryptoStream (base64) in NetworkStream it doesn't work. The
> following code runs on both client and server:
>
> ---
> tcpclass.networkStream = new NetworkStream(tcpclass.Socket);
>
> tcpclass.sendStream = tcpclass.networkStream;
> tcpclass.receiveStream = tcpclass.networkStream;
>
> tcpclass.sendStream = new CryptoStream(tcpclass.sendStream, new
> ToBase64Transform(), CryptStreamMode.Write);
>
> tcpclass.receiveStream = new CryptoStream(tcpclass.receiveStream, new
> FromBase64Transform(), CryptStreamMode.Read);
> ----
> tcpclass.sendStream.BeginWrite(outBuffer,....)
> ---
> tcpclass.receiveStream.BeginRead(inBuffer...)
> ---
>
> If the server uses the CryptoStream and the client only uses NetworkStream,
> the client can receive the encoded message. But if client also uses
> CryptoStream, it can't read anything.
>
> Any help will be apreciated!

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

I would strongly advise checking that the encryption/decryption you're
using works before you put the networking in.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
19 Mar 2006 3:37 PM
Andre Azevedo
Hi Mr Skeet,

> Could you post a short but complete program which demonstrates the
> problem?

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