Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Network and Cyrpto Stream help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andre Azevedo - 18 Mar 2006 18:14 GMT
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
Jon Skeet [C# MVP] - 18 Mar 2006 19:22 GMT
> I'm using async NetworkStream methods in my client/server socket app and
> it's working fine.
[quoted text clipped - 23 lines]
>
> 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.

Signature

Jon Skeet - <skeet@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

Andre Azevedo - 19 Mar 2006 16:37 GMT
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);
           }
   }
}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.