salut.
Je suis entrains de faire une petite application client serveur simple.
voici l'algo
Le client se connecte au serveur sur une ip et un port.
puis il affiche une invite de commande. l'utilisateur entre une string et l'envois au serveur.
Ce dernier la compare avec les commande qu'il a (strcmp) puis envois au client une confirmation de ce qui s'est passé
le problème reside dans la comparaison.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
if(strcmp(bufferRecv,"damn")==0)
                            {   strcpy(bufferSend,"Command 1 executed successfully !\n");}
                            else if(strcmp(bufferRecv,"linux")==0)
                            {   strcpy(bufferSend,"Command 2 excuted successfully !\n");}
                            else
                            {   strcpy(bufferSend,"Command not found !\n");}
Quand j'entre une commande, le serveur me repond toujour qu'elle n'existe pas.

voici la source de l'application
Clien:
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
 
 
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
 
#define IP "127.0.0.1"
#define PORT 2000
 
void color(int couleurDuTexte,int couleurDeFond)
{
        HANDLE H=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(H,couleurDeFond*16+couleurDuTexte);
}
 
 
int main(void)
{
    int error;
 
    color(2,0);
    puts("Client side \n\n");
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,0), &WSAData);
 
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    if( sock != INVALID_SOCKET )
    {
        color(2,0);
        printf("Socket %d is new opened in TCP/IP mode.\n",sock);
 
        SOCKADDR_IN sin;
        sin.sin_addr.s_addr    = inet_addr(IP);
        sin.sin_family    = AF_INET;
        sin.sin_port    = htons(PORT);
 
        error = connect(sock, (SOCKADDR *)&sin, sizeof sin);
        if( error != SOCKET_ERROR )
        {
            char bufferSend[50];
            char bufferRecv[50];
            int go = 1;
 
            color(2,0);
            printf("Connected to server...\n\n");
            do
            {
                color(9,0);
                printf(".-=[RFT]=-. >");
                fgets(bufferSend, sizeof bufferSend, stdin);
                error = send(sock, bufferSend, strlen(bufferSend), 0);
                color(7,0);
                printf("Wating for reply....\n\n");
                if(error == SOCKET_ERROR)
                {   printf("Could not send data. %d\n",WSAGetLastError());}
 
                error = recv(sock, bufferRecv, sizeof(bufferRecv)-1, 0);
                if(error == SOCKET_ERROR)
                {   printf("Could not send data. %d\n",WSAGetLastError());}
                else
                {
                    bufferRecv[error] = '\0';
                    color(12,0);
                    printf("%s\n",bufferRecv);
                }
            }
            while (go);
 
            color(1,0);
            printf("send imposible, closing socket... %d\n",WSAGetLastError());
            closesocket(sock);
            printf("Socket %d is new closed. %d\n",sock,WSAGetLastError());
        }
        else
        {
            color(4,0);
            printf("Not connected ! connection failure.\n", WSAGetLastError());
            return EXIT_FAILURE;
        }
    }
    else
    {
        printf("Invalid Socket. Socket is not opened.\n", WSAGetLastError());
        return EXIT_FAILURE;
    }
 
 
    WSACleanup();
    color(7,0);
    system("pause");
    return EXIT_SUCCESS;
}
serveur:
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
 
 
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
 
#define PORT 2000
 
void color(int couleurDuTexte,int couleurDeFond)
{
        HANDLE H=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(H,couleurDeFond*16+couleurDuTexte);
}
 
 
 
int main(void)
{
    char bufferRecv[500];
    char bufferSend[500];
    int error;
 
    color(2,0);
    printf("Serveur side\n\n");
 
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,0), &WSAData);
 
    SOCKADDR_IN sin = {0};
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
 
    if( sock != INVALID_SOCKET )
    {
        color(2,0);
        printf("Socket %d is now opened in TCP/IP mode.\n",sock);
 
        sin.sin_addr.s_addr    = htonl(INADDR_ANY);
        sin.sin_family    = AF_INET;
        sin.sin_port    = htons(PORT);
 
        error = bind(sock, (SOCKADDR *)&sin, sizeof(sin));
        if( error != SOCKET_ERROR )
        {
            error = listen(sock, 2);
            if( error != SOCKET_ERROR )
            {
                color(2,0);
                printf("listening on port %d\n"
                       "Waiting for a client connection on this port...\n",PORT);
 
                SOCKADDR_IN csin = {0};
                int sizeofcsin = sizeof(csin);
                SOCKET csock = accept(sock, (SOCKADDR *)&csin, &sizeofcsin);
                if( csock != INVALID_SOCKET )
                {
                    printf("Client connected\n\n");
 
                    int go = 1;
                    while(go)
                    {
                        memset(bufferRecv,0,sizeof(bufferRecv));
                        memset(bufferSend,0,sizeof(bufferSend)); //initialising the tow char variables
                        error = recv(csock, bufferRecv, sizeof(bufferRecv)-1, 0);
                        if(error != SOCKET_ERROR)
                        {
                            bufferRecv[error] = '\0';
                            color(12,0);
                            printf("Command receaved.\n"); //tells that the command was receved
                            // command checking prrocess
                            if(strcmp(bufferRecv,"damn")==0)
                            {   strcpy(bufferSend,"Command 1 executed successfully !\n");}
                            else if(strcmp(bufferRecv,"linux")==0)
                            {   strcpy(bufferSend,"Command 2 excuted successfully !\n");}
                            else
                            {   strcpy(bufferSend,"Command not found !\n");}
                        }
                        else
                        {
                            printf("Socket error, recv ! %d\n",WSAGetLastError());
 
                        }
 
                        color(9,0);
                        printf("Sending reply ...\n ");
                        error = send(csock, bufferSend, strlen(bufferSend), 0);
                        color(7,0);
                        printf("Waiting for another command....\n\n");
                        if(error == SOCKET_ERROR)
                        {
                            color(4,0);
                            printf("Socket error, send ! %d\n", WSAGetLastError());
 
                        }
                    }
 
                    shutdown(csock, 2);
                    closesocket(csock);
                    color(1,0);
                    printf("Client socket %d is now closed.\n",csock);
                }
                else
                {
                    color(4,0);
                    printf("Invalide Socket, error in opening Client socket %d\n", WSAGetLastError());
                    system("pause");
                    return EXIT_FAILURE;
                }
            }
        }
        closesocket(sock);
        color(1,0);
        printf("Server socket %d is now closed.\n",sock);
    }
    else
    {
        color(4,0);
        printf("Invalide Socket, erreur in opening Server socket %d\n", WSAGetLastError());
        system("pause");
        return EXIT_FAILURE;
    }
 
 
    WSACleanup();
    color(7,0);
    system("pause");
    return EXIT_SUCCESS;
}