Bonjour,
Je programme sous Visual C++ et je souhaite établir une connection UDP entre deux pc via une liaison Wifi. J'ai essayé avec Winsock2 mais ca ne marche pas en Wifi (réseau ad hoc). Par contre j'ai testé en LAN la connexion s'établit parfaitement.
Est ce qu'il est impossible d'établir la connection en Wifi en utilisant Winsock2? Pouvez vous m'aider svp?
Merci d'avance.
voici le code que j'ai implémenté pour le serveur.
voici le code que j'ai implémenter pour le client.Code:
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 // IAMNet.cpp : définit le point d'entrée pour l'application console. // #include "stdafx.h" #include "IAMNet.h" #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") #ifdef _DEBUG #define new DEBUG_NEW #endif // Seul et unique objet application CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // Initialise MFC et affiche un message d'erreur en cas d'échec if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO : modifiez le code d'erreur selon les besoins _tprintf(_T("Erreur irrécupérable : l'initialisation MFC a échoué\n")); nRetCode = 1; } else { // TODO : codez le comportement de l'application à cet emplacement. WSADATA WSAData; SOCKET sock; SOCKADDR_IN clientsin; SOCKET csock; SOCKADDR_IN sin; SOCKADDR_IN csin; CHAR buff; WSAStartup(MAKEWORD(2,0), &WSAData); cout<<"hello\n\r"; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_family = AF_INET; sin.sin_port = htons(50014); sock = socket(AF_INET,SOCK_STREAM,0); bind(sock, (SOCKADDR *)&sin, sizeof(sin)); gethostname(&buff, sizeof(buff)); listen(sock, 0); int val = 0; while(1) { int sinsize = sizeof(csin); if((csock = accept(sock, (SOCKADDR *)&csin, &sinsize)) != INVALID_SOCKET) { int size = sizeof(clientsin); int succes = getpeername(csock,(SOCKADDR *)&clientsin, &size); int a = WSAGetLastError(); send(csock, "Hello world!\r\n", 30, 0); cout<<"client connected \n\r"; } } } return nRetCode; }
Code:
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 // iamclient.cpp : définit le point d'entrée pour l'application console. // #include "stdafx.h" #include "iamclient.h" #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") #ifdef _DEBUG #define new DEBUG_NEW #endif // Seul et unique objet application CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // Initialise MFC et affiche un message d'erreur en cas d'échec if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO : modifiez le code d'erreur selon les besoins _tprintf(_T("Erreur irrécupérable : l'initialisation MFC a échoué\n")); nRetCode = 1; } else { // TODO : codez le comportement de l'application à cet emplacement. WSADATA WSAData; WSAStartup(MAKEWORD(2,0), &WSAData); SOCKET sock; SOCKADDR_IN sin; char *buff = new char[255]; sin.sin_addr.s_addr = inet_addr("180.0.0.2"); sin.sin_family = AF_INET; sin.sin_port = htons(4148); sock = socket(AF_INET,SOCK_STREAM,0); bind(sock, (SOCKADDR *)&sin, sizeof(sin)); connect(sock, (SOCKADDR *)&sin, sizeof(sin)); while(1) { recv(sock, buff, sizeof(buff),0); cout<<buff; } } return nRetCode; }