Wrox Programmer Forums
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 9th, 2008, 01:55 PM
Registered User
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default NetworkStream

It is a proxy server example (console application). i am just listening a port and when a client hit that port it read the data from client, connect to server ip/port, send the request to server, receive the response, send that response back to client.

Problem:
In case of server response is available completely where as NetworkStream.DataAvailable is true. chunk of data is available that is about 883Bytes when i read that chunk, now NetworkStream.DataAvailable is false and data will not available next time.
If you increse the sleep time of thread. complete data will be available.


---------------------------------------------------
static void Main(string[] args)
        {
            const int port = 8080;
            IPAddress localIP = IPAddress.Parse("127.0.0.1");
            TcpListener tcplistener = new TcpListener(localIP, port);
            tcplistener.Start();
            while (true)
            {
                TcpClient client = tcplistener.AcceptTcpClient();
                ProxyServer webproxy = new ProxyServer(client);
                new Thread(new ThreadStart(webproxy.run)).Start();
            }
        }
---------------------------------------------------

class ProxyServer
    {
        TcpClient Client;

        public ProxyServer(TcpClient socket)
        {
            this.Client = socket;
        }
        public void run()
        {
            TcpClient Server = new TcpClient("10.0.0.1", 6588); //Gateway IP and port to resolve you request to network
            //TcpClient Server = new TcpClient("127.0.0.1", 6588);
            //TcpClient Server = new TcpClient("127.0.0.1", 80);
            StringBuilder Log = new StringBuilder();
            try
            {
                Log.Append ("Start -- " + Thread.CurrentThread.ManagedThreadId + "\n");

                NetworkStream nClientStream = Client.GetStream();
                NetworkStream nServerStream = Server.GetStream();
                int recv;
                byte[] data;
                int counter;

                while (true)
                {
                    if (nClientStream.DataAvailable)
                        break;
                    else
                    {
                        Thread.Sleep(50);
                    }
                }
                counter = 1;
                while (true)
                {
                    data = new byte[Client.ReceiveBufferSize];
                    recv = nClientStream.Read(data, 0, data.Length);

                    nServerStream.Write(data, 0, recv);
                    nServerStream.Flush();
                    if (!nClientStream.DataAvailable)
                    {
                        break;
                    }
                    counter++;
                }
                Log.Append("Client Read Counter: " + counter + "\n");
                //nClientStream.Flush();

                while (true)
                {
                    if (nServerStream.DataAvailable)
                        break;
                    else
                    {
                        Thread.Sleep(50);
                    }
                }
                counter = 1;
                Log.Append("nServerStream.CanRead=" + nServerStream.CanRead + "\n");
                Log.Append("Server.Available=" + Server.Available.ToString() + "\n");


                /*
                    data is available but only a chunk! about 883Bytes
                    in below while loop it will be finised in first time and data will not available
                    where as if you increse the sleep time of thread. complete data will be available
                */
                Thread.Sleep(3000); //Increase the sleep time to get the proper data
                while (true)
                {
                    data = new byte[Server.ReceiveBufferSize];
                    recv = nServerStream.Read(data, 0, data.Length);
                    Log.Append ("recv = " + recv + "\n");
                    nClientStream.Write(data, 0, recv);
                    nClientStream.Flush();
                    if (!nServerStream.DataAvailable)
                    {
                        break;
                    }
                    counter++;
                }
                //nServerStream.Flush();
                Log.Append("Server Read Counter: " + counter + "\n");

                nServerStream.Close();
                nClientStream.Close();

                Log.Append("Stop -- " + Thread.CurrentThread.ManagedThreadId);
            }
            catch (Exception exc2)
            {
                Log.Append("Exception -- " + Thread.CurrentThread.ManagedThreadId + "\n");
                Log.Append(exc2.Message + "\n");
                Log.Append(exc2.StackTrace + "\n");
            }
            finally
            {
                Client.Close();
                Server.Close();
            }

            Console.WriteLine(Log.ToString ());
        }
    }









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.