Hi !!! I have one problem! I use StreamWriter to send (object) stream to server (not string). But how to read this object with StreamReader ???

The server who send stream (object):

private void SomeMethod(SqlDataReader reader)
        {
            string column = "ColumnName";
            object stream = ReadFromDB(reader, column);
            SendStream(stream);
        }

        public object ReadFromDB(SqlDataReader reader, string column)
        {
            List<string> dataFromColumn = new List<string>();

            while (reader.Read())
            {
                dataFromColumn.Add((string)reader[column]);
            }

            object listWithData = dataFromColumn;

            return listWithData;
        }               

        public void SendStream(object stream)
        {
            TcpClient tcpConnection = new TcpClient();
            tcpConnection.Connect(addressPort);

            NetworkStream netStream = tcpConnection.GetStream();

            using (StreamWriter strmWriter = new StreamWriter(netStream))
            {
                //netStream.WriteTimeout = 1000;
                strmWriter.Write(stream);
                strmWriter.Flush();
            }
        }

The client who need to read stream (object):

class StreamManager
    {
        public StreamManager(TcpClient tcpConnection)
        {
            this.netStream = tcpConnection.GetStream();
        }
        
        public List<string> ReciveResultFromDB()
        {
            char[] buffer = new char[4096];
            List<string> getResultFromDB = new List<string>();
            StreamReader strmReader = new StreamReader(netStream);

            strmReader.Read(buffer, 0, buffer.Length);

            // ??????????????????????    
            // ??????????????????????

            return getResultFromDB;
        }

        private NetworkStream netStream;
    }

Sorry for my bad English !!!

You can use BinaryFormatter 's Serialize and Deserialize methods instead of StreamWriter / StreamReader . Here's a little sample I wrote (with no error checking at all):

Receiver

static void Main(string[] args)
{
    TcpListener listener = new TcpListener(IPAddress.Any, 5005);
    listener.Start();
    Console.WriteLine("Server started.");

    Socket client = listener.AcceptSocket();
    Console.WriteLine("Accepted client {0}.\n", client.RemoteEndPoint);

    List<string> l = null;

    using (NetworkStream ns = new NetworkStream(client))
    {
        BinaryFormatter bf = new BinaryFormatter();
        l = (List<string>)bf.Deserialize(ns);
    }

    if (l != null)
        foreach (var item in l)
            Console.WriteLine(item);

    client.Close();
    listener.Stop();
}

Sender

static void Main(string[] args)
{
    TcpClient client = new TcpClient();
    client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5005));
    Console.WriteLine("Connected.");

    List<string> l = new List<string>() { "Lorem", "ipsum", "dolor", "sit", "amet" };

    BinaryFormatter bf = new BinaryFormatter();
    NetworkStream ns = client.GetStream();
    bf.Serialize(ns, l);
    Console.WriteLine("Data sent.");

    ns.Close();
    client.Close();
}

Thank you for your suggestion I`ll try it and I`ll answer you later.

Yes it work perfect !!! :) Thank you very much ! I`ll mark this 'Discussion Thread' as SOLVED !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.