C# 多线程服务器 - 跨域策略

本文关键字:策略 多线程 服务器 | 更新日期: 2023-09-27 18:32:35

>我有一台服务器,在浏览器中侦听来自 SWF 文件的连接。

在本地运行时,它会拾取一个连接,然后在日志中离开后很快将其丢弃:

[31/1/2016 18:10:14] 127.0.0.1已连接。 完整 127.0.0.1:58482

[31/1/2016 18:10:14] 从客户端 0 获取<策略文件请求 />

[31/1/2016 18:10:16] 客户端 0 已断开连接并已删除。

当服务器运行并且客户端在 flashdevelopment 中以调试模式启动时,不会记录此情况,但客户端会根据需要连接并执行操作。

我正在包含我的 TcpClient 类,我是否犯了错误或拼写错误,或者当在浏览器中而不是在 flashdevelopment 中运行时,它的行为应该有所不同吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ParticleFramework.Communication
{
    class TcpClient
    {
        #region Required Variables
        public Socket socket;
        public int index;
        private byte[] dataBuffer = new byte[0x400];
        private AsyncCallback ReceiveCallback;
        private AsyncCallback SendCallback;
        #endregion
        #region ArchiCruise Vars
        public ArchiCruise.Users.UserObject userObject;
        public string ip;
        #endregion
        public TcpClient(Socket sock, int num)
        {
            index = num;
            socket = sock;
            ip = socket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0];
            ReceiveCallback = new AsyncCallback(this.ReceivedData);
            SendCallback = new AsyncCallback(this.sentData);
            this.WaitForData();
        }
        public void Disconnect()
        {
            if (socket.Connected)
            {
                socket.Close();
                if (userObject != null) userObject.remove();
                Particle.Server.removeClient(this);
                Log.Info("Client" + this.index + " disconnected and removed.");
                Console.WriteLine("Client" + this.index + " disconnected.");
            }
        }
        private void ReceivedData(IAsyncResult iAr)
        {
            try
            {
                int count = 0;
                try
                {
                    count = socket.EndReceive(iAr);
                }
                catch
                {
                    Disconnect();
                }
                StringBuilder builder = new StringBuilder();
                builder.Append(System.Text.Encoding.Default.GetString(this.dataBuffer, 0, count));
                string str = System.Text.Encoding.Default.GetString(this.dataBuffer, 0, count);
                if (str.Contains("<policy-file-requet/>"))
                {
                    Log.Info("Sending policy file to client" + this.index);
                    rawSend("<?xml version'"1.0'"?><cross-domain-policy><allow-access-from-domain='"*'" to-ports='"*'" /><cross-domain-policy>" + Convert.ToChar(0));
                }
                else if (!(str.ToString() == ""))
                {
                    string packet = str.Substring(0, str.Length - 1);
                    //packet = ArchiCruise.Security.Encryption.decrypt(packet);
                    Log.Info("Got " + str + " from client " + this.index);
                    Particle.packetClass.handle(packet, this);
                }
                else
                {
                    Disconnect();
                }
            }
            catch (Exception exception)
            {
                Log.Info("Data recieve error: " + exception.ToString() + " " + exception.Source);
                Disconnect();
            }
            finally
            {
                this.WaitForData();
            }
        }
        private void WaitForData()
        {
            try
            {
                socket.BeginReceive(this.dataBuffer, 0, this.dataBuffer.Length, SocketFlags.None, this.ReceiveCallback, socket);
            }
            catch
            {
                Disconnect();
            }
        }
        public void sendData(string Data)
        {
            Data += (char)1;
            rawSend(Data);
        }
        internal void rawSend(string Data)
        {
            try
            {
                Data += "'0";
                byte[] bytes = System.Text.Encoding.Default.GetBytes(Data);
                socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(this.sentData), null);
                Log.Info("Sent " + Data + " to client " + this.index);
            }
            catch
            {
                Disconnect();
            }
        }
        private void sentData(IAsyncResult iAr)
        {
            try
            {
                socket.EndSend(iAr);
            }
            catch
            {
                Disconnect();
            }
        }
    }
}

C# 多线程服务器 - 跨域策略

你犯了一个拼写错误。

if (str.Contains("<policy-file-requet/>"))

应该是

if (str.Contains("<policy-file-request/>"))