TCP客户端NetworkStream保持连接,不允许成功关闭

时间:2012-12-05 22:59:33

标签: c# tcp networkstream

我不知道我做错了什么但是当我从网络浏览器访问我的TCP服务器时,它会不断刷新页面。

这是我的代码:

public class HttpServer
{

    protected int port;
    private TcpListener tcpListener;
    private Thread listenThread;

    public HttpServer(int port)
    {
        this.port = port;
    }

    public void listen()
    {
        this.tcpListener = new TcpListener(IPAddress.Any, port);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;


        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)

        {
            bytesRead = 0;
            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                break;
            }

            {
                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                System.Diagnostics.Debug.WriteLine("MSG: " + encoder.GetString(message, 0, bytesRead));
                String page = getPage(encoder.GetString(message, 0, bytesRead));
                String parameters = getPageParameters(encoder.GetString(message, 0, bytesRead));
                Console.WriteLine("Page Access: " + page);
                try
                {
                    String[] oPara = parameters.Split('&');
                    for (int i = 0; i < oPara.Length; i++)
                    {
                        Console.WriteLine("Parameter Recieved #" + (i + 1).ToString() + ": " + oPara[i]);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("NULL PARAMETERS: " + ex.Message);
                }

                String oneT = "HTTP/1.0 200 OK\n\n";
                clientStream.Write(Encoding.ASCII.GetBytes(oneT), 0, Encoding.ASCII.GetBytes(oneT).Length);

                byte[] buffer2 = encoder.GetBytes(getPageHtml(page, parameters));

                clientStream.Write(buffer2, 0, buffer2.Length);



                clientStream.Close();
                break;
            }
        }

        try
        {
            tcpClient.Close();
        }
        catch
        {
        }
    }

    private String getPageParameters(String message)
    {
        try
        {
            //MSG: GET /homw?EX=1 HTTP/1.1
            //Host: 192.168.127.2:5125
            //User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0
            //Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
            //Accept-Language: en-US,en;q=0.5
            //Accept-Encoding: gzip, deflate

            String sO = message.Replace("Host", "^");
            String[] sA = sO.Split('^');
            //GET /homw?EX=1 HTTP/1.1

            String[] sAA = sA[0].Split(' ');
            String[] ZA = sAA[1].Split('?');///homw?EX=1
            ///EX=1 @ 1
            String msg = ZA[1];
            return msg;
        }
        catch
        {
            return "";
        }
    }

    private String getPage(String message)
    {
        //MSG: GET /homw HTTP/1.1
        //Host: 192.168.127.2:5125
        //User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0
        //Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
        //Accept-Language: en-US,en;q=0.5
        //Accept-Encoding: gzip, deflate

        String sO = message.Replace("Host", "^");
        String[] sA = sO.Split('^');
        //GET /homw HTTP/1.1

        String[] sAA = sA[0].Split(' ');
        String msg = "";
        for (int i = 1; i < sAA.Length - 1; i++)
        {
            msg += sAA[i];
        }
        return msg;
    }

    private void ListenForClients()
    {
        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication
            //with connected client
            Console.WriteLine("New Client");
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
            Thread.Sleep(500);
        }
    }

请有人告诉我我做错了什么,因为我找不到任何东西。

0 个答案:

没有答案