Bonjour, je rencontre un petit problème dans un de mes programme.

Une partie de ce programme est un remote shell, la sortie de la commande se fait presque correctement, c'est à dire que lors de l'entrée de la 1ere commande la réception se fait. Lors de la seconde commande je ne reçois pas la sortie de la commande, je dois renvoyer la commande pour recevoir la sortie.

Je ne comprends pas trop pourquoi ça fait cela. En local sa fonctionne parfaitement par contre, mais dès que je test sur deux pc distinct là je rencontre le problème ci-dessus.

Voilà le code complet client et server de cette partie.

Client side :

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
void cb_send_cmd(GtkWidget *widget, gpointer user_data)
{
   //SOCKET remote_shell_sock = 0;
 
    //GtkWidget *remote_shell_closed_dialog = NULL;
 
    GtkTextBuffer *text_buffer = NULL;
    gchar *text = NULL;
    GtkTextIter start;
    GtkTextIter end;
    gchar *utf8_text = NULL;
 
    FILE *log_cmd_output = NULL;
    FILE *log_cmd_historic = NULL;
 
    //char *buffer = NULL;
    const char *buffer_cmd = NULL;
    //char *log_buffer = NULL;
    //char *historic_buffer = NULL;
    char *start_command = "> Command : ";
 
    char historic[MAXDATASIZE] = {""};
    char cmd_output[MAXDATASIZE] = {""};
    int caractereLu = 0;
    int i = 0;
    char line[MAXDATASIZE] = {""};
    char line_output[MAXDATASIZE] = {""};
    char *print_line = NULL;
    char *print_line_output = NULL;
 
    size_t data_len = 0;
 
 
    sended_one_cmd = 1;
 
    buffer_cmd = gtk_entry_get_text(GTK_ENTRY(user_data));
 
    log_cmd_historic = fopen("Logs/cmd_historic.log", "a");
    if(log_cmd_historic == NULL)
    {
        error("fopen() log_cmd_historic", "cb_send_cmd()");
        return;
    }
    fputs("> Command : ", log_cmd_historic);
    fputs(buffer_cmd, log_cmd_historic);
    fputs("\n", log_cmd_historic);
 
    fclose(log_cmd_historic);
 
    log_cmd_output = fopen("Logs/remote_shell.log", "a");
    if(log_cmd_output == NULL)
    {
        error("fopen() log_cmd_output", "cb_send_cmd()");
        return;
    }
 
    // Write in log file the command name
    fputs("> Command : ", log_cmd_output);
    fputs(buffer_cmd, log_cmd_output);
    fputs("\n", log_cmd_output);
 
    var_allocation();
 
    /*
    buffer = malloc(BUFSIZ * sizeof(char));
    if(buffer == NULL)
    {
        error("malloc() buffer", "send_cmd()");
        return;
    }
 
    log_buffer = malloc(MAXDATASIZE  * sizeof(char));
    if(log_buffer == NULL)
    {
        error("malloc log_buffer", "cb_send_cmd()");
        return;
    }
 
    historic_buffer = malloc(MAXDATASIZE  * sizeof(char));
    if(historic_buffer == NULL)
    {
        error("malloc historic_buffer", "cb_send_cmd()");
        return;
    }
    */
 
    data_len = strlen(buffer_cmd) + 1;
 
    // Send the command length
    if(send(sock, (char*)&data_len, sizeof(data_len), 0) == SOCKET_ERROR)
    {
        error("send() data_len", "cb_send_cmd()");
        return;
    }
 
    // Send the command
    if(send(sock, buffer_cmd, data_len, 0) == SOCKET_ERROR)
    {
        error("send() buffer_cmd", "cb_send_cmd()");
        return;
    }
 
    if(strncmp(buffer_cmd, "quit", 4) == 0)
    {
 
        gtk_entry_set_text(GTK_ENTRY(user_data), "Use quit command for exit ...");
 
        if(system("rm -rf Logs/cmd_historic.log") == -1)
        {
            error("rm -rf Logs/cmd_historic.log in quit",  "cb_send_cmd()");
            return;
        }
 
        if(system("rm -rf Logs/remote_shell.log") == -1)
        {
            error("rm -rf Logs/remote_shell.log in quit",  "cb_send_cmd()");
            return;
        }
 
        //shutdown(remote_shell_sock, SHUT_WR);
 
        remote_shell_closed_dialog = gtk_message_dialog_new (GTK_WINDOW(main_win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Remote Shell have been closed !");
        switch(gtk_dialog_run(GTK_DIALOG(remote_shell_closed_dialog)))
        {
            default :
                gtk_widget_destroy(remote_shell_closed_dialog);
                break;
        }
 
        return;
    }
 
    /** PROBLEM DE RECEPTION DE LA COMMANDE **/
 
    // Receive the command output
    if(recv(sock, buffer, BUFSIZ, 0) == SOCKET_ERROR)
    {
        error("recv() buffer", "cb_send_cmd()");
        return;
    }
 
    printf("\n\nCommand output received : %s", buffer);
    printf("\n\n");
 
    // Write in log file the command output
    fputs(buffer, log_cmd_output);
    fputs("\n", log_cmd_output);
 
    fclose(log_cmd_output);
 
    // Fill buffer from print in command's historic
    utf8_text = g_locale_to_utf8(start_command, strlen(start_command), NULL, NULL, NULL);
    historic_buffer = strncpy(historic_buffer, utf8_text, strlen(utf8_text) + 1);
    historic_buffer = strncat(historic_buffer, buffer_cmd, strlen(buffer_cmd) + 1);
    historic_buffer = strncat(historic_buffer, "\n", 2);
 
    // Print output command in historic
    log_cmd_historic = fopen("Logs/cmd_historic.log", "r");
    if(log_cmd_historic == NULL)
    {
        error("fopen() cmd_historic opened with r", "cb_send_cmd()");
        return;
    }
 
    utf8_text = g_locale_to_utf8(historic, strlen(historic), NULL, NULL, NULL);
 
    while((caractereLu = fgetc(log_cmd_historic)) != EOF)
    {
        historic[i] = (char)caractereLu;
        i++;
    }
 
    print_line = malloc(MAXDATASIZE * sizeof(char));
    if(print_line == NULL)
    {
        error("malloc() print_line", "cb_send_cmd()");
        return;
    }
    fseek(log_cmd_historic, 0, SEEK_SET);
 
    if(log_cmd_historic != NULL)
    {
        strncpy(print_line, line, strlen(print_line) + 1);
        while (fgets(line, MAXDATASIZE, log_cmd_historic) != NULL)
        {
            strncat(print_line, line, strlen(line));
        }
        fclose(log_cmd_historic);
    }
 
    utf8_text = g_locale_to_utf8(print_line, strlen(print_line), NULL, NULL, NULL);
 
    // Obtaining the buffer associated with the widget
    text_buffer = gtk_text_view_get_buffer((GtkTextView*)(historic_text_view));
 
    // Set the default buffer text.
    gtk_text_buffer_set_text(text_buffer, utf8_text, -1);
 
    // Obtain iters for the start and end of points of the buffer
    gtk_text_buffer_get_start_iter(text_buffer, &start);
    gtk_text_buffer_get_end_iter(text_buffer, &end);
 
    // Get the entire buffer text
    text = gtk_text_buffer_get_text(text_buffer, &start, &end, FALSE);
 
    // Print the text
    g_print("%s", text);
 
    g_free(text);
 
    // Prepare for print command out put
    log_cmd_output = fopen("Logs/remote_shell.log", "r");
    if(log_cmd_output == NULL)
    {
        error("fopen() log_cmd_output opened with r", "cb_send_cmd()");
        return;
    }
 
    utf8_text = g_locale_to_utf8(cmd_output, strlen(cmd_output), NULL, NULL, NULL);
 
    while((caractereLu = fgetc(log_cmd_output)) != EOF)
    {
        cmd_output[i] = (char)caractereLu;
        i++;
    }
 
    print_line_output = malloc(MAXDATASIZE * sizeof(char));
    if(print_line_output == NULL)
    {
        error("malloc() print_line_output", "cb_send_cmd()");
        return;
    }
    fseek(log_cmd_output, 0, SEEK_SET);
 
    if(log_cmd_output != NULL)
    {
        strncpy(print_line_output, line_output, strlen(print_line_output) + 1);
        while (fgets(line_output, MAXDATASIZE, log_cmd_output) != NULL)
        {
            strncat(print_line_output, line_output, strlen(line_output));
        }
        fclose(log_cmd_output);
    }
 
    // Print outpout command in log
    // Copy the command output
    utf8_text = g_locale_to_utf8(print_line_output, strlen(print_line_output), NULL, NULL, NULL);
 
    // Obtaining the buffer associated with the widget
    text_buffer = gtk_text_view_get_buffer((GtkTextView*)(text_view));
 
    // Set the default buffer text.
    gtk_text_buffer_set_text(text_buffer, utf8_text, -1);
 
    // Obtain iters for the start and end of points of the buffer
    gtk_text_buffer_get_start_iter(text_buffer, &start);
    gtk_text_buffer_get_end_iter(text_buffer, &end);
 
    // Get the entire buffer text
    text = gtk_text_buffer_get_text(text_buffer, &start, &end, FALSE);
 
    // Print the text
    g_print("%s", text);
 
    g_free(text);
 
    free(buffer);
    free (log_buffer);
    free (historic_buffer);
 
    // Parametres inutilises
    (void)widget;
}
Server side :

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
void start_remote_shell(char *argv[])
{
    pid_t the_son = 0;
    char *shell = NULL;
 
    FILE *pipe[2] = {NULL};
 
    char buffer[BUFSIZ]= {""};
    char buffer_cmd[MAXDATASIZE] = {""};
    size_t data_len = 0;
    int ret = 0;
 
    the_son = fork();
    if(the_son < 0)
    {
        error("fork() the_son", "start_remote_shell()");
        exit(1);
    }
 
    if(the_son == 0)
    {
        shell = getenv("SHELL");
 
        if(shell == NULL)
        {
            error("getenv() shell", "start_remote_shell()");
            exit(1);
        }
 
        if(execv(shell, &argv[0]) == -1)
        {
            error("execv() shell", "start_remote_shell()");
            exit(1);
        }
    }
 
    else /*father*/
    {
        for(;;)
        {
            if(recv(csock, (char*)&data_len, sizeof(data_len), 0) == -1)
            {
                error("recv() data_len", "start_remote_shell()");
                exit(1);
            }
 
            /* Receive the command */
            ret = recv (csock, buffer, data_len, 0);
            if (ret < 0)
            {
                error("recv() buffer", "start_remote_shell()");
                exit(1);
            }
 
            buffer[data_len - 1] = '\0';
 
            if(strncmp(buffer, "quit", 4) == 0)
                exit(1);
 
            pipe[0] = popen(buffer, "w");
            if(pipe[0] == NULL)
            {
                error("popen() pipe[0]", "start_remote_shell()");
                exit(1);
            }
 
            /* Send command results */
            pipe[1] =  popen(buffer, "r");
            if(pipe[1] == NULL)
            {
                error("popen() pipe[1]", "start_remote_shell()");
                exit(1);
            }
 
            ret = fread(buffer_cmd, MAXDATASIZE, sizeof(char), pipe[1]);
            if(ret < 0)
            {
 
                error("fread() buffer_cmd", "start_remote_shell()");
                exit(1);
            }
 
            printf("\n\nret = %d\n\n", ret);
 
            if(send(csock, buffer_cmd, BUFSIZ, 0) == SOCKET_ERROR)
            {
                error("send() buffer_cmd", "start_remote_shell()");
                exit(1);
            }
 
            memset(buffer_cmd, 0, MAXDATASIZE);
            memset(buffer, 0, BUFSIZ);
 
            if(pclose(pipe[0]) == -1)
            {
                error("pclose() pipe[0]", "start_remote_shell()");
                exit(1);
            }
 
            if(pclose(pipe[1]) == -1)
            {
                error("pclose() pipe[1]", "start_remote_shell()");
                exit(1);
            }
        }
    }
 
    return;
 
    //pthread_exit(NULL);
}
Voici une capture d'écran de ce que j'obtiens sur la GUI :

Nom : Capture d’écran du 2024-09-24 08-17-45.png
Affichages : 154
Taille : 692,7 Ko


On voit bien que la 1ere commande (ls) pas de soucis, puis la seconde (ls -a) doit être envoyée deux fois pour recevoir la sortie.

Merci d'avance pour votre aide, bien à vous.

PS : Coté server la commande s'execute toujours correctement, le problème semble être uniquement dans le transfert du server vers le client de la sortie de la 2nd commande.

PS² : Coté server la fonction start_remote_shell() n'est pas déclaré comme un thread alors qu'il y a du code pour un thread dedans. J'aurais du mettre un prototype comme ça pour un thread : void *start_remote_shell(param, param). Est ce cela qui cause le problème ? Je n'en suis pas totalement persuadé.