Exemple qui devrait marcher :
1. #include <winsock2.h>
2.
3. #define SON_NUMERO_DE_PORT_OU_ENVOYER 1434
4. #define ADRESSE_IP_DE_LA_CIBLE "127.0.0.1"
5. #define TON_MESSAGE_A_BALANCER "Bla bla..."
6.
7. int main(int argc, char* argv[])
8. {
9. WSADATA xWs;
10. struct sockaddr_in xMySocket; //Un jour faudra trouver une traduction valable pour "socket"...
11. struct sockaddr_in xHisSocket;
12. int nRet;
13. int nSockID = -1;
14.
15. nRet = WSAStartup( 0x0101, &xWs );
16. if (nRet != 0)
17. {
18. perror("Houla, pas moyen d'initialiser les fonctions réseaux ?!?");
19. return 1;
20. }
21.
22. nRet = (nSockID = socket(AF_INET, SOCK_DGRAM, 0));
23. if (nRet == -1)
24. {
25. perror("Erreur de denomination de socket...");
26. return 1;
27. }
28.
29. xMySocket.sin_family = AF_INET;
30. xMySocket.sin_port = htons( -1 );
31. xMySocket.sin_addr.s_addr = INADDR_ANY;
32. ZeroMemory(&(xMySocket.sin_zero), 8);
33.
34. nRet = bind( nSockID, (struct sockaddr *)&xMySocket, sizeof(xMySocket) );
35. if (nRet != 0)
36. {
37. perror("Erreur de reservation du port...");
38. return 1;
39. }
40.
41. xHisSocket.sin_family = AF_INET;
42. xMySocket.sin_port = htons( SON_NUMERO_DE_PORT_OU_ENVOYER );
43. xMySocket.sin_addr.s_addr = inet_addr( ADRESSE_IP_DE_LA_CIBLE );
44. ZeroMemory(&(xMySocket.sin_zero), 8);
45.
46. nRet = sendto( nSockID, &TON_MESSAGE_A_BALANCER, strlen(TON_MESSAGE_A_BALANCER), 0, (struct sockaddr *)&xHisSocket, sizeof(xHisSocket) );
47. if (nRet == SOCKET_ERROR)
48. {
49. perror("Ben, l'envoi du message a echoue, pas de chance...");
50. return 1;
51. }
52. else
53. {
54. printf( "Envoye les %d octets du message...n", nRet );
55. return 0;
56. }
57. }
(Edit pour désactiver ces p*ains de smileys dans le code... )