Improving socket performance in C #

1

Client.cs

    public void start()
    {
        this.Socket.BeginReceive(Buffer, 0, Buffer.Length, 0, new AsyncCallback(beginReceive), this.Socket);
    }
    private void beginReceive(IAsyncResult ar)
    {
        try
        {
            Socket socket = (Socket)ar.AsyncState;
            int received = socket.EndReceive(ar);
            byte[] data = new byte[received];
            Array.Copy(Buffer, data, received);
            string content = Encoding.ASCII.GetString(data);
            if (content.StartsWith("<policy-file-request/>"))
            {
                this.sendConsoleMessage("Novo client conectado!");
                byte[] byteData = Encoding.ASCII.GetBytes("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>
    public void run()
    {
        int[] selectedports = new int[] { 443, 44440, 44444, 5555, 3724, 6112 };
        List<int> ports = new List<int>();
        foreach (int port in selectedports)
        {
            if (checkPort(port))
            {
                ports.Add(port);
            }
        }
        if (ports.Count > 0)
        {
            foreach (int port in ports)
            {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Bind(new IPEndPoint(IPAddress.Any, port));
                server.Listen(100);
                server.NoDelay = true;
                server.BeginAccept(new AsyncCallback(beginAccept), server);
            }
            this.sendConsoleMessage("[" + string.Join(", ", ports) + "]");
        }
    }
    private void beginAccept(IAsyncResult ar)
    {
        Socket asocket = (Socket)ar.AsyncState;
        Socket socket = asocket.EndAccept(ar);
        _clientSocket.Add(socket);
        Client client = new Client(this, socket);
        client.start();
        asocket.BeginAccept(new AsyncCallback(beginAccept), asocket);
    }
"); socket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(beginSend), socket); } else { DataInputStream recvd = new DataInputStream(data); int lenLenght = recvd.readByte(); int count = 1; if (new int[3] { 1, 2, 4 }.Contains(lenLenght)) { int size = 0; if (lenLenght.Equals(1)) size = recvd.readByte(); else if (lenLenght.Equals(2)) size = recvd.readShort(); else size = recvd.readInt(); count += size + 1; byte[] tobyte = new byte[count]; Array.Copy(data, tobyte, count); this.datareceive(recvd, tobyte); } } socket.BeginReceive(Buffer, 0, Buffer.Length, 0, new AsyncCallback(beginReceive), socket); } catch { } } private void beginSend(IAsyncResult ar) { Socket socket = (Socket)ar.AsyncState; socket.EndSend(ar); }

Server.cs

    public void start()
    {
        this.Socket.BeginReceive(Buffer, 0, Buffer.Length, 0, new AsyncCallback(beginReceive), this.Socket);
    }
    private void beginReceive(IAsyncResult ar)
    {
        try
        {
            Socket socket = (Socket)ar.AsyncState;
            int received = socket.EndReceive(ar);
            byte[] data = new byte[received];
            Array.Copy(Buffer, data, received);
            string content = Encoding.ASCII.GetString(data);
            if (content.StartsWith("<policy-file-request/>"))
            {
                this.sendConsoleMessage("Novo client conectado!");
                byte[] byteData = Encoding.ASCII.GetBytes("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>
    public void run()
    {
        int[] selectedports = new int[] { 443, 44440, 44444, 5555, 3724, 6112 };
        List<int> ports = new List<int>();
        foreach (int port in selectedports)
        {
            if (checkPort(port))
            {
                ports.Add(port);
            }
        }
        if (ports.Count > 0)
        {
            foreach (int port in ports)
            {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Bind(new IPEndPoint(IPAddress.Any, port));
                server.Listen(100);
                server.NoDelay = true;
                server.BeginAccept(new AsyncCallback(beginAccept), server);
            }
            this.sendConsoleMessage("[" + string.Join(", ", ports) + "]");
        }
    }
    private void beginAccept(IAsyncResult ar)
    {
        Socket asocket = (Socket)ar.AsyncState;
        Socket socket = asocket.EndAccept(ar);
        _clientSocket.Add(socket);
        Client client = new Client(this, socket);
        client.start();
        asocket.BeginAccept(new AsyncCallback(beginAccept), asocket);
    }
"); socket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(beginSend), socket); } else { DataInputStream recvd = new DataInputStream(data); int lenLenght = recvd.readByte(); int count = 1; if (new int[3] { 1, 2, 4 }.Contains(lenLenght)) { int size = 0; if (lenLenght.Equals(1)) size = recvd.readByte(); else if (lenLenght.Equals(2)) size = recvd.readShort(); else size = recvd.readInt(); count += size + 1; byte[] tobyte = new byte[count]; Array.Copy(data, tobyte, count); this.datareceive(recvd, tobyte); } } socket.BeginReceive(Buffer, 0, Buffer.Length, 0, new AsyncCallback(beginReceive), socket); } catch { } } private void beginSend(IAsyncResult ar) { Socket socket = (Socket)ar.AsyncState; socket.EndSend(ar); }

It's functional, but my game is crashing, can anyone redo or know how to give hints how can I improve this? please! : p

    
asked by anonymous 18.06.2016 / 19:41

0 answers