Improve C#socket performance

Asked

Viewed 129 times

2

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>\0");
                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 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);
    }

It’s functional, but my game is crashing too much, can anyone redo or give tips on how to improve it? Please! p

  • Instead of having byte[] data = new byte[Received]; put for example byte[] date = new byte[2048]; and if Received is > that 2048 do a while and divide by n calls so always sends small fixed values instead of large and variable values.

  • Thanks for the attention, gave a better one even more continues with some brakes, but thanks anyway

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.