Bonjour,
j'avais chosit le C# pour réaliser mon application de serveur-proxy suite à comparatif de vitesse qui le montrait bien placé pour les threads.
Après une ébauche du projet (200 lignes), je m'apperçois que mon programme utilise 50% du processeur avec un client connecté, 100% avec 2 clients.


Pourtant, le serveur (écrit en C++) sur lequel est connecté le proxy utilise peu de ressources processeur, alors qu'il analyse autant de données que le proxy !

Je me demande alors comment trouver les instruction qui consomment beaucoup de ressources processeur ?
variables globales ? manipulations de tableaux excessive sachant que le programme reçoit des données tous les dizièmes de secondes ?


Voici le code source de mon projet.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.IO;
using YSPS_apps;
 
namespace YSPS_apps
{
    //global variables
    public static class Common 
    {
        public static ArrayList acceptList = new ArrayList(); // liste to access the sockets variables of all the clients
        public static ArrayList usernameList = new ArrayList(); // list of the usernames
    }
 
 
    public class Client
    {
        public Socket socketServer;
        public Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        public string username;
        public int version;
 
        private static Int32 bytesToInt32(byte[] FourByteArrToConvert)
        {// convert a byte to an integer
            return System.BitConverter.ToInt32(FourByteArrToConvert, 0);
        }
        private static byte[] int32ToBytes(Int32 int32Input)
        {// convert an integer to a byte
            byte[] numBytesBuf = new byte[4];
            return System.BitConverter.GetBytes(int32Input);
        }
        private static byte[] byte_concat(byte[] array1, byte[] array2)
        {
            byte[] array3 = new byte[array1.Length + array2.Length];
            array1.CopyTo(array3, 0);
            array2.CopyTo(array3, array1.Length);
            return array3;
        }
 
        public Client(System.Net.Sockets.Socket CurrentClient2)
        {
            socketServer = CurrentClient2;
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7914);
            socketClient.Connect(ipEnd);// YSPS connects to the server
            Console.Out.WriteLine(socketClient.Available + "et" + socketServer.Available);//debug output
            while (socketServer.Available == 0)
            {
                Console.Out.WriteLine("sleep");//debug output
                Thread.Sleep(200);//wait an expect an answer from the YSFS server
                if (socketServer.Available != 0)
                    break;
            }
            byte[] msgFromServer = new Byte[socketServer.Available];
            socketServer.Receive(msgFromServer, 0, socketServer.Available, SocketFlags.None);
            //Console.Out.WriteLine("valeur" + System.Text.Encoding.UTF8.GetString(msgFromServer).Trim().IndexOf('\x0018') + System.Text.Encoding.UTF8.GetString(msgFromServer).Trim().IndexOf('\x0061') + System.Text.Encoding.UTF8.GetString(msgFromServer).Trim().IndexOf('\x007A') + System.Text.Encoding.UTF8.GetString(msgFromServer).Trim().IndexOf('\x0065'));
            byte[] user=new byte[16];
            for (int i = 0; i < 16; i++)
            {
                user[i] = 32;
            }
            for (int i = 8; i < 16;i++)
            {
                if (msgFromServer[i] == 0)
                {
                    break;
                }
                user[i-8]=msgFromServer[i];
            }
 
