Bonjour, j'ai crée une application de chat en c++, j'ai exécuté le serveur et le client sur mon pc tout se passe bien mon programme tourne.

Le problème est que lorsque j'ai exécuté le serveur sur mon pc et le client sur autre pc pas de communication entre eux.

je n'ai aucune idée comment communiquer les deux pc !!!

code 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
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
#pragma comment(lib, "Ws2_32.lib")
 
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
 
using namespace std;
 
 
SOCKADDR_IN addr;
SOCKET sListen;
SOCKET sConnect;
SOCKET* Connections;
 
int addrlen = sizeof(addr);
int ConCounter = 0;
 
struct Buffer
{
	int ID;
	char Message[256];
};
 
int ServerThread(int ID){
 
	 // Send and receive data.
    int bytesSent;
 
    int bytesRecv = SOCKET_ERROR;
 
    char sendbuf[2000]="";
     char sendbuf2[2000]="";
 
    char recvbuf[2000]="";
 
    char timebuf[128];
 
 
    bytesSent = send( Connections[ID], sendbuf, strlen(sendbuf), 0);
 
 
 
    while (1)
    {
 
        ZeroMemory (recvbuf, sizeof(recvbuf));
 
        bytesRecv = recv( Connections[ID], recvbuf, 256 ,NULL);
 
		cout << "<Client " << ID << ":> " << recvbuf <<endl;
 
 
        bytesSent = send( Connections[ID], sendbuf, strlen(sendbuf), 0);
 
 
 
			if (strcmp(recvbuf, "exit") == 0)
        {
            goto fin;
        }
        else
        {
 
 
            if (bytesSent == SOCKET_ERROR)
            {
 
                goto fin;
            }
        }
    }
 
fin:
 
    printf("Client processed\n");
 
	return 0;
 
}
 
int InitWinSock()
{
	int RetVal=0;
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2,1);
RetVal = WSAStartup(DLLVersion, &wsaData);
 
return RetVal;
 
}
 
int main()
{
		int RetVal = 0;
	RetVal = InitWinSock();
	if(RetVal != 0){
	MessageBoxA(NULL, "Winsock startup failed", "Error",MB_OK | MB_ICONERROR);
	exit(1);
	}
 
	Connections = (SOCKET*)calloc(64, sizeof(SOCKET));
 
	sListen= socket(AF_INET, SOCK_STREAM, NULL);
	sConnect = socket(AF_INET, SOCK_STREAM, NULL);
 
	addr.sin_addr.s_addr = inet_addr("localhost");
	addr.sin_port = htons(1234);
	addr.sin_family = AF_INET;
 
	int err = 0;
	int sock_err;
	sock_err=bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
 
 
	cout << "Server Connected " << endl;
 
	sock_err=listen(sListen, 64);
 
	if (sock_err != SOCKET_ERROR)
            {
               /* wait for a client connection */
               printf ("waiting for a client connection...\n");
	}
 
 
 
 
	for(;; Sleep(50))
	{
		if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
		{
			if(sConnect!= INVALID_SOCKET){
			 printf ("client connected \n");
			 }
 
 
		Connections[ConCounter] = sConnect;
		char* ID = new char[64];
 
		ZeroMemory(ID, sizeof(ID));
 
     	itoa(ConCounter, ID, 10);
		send(Connections[ConCounter], ID, sizeof(ID), NULL);
 
		ConCounter= ConCounter + 1;
        CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) ServerThread, (LPVOID)(ConCounter - 1), NULL, NULL);
 
		}
	}
 
	closesocket(sListen);
    WSACleanup();
	return 0;
 
}
code client:

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
#pragma comment(lib, "Ws2_32.lib")
 
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
 
using namespace std;
 
SOCKADDR_IN addr;
 
SOCKET sConnect;
 
struct Buffer
{
    int ID;
    char Message[256];
};
 
int ClientThread()
{
    Buffer sbuffer;
 
    char buffer[sizeof(sbuffer)];
 
    for(;; Sleep(10))
    {
        int numRead = recv(sConnect, buffer, sizeof(buffer), NULL);
        if (numRead < 1) break;
 
        memcpy(&sbuffer, buffer, numRead);
        cout << "<Client " << sbuffer.ID << ":> " << sbuffer.Message << endl;
    }
 
    return 0;
}
 
int main()
{
    system("cls");
 
    int RetVal = 0;
 
    WSAData wsaData;
    WORD DllVersion = MAKEWORD(2,1);
    RetVal = WSAStartup(DllVersion, &wsaData);
    if (RetVal != 0)
    {
        MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
        exit(1);
    }
 
    sConnect = socket(AF_INET, SOCK_STREAM, NULL);
 
    addr.sin_addr.s_addr = inet_addr("localhost");
    addr.sin_port        = htons(1234);
    addr.sin_family      = AF_INET;
 
    do
    {
        cout << "Connect to Masterserver? [ENTER]" <<endl;
        getchar();
 
        RetVal = connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));
        if (RetVal == 0) break;
 
        MessageBoxA(NULL, "Could not connect to server", "Error", MB_OK | MB_ICONERROR);
    }
    while (true);
 
    char cID[64];
    ZeroMemory(cID, 64);
 
    recv(sConnect, cID, 64, NULL);
    int ID = atoi(cID);
 
    cout << "Connected" << endl;
    cout << "You are Client No: " << ID << endl;
 
    CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE) ClientThread, NULL, NULL, NULL);
 
    for(;; Sleep(10))
    {
        string buffer;
        getline(cin, buffer);
        if (send(sConnect, buffer.c_str(), buffer.length(), NULL) < 1)
 
 exit(1);
 
    }
 
    return 0;
}
aide moi SVP merci