Client.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "Client.h"
  2. Client::Client(std::string IP, int port): m_IP(IP), m_port(port)
  3. {
  4. m_csock.recsize = sizeof(m_csock.sin);
  5. std::cout << "-°*,-Programme Client-,*°-" << std::endl;
  6. std::cout << "Initialisation..." << std::endl;
  7. #ifdef WIN32
  8. m_erreur = WSAStartup(MAKEWORD(2,2), &m_WSAData);
  9. #else
  10. m_erreur = 0;
  11. #endif // WIN32
  12. }
  13. Client::~Client()
  14. {
  15. std::cout << "Fermeture de la socket client" << std::endl;
  16. closesocket(m_csock.sock);
  17. std::cout << "Fermeture du client termine" << std::endl;
  18. #ifdef WIN32
  19. WSACleanup();
  20. #endif // WIN32
  21. }
  22. bool Client::rendreUtilisable()
  23. {
  24. if(!m_erreur)
  25. {
  26. m_csock.sock = socket(AF_INET,SOCK_STREAM, 0);
  27. if(m_csock.sock != INVALID_SOCKET)
  28. {
  29. std::cout << "La socket " << m_csock.sock << " est maintenant ouverte en mode TCP/IP" << std::endl;
  30. m_csock.sin.sin_addr.s_addr = inet_addr(m_IP.c_str());
  31. m_csock.sin.sin_family = AF_INET;
  32. m_csock.sin.sin_port = htons(m_port);
  33. if(connect(m_csock.sock, (SOCKADDR*)&m_csock.sin, m_csock.recsize) != SOCKET_ERROR)
  34. {
  35. std::cout << "Connection a " << inet_ntoa(m_csock.sin.sin_addr) << " sur le port " << htons(m_csock.sin.sin_port) << " reussie" << std::endl;
  36. }
  37. else
  38. return false;
  39. }
  40. else
  41. return false;
  42. }
  43. else
  44. return false;
  45. return true;
  46. }
  47. void Client::envoyer(void* donnee, int tailleByte)
  48. {
  49. if(tailleByte != 0)
  50. send(m_csock.sock, (char*)donnee, tailleByte, 0);
  51. else
  52. send(m_csock.sock, (char*)donnee, sizeof(donnee), 0);
  53. }
  54. void Client::recevoir(void* donnee, int tailleByte)
  55. {
  56. if(tailleByte != 0)
  57. recv(m_csock.sock, (char*)donnee, tailleByte, 0);
  58. else
  59. recv(m_csock.sock, (char*)donnee, sizeof(donnee), 0);
  60. }