            username = System.Text.Encoding.UTF8.GetString(user);
            username=username.Normalize().Trim();
            Console.Out.WriteLine(username.Length);
            Console.Out.WriteLine("username " + username+ "end");
            Common.usernameList.Add(username);
            socketClient.Send(msgFromServer);
        }
        public void run()
        {
            while (true)
            {
                if (socketClient.Available > 0)
                {//from server
                    byte[] msgToClient = new Byte[socketClient.Available];
                    socketClient.Receive(msgToClient);
                    //socketClient.Receive(msgToClient, 0, socketClient.Available, SocketFlags.None);
                    string messageReceived = System.Text.Encoding.UTF8.GetString(msgToClient).Trim();
 
                    try
                    {
                        socketServer.Send(msgToClient);
                    }
                    catch
                    {
                        Console.Out.WriteLine(username + " has left the server");
                        break;
                    }
                }
                if (socketServer.Available > 0)
                {//from client
                    byte[] msgLength = new Byte[4];
                    socketServer.Receive(msgLength, 0, 4, SocketFlags.None);
                    int byte_length = bytesToInt32(msgLength);
                    if (byte_length <= socketServer.Available)
                    {//if the message-length match with the real length of the packet
                        byte[] msgToServer = new Byte[byte_length];
                        socketServer.Receive(msgToServer, 0, byte_length, SocketFlags.None);
 
                        int data_kind = bytesToInt32(msgToServer);
                        //Console.Out.WriteLine(data_kind.ToString());//debug
                        if (data_kind == 32)
                        {//if it's a chat message
                            Console.Out.WriteLine("chat message");
                            string message = System.Text.Encoding.UTF8.GetString(msgToServer).ToString().Trim();//the whole chat message
                            Console.Out.WriteLine(message);//debug
                            int send_to_pos = message.IndexOf("%", username.Length + 2);
                            if (send_to_pos != -1)
                            {//if we found "%"
                                Console.Out.WriteLine("to an user");
                                int end_send_to_pos = message.IndexOf('%', send_to_pos + 1);
                                if (end_send_to_pos != -1)
                                {
                                    string to_user = message.Substring(send_to_pos + 1, end_send_to_pos - send_to_pos - 1).Trim();
                                    //string chat_message = "[" + username + "] " + message.Substring(end_send_to_pos, message.Length - end_send_to_pos).Trim();
                                    bool to_user_exist = false;
                                    for (int i = 0; i < Common.usernameList.Count; i++)
                                    {
                                        Console.Out.WriteLine("testing " + ((string)Common.usernameList[i]).Trim() + " with " + to_user + " end");
                                        if (((string)Common.usernameList[i]).Trim() == to_user)
                                        {
                                            to_user_exist = true;
                                            Console.Out.WriteLine("User found");
                                            ((Socket)Common.acceptList[i]).Send(byte_concat(msgLength, msgToServer));
                                        }
                                    }
                                    if (to_user_exist == false)
                                    {
                                        Console.Out.WriteLine("Unable to send this message to " + to_user);
                                    }
                                }
                                else
                                {
                                    Console.Out.WriteLine("Failed to find the 2nd %");
                                }
                            }
                            else
                            {
                                socketClient.Send(byte_concat(msgLength, msgToServer));//send the chat message to everybody
                            }
                        }
                        else
                        {
                            socketClient.Send(byte_concat(msgLength, msgToServer));//Send to the server
                        }
                    }
 
                }
            }
        }
 
    }
    public class Server
    {
        public void Start()
        {
            IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName());
            foreach (IPAddress ip in ipHostEntry.AddressList)
            {
                Console.Out.WriteLine(ip.ToString());
            }
            IPAddress ipAddress = ipHostEntry.AddressList[ipHostEntry.AddressList.Length - 1];
 
            Socket CurrentClient = null;
            Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                ServerSocket.Bind(new IPEndPoint(0, 7915));
                ServerSocket.Listen(30);
                while (true)
                {
                    Console.WriteLine("Attente d'une nouvelle connexion...");
                    CurrentClient = ServerSocket.Accept();
                    Console.Out.WriteLine(CurrentClient.GetType().ToString());
                    Common.acceptList.Add(CurrentClient);
                    Client obj = new Client(CurrentClient);
                    Thread thread2 = new Thread(new ThreadStart(obj.run));
                    thread2.Start();
                    Console.WriteLine("Nouveau client:" + CurrentClient.GetHashCode());
                }
            }
            catch (SocketException E)
            {
                Console.WriteLine(E.Message);
            }
        }
    }
 
    class MainClass
    {
        public static void Main(string[] args)
        {
            Server startIt = new Server();
            startIt.Start();
 
        }
 
    }
 
 
}