コード例 #1
0
ファイル: Server.cs プロジェクト: diwulechao/cometbox
        private void Loop()
        {
            clients = new List<Client>();

            while ( true ) {
                TcpClient client = listener.AcceptTcpClient();

                Client dc = new Client(client, this, authconfig);

                int i = 0;
                while (i < clients.Count) {
                    if ( !clients[i].IsLive ) {
                        clients.RemoveAt(i);
                    }
                    i++;
                }
            }
        }
コード例 #2
0
ファイル: Response.cs プロジェクト: diwulechao/cometbox
        public void SendResponse(NetworkStream stream, Client client)
        {
            if (File != null && !File.Exists) {
                return;
            }

            StringBuilder r = new StringBuilder();

            r.Append("HTTP/1.1 " + GetStatusString(Status) + "\r\n");
            r.Append("Server: cometbox\r\n");
            r.Append("Date: " + DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'") + "\r\n");
            r.Append("Accept-Ranges: none\r\n");

            foreach (KeyValuePair<string, string> header in Headers) {
                r.Append(header.Key + ": " + header.Value + "\r\n");
            }

            if (File != null) {
                r.Append("Content-Type: " + Mime + "\r\n");
                r.Append("Content-Length: " + File.Length + "\r\n");
            } else if (Body.Length > 0) {
                r.Append("Content-Type: " + Mime + "\r\n");
                r.Append("Content-Length: " + Body.Length + "\r\n");
            }
            r.Append("\r\n");

            byte[] htext = Encoding.ASCII.GetBytes(r.ToString());
            stream.Write(htext, 0, htext.Length);

            if (Body != "") {
                client.Send(Body);
            } else if (File != null) {
                using (FileStream fs = File.OpenRead()) {
                    byte[] b = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = fs.Read(b, 0, 1024)) > 0) {
                        stream.Write(b, 0, bytesRead);
                    }
                    fs.Close();
                }
            }
        }