【问题标题】:C# Multi-threaded Server - Cross Domain PolicyC# 多线程服务器 - 跨域策略
【发布时间】:2016-05-09 03:12:05
【问题描述】:

我有一个服务器,在浏览器中侦听来自 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] Client0 断开连接并被移除。

在运行服务器并且客户端在 flashdevelop 中以调试模式启动时,不会记录此内容,但客户端会连接并按照需要执行操作。

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

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# actionscript-3 cross-domain-policy


    【解决方案1】:

    你犯了一个拼写错误。

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

    应该是

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

    【讨论】:

    • 废话,谢谢。这并不能完全解决我现在刚刚得到的问题; [31/1/2016 18:19:50] Particle Server logging started... [31/1/2016 18:19:51] Test query succeeded. Database initialized. [31/1/2016 18:20:04] 127.0.0.1connected. Full 127.0.0.1:58625 [31/1/2016 18:20:04] Sending policy file to client0 [31/1/2016 18:20:04] Sent &lt; ?xml version"1.0"?&gt;&lt; cross-domain-policy&gt;&lt; allow-access-from-domain="*" to-ports="*" /&gt;&lt; cross-domain-policy&gt; to client 0 [31/1/2016 18:20:04] Client0 disconnected and removed.
    • 它显然需要继续前进并将客户端连接到服务器并获取他们的 ssiticket 等......但它仍然将它们踢出服务器
    • 获取他们的 SSO 票? :) 抱歉,这不是帮助模拟开发的服务。
    • 不,我知道。但问题是要找出服务器过早将用户踢出的原因,而您发现了一个错误,但它并没有解决这个问题,但还是谢谢您
    猜你喜欢
    • 2011-02-20
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多