1
0

Serveur.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "Serveur.h"
  2. Serveur::Serveur(int port, int nbrClient): m_port(port), m_nbrClient(nbrClient)
  3. {
  4. #ifdef WIN32
  5. m_erreur = WSAStartup(MAKEWORD(2,2), &m_WSAData);
  6. #else
  7. m_erreur = 0;
  8. #endif // WIN32
  9. m_sock.recsize = sizeof(m_sock.sin);
  10. m_csock = new Sock[m_nbrClient];
  11. for(int i = 0;i<m_nbrClient;i++)
  12. {
  13. m_csock[i].recsize = sizeof(m_csock[i].sin);
  14. }
  15. }
  16. Serveur::~Serveur()
  17. {
  18. #ifdef WIN32
  19. WSACleanup();
  20. #endif // WIN32
  21. std::cout << "Fermeture des sockets clientes" << std::endl;
  22. for(int i = 0;i<m_nbrClient;i++)
  23. closesocket(m_csock[i].sock);
  24. std::cout << "Fermeture de la socket serveur" << std::endl;
  25. closesocket(m_sock.sock);
  26. std::cout << "Fermeture du serveur termine" << std::endl;
  27. }
  28. bool Serveur::rendreUtilisable()
  29. {
  30. if(!m_erreur)
  31. {
  32. m_sock.sock = socket(AF_INET, SOCK_STREAM, 0);// SOCK_STREAM pour TCP/IP, SOCK_DGRAM pour UDP/IP
  33. if(m_sock.sock != INVALID_SOCKET)
  34. {
  35. std::cout << "La socket " << m_sock.sock << " est maintenant ouverte en mode TCP/IP" << std::endl;
  36. m_sock.sin.sin_addr.s_addr = htonl(INADDR_ANY);
  37. m_sock.sin.sin_family = AF_INET;
  38. m_sock.sin.sin_port = htons(m_port);
  39. if(bind(m_sock.sock, (SOCKADDR*)&m_sock.sin, m_sock.recsize) != SOCKET_ERROR)
  40. {
  41. std::cout << "Listage du port " << m_port << "..." << std::endl;
  42. if(listen(m_sock.sock, 5) != SOCKET_ERROR)
  43. {
  44. std::cout << "Patientez pendant que les " << m_nbrClient << " clients se connectent sur le port " << m_port << "..." << std::endl;
  45. for(int i = 0;i<m_nbrClient;i++)
  46. {
  47. m_csock[i].sock = accept(m_sock.sock, (SOCKADDR*)&m_csock[i].sin, &m_csock[i].recsize);// renvoi INVALID_SOCKET si il y a un probleme
  48. if(m_csock[i].sock != INVALID_SOCKET)
  49. {
  50. std::cout << "Un clien se connect avec la socket " << m_csock[i].sock << " de " << inet_ntoa(m_csock[i].sin.sin_addr) << " : " << htons(m_csock[i].sin.sin_port) << std::endl;
  51. }
  52. else
  53. return false;
  54. }
  55. }
  56. else
  57. return false;
  58. }
  59. else
  60. return false;
  61. }
  62. else
  63. return false;
  64. }
  65. else
  66. return false;
  67. return true;
  68. }
  69. void Serveur::envoyer(void* donnee, int indice, int tailleByte)
  70. {
  71. if(tailleByte != 0)
  72. send(m_csock[indice].sock, (char*)donnee, tailleByte, 0);
  73. else
  74. send(m_csock[indice].sock, (char*)donnee, sizeof(donnee), 0);
  75. }
  76. void Serveur::recevoir(void* donnee, int indice, int tailleByte)
  77. {
  78. if(tailleByte != 0)
  79. recv(m_csock[indice].sock, (char*)donnee, tailleByte, 0);
  80. else
  81. recv(m_csock[indice].sock, (char*)donnee, sizeof(donnee), 0);
  82. }