TCPClient streaming

Dec 14, 2010 at 11:03 PM
Edited Dec 14, 2010 at 11:03 PM

Hi.

Can one give me an example on how to use a client and streams?

Got the server running, but would like to get a client going...

Fx.

                using (TcpClient client = new TcpClient())
                {
                    client.Connect(new IPEndPoint(IPAddress.Loopback, 8084));

                    using (Stream stream = client.GetStream())
                    {
                        //StreamReader reader = new StreamReader(stream);

                        //string g = reader.Peek().ToString();
                        BinaryWriter writer = new BinaryWriter(stream);
                        {
                            writer.Write("Hello");
                            writer.Flush();
                            stream.Flush();
                        }

                        System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                        string g = reader.ReadToEnd();
                    }
                }

 

Regards

 

Thomas

Dec 15, 2010 at 9:35 AM

Nevermind..

Read the examples :)

 

Thomas

Coordinator
Dec 15, 2010 at 11:15 AM

                socket.Connect(serverAddress);
                Stream socketStream = GetSocketStream(socket);
                using (StreamReader reader = new StreamReader(socketStream, Encoding.Default, true))
                using (StreamWriter writer = new StreamWriter(socketStream, Encoding.Default, 1024 * 8))
                {
                    reader.ReadLine();

                    for (int i = 0; i < 10; i++)
                    {
                        string commandName = Guid.NewGuid().ToString().Substring(0, 3);
                        string command = commandName + " " + DateTime.Now;
                        writer.WriteLine(command);
                        writer.Flush();
                        string line = reader.ReadLine();
                        Console.WriteLine(line);
                        Assert.AreEqual(string.Format(TestSession.UnknownCommandMessageFormat, commandName), line);
                    }                        
                }

There are lots of this kind sample code in UnitTest project, please read the code in this link:

http://supersocket.codeplex.com/SourceControl/changeset/view/60930#1286388