Skip to content

Manipulation des sockets en C et C++

  1. // Declaration socket
  2. WSADATA WSAData;
  3. SOCKET sock;
  4. SOCKADDR_IN sin;
  5. // Initialisation socket
  6. WSAStartup(MAKEWORD(2,0), &WSAData);
  7. sin.sin_addr.s_addr= inet_addr(“127.0.0.1”); // Adresse IP
  8. sin.sin_family= AF_INET;
  9. sin.sin_port= htons(4001);ย  // Port
  10. sock = socket(AF_INET,SOCK_STREAM,0);
  11. bind(sock, (SOCKADDR *)&sin, sizeof(sin));
  12. // Si connection etablie
  13. if (connect(sock, (SOCKADDR *)&sin, sizeof(sin)) == 0)
  14. {
  15. // Declaration socket
  16. WSADATA WSAData;
  17. SOCKET sock;
  18. SOCKADDR_IN sin;
  19. // Initialisation socket
  20. WSAStartup(MAKEWORD(2,0), &WSAData);
  21. sin.sin_addr.s_addrย  ย  ย = inet_addr(“127.0.0.1”); // Adresse IP
  22. sin.sin_familyย  ย  = AF_INET;
  23. sin.sin_portย  ย  ย  ย  = htons(4001);ย  // Port
  24. sock = socket(AF_INET,SOCK_STREAM,0);
  25. bind(sock, (SOCKADDR *)&sin, sizeof(sin));
  26. // Si connection etablie
  27. if (connect(sock, (SOCKADDR *)&sin, sizeof(sin)) == 0)
  28. else
  29. // Fin
  30. closesocket(sock);
  31. WSACleanup();