Partage
  • Partager sur Facebook
  • Partager sur Twitter

[C++] (wsock32 & PTthread) Thread & Socket

    5 mars 2008 à 22:18:51

    Bonsoir à tous !
    Je m'exerce dans le but de créer une application client/serveur basique (dans le but de mieux maitriser les sockets et le C++).

    Je suis confronter à un petit problème, je ne suis même pas sûr de ma façon de procéder.

    J'ai ici ma classe concernant la gestion de socket, que voici:
    1. class SocketGestion {
    2.         public:
    3.                 SocketGestion();
    4.                 void ConfigurationSocket();
    5.                 void ConnectSocket();
    6.                 void ListenSocket();
    7.                 void AcceptSocket();
    8.                 bool GetData();
    9.                 void SendData();
    10.         private:
    11.                 SOCKET SG_socket;
    12.                 SOCKADDR_IN SG_sin;
    13.                 SOCKET SG_client;
    14.                 SOCKADDR_IN SG_sinCli;
    15. };
    16. SocketGestion::SocketGestion() {
    17.     int getError = 0;
    18.         WSADATA WSAData;
    19.     WSAStartup(MAKEWORD(2,0), &WSAData);
    20.         getError = socket(AF_INET, SOCK_STREAM, 0);
    21.         if (getError != MY_SOCKET_ERROR) {
    22.                 this->SG_socket = getError;
    23.                 std::cout << "Creation de Socket... OK [ " << this->SG_socket << " ] " << std::endl;
    24.                 this->ConfigurationSocket();
    25.         } else {
    26.                 std::cout << "Erreur: socket, int error: " << getError << std::endl;
    27.         }
    28. }
    29. void SocketGestion::ConfigurationSocket() {
    30.     int getError = 0;
    31.     this->SG_sin.sin_addr.s_addr = htonl(INADDR_ANY);
    32.     this->SG_sin.sin_family = AF_INET;
    33.     this->SG_sin.sin_port = htons(665);
    34.     getError = bind(this->SG_socket, (SOCKADDR *)&this->SG_sin, sizeof(this->SG_sin));
    35.     if (getError != MY_SOCKET_ERROR) {
    36.         std::cout << "Initialisation de la Socket... OK ! \nConfiguration de la socket... OK \n" << std::endl;
    37.     } else {
    38.         std::cout << "Erreur: bind, int error:" << getError << std::endl;
    39.     }
    40. }
    41. void SocketGestion::ConnectSocket() {
    42.     int getError = 0;
    43.     std::cout << "Connexion en cours..." << std::endl;
    44.     std::cout << "Au port..." << htons(this->SG_sin.sin_port) << std::endl;
    45.     getError = connect(this->SG_socket, (SOCKADDR *)&this->SG_sin, sizeof(this->SG_sin));
    46.     if (getError != MY_SOCKET_ERROR) {
    47.         std::cout << "Connexion au serveur " << inet_ntoa(this->SG_sin.sin_addr) << "reussie" << std::endl;
    48.     } else {
    49.         std::cout << "Erreur: connect, int error: " << getError << " (IP:" << inet_ntoa(this->SG_sin.sin_addr) << ")" <<std::endl;
    50.     }
    51. }
    52. void SocketGestion::ListenSocket() {
    53.     int getError = 0;
    54.     getError = listen(this->SG_socket, 5);
    55.     if (getError != MY_SOCKET_ERROR) {
    56.         std::cout << "Ecoute du port " << htons(this->SG_sin.sin_port) << "... EN COURS..." << std::endl;
    57.     } else {
    58.         std::cout << "Error: listen, int error: " << getError << std::endl;
    59.     }
    60. }
    61. void SocketGestion::AcceptSocket() {
    62.     int getError = 0;
    63.     int SG_sinSize = sizeof(this->SG_sin);
    64.     getError = accept(this->SG_socket, (SOCKADDR *)&this->SG_sin, &SG_sinSize);
    65.     if (getError != MY_SOCKET_ERROR) {
    66.         std::cout << "Connexion reussie avec: " << inet_ntoa(this->SG_sin.sin_addr) << " par le port: " << htons(this->SG_sin.sin_port) << std::endl;
    67.         this->SG_client = getError;
    68.         this->SendData();
    69.     }
    70. }
    71. void SocketGestion::SendData() {
    72.     int getError = 0;
    73.     char TextValue[200] = "Ohayo !";
    74.     std::cout << sizeof(TextValue) << std::endl;
    75.     getError = send(this->SG_client, TextValue, sizeof(TextValue), 0);
    76.     if (getError != MY_SOCKET_ERROR) {
    77.         std::cout << "Message envoye !" << std::endl;
    78.     } else {
    79.         std::cout << "Error: send, int error: " << getError << std::endl;
    80.     }
    81. }
    82. bool SocketGestion::GetData() {
    83.     char receiveValue[512] = "";
    84.     char quit[512] = "/quit";
    85.     int getError = 0;
    86.     getError = recv(this->SG_client, receiveValue, sizeof(receiveValue), 0);
    87.     if (getError != MY_SOCKET_ERROR) {
    88.         std::cout << "Recu:" << receiveValue << std::endl;
    89.     }
    90.     if (quit == receiveValue)
    91.         return false;
    92.     else
    93.         return true;
    94. }


    Ce que je veux faire, c'est pouvoir connecter plusieurs clients au serveur.
    Voilà comment je voyais le fonctionnement de l'appli serveur:
    - On écoute le port concerné (665 ici).
    - On crée un thread qui est en attente d'acceptation (fonction socket accept())

    Ce que j'ai donc essayé de faire...
    Mais je pense que je me suis perdu...
    Voir même, c'est peut-être la fonction listen de wsock32 qui gère le nombre de connexion possible... SI vous pouvez m'éclairez... merci :D

    1. void *getConnexion(void *thread_data) {
    2.     SocketGestion::SocketGestion *MySocket1 = (SocketGestion*) &thread_data;
    3.     while (1) {
    4.         MySocket1->AcceptSocket();
    5.     }
    6. }
    7. int main(int argc, char *argv[])
    8. {
    9.     // Création ID du Thread
    10.     pthread_t getConnex;
    11.     SocketGestion MySocket;
    12.     SocketGestion *MySocketData = &MySocket;
    13.     std::cout << "Gestion des connexions au serveur" << std::endl;
    14.     MySocket.SocketGestion::ListenSocket();
    15.     pthread_create(&getConnex, NULL, getConnexion, (void *) &MySocketData);
    16.     pthread_join(getConnex, NULL);
    17.     return EXIT_SUCCESS;
    18. }



    EDIT#:
    Je ne comprends pas...
    Apparement mon thread bloque la fonction main... :euh:
    J'pensais que les threads avaient justement pour but de ne pas faire ça... je m'y prends mal ? ^^
    • Partager sur Facebook
    • Partager sur Twitter

    [C++] (wsock32 & PTthread) Thread & Socket

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown