Partage
  • Partager sur Facebook
  • Partager sur Twitter

[C++] (wsock32) Envoie/Reception données textuelles

    4 mars 2008 à 22:49:17

    Bonsoir à tous,
    Je me pose beaucoup de question quant à l'utilisation des sockets.
    Je me suis inspiré d'une documentation et d'un tuto sur le siteduzero, mais je ne parviens pas à mes fins.

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


    (Précision: MY_SOCKET_ERROR vaut -1)

    Que j'utilise coté serveur comme suit:
    1. int main(int argc, char *argv[])
    2. {
    3.     int quit = 0;
    4.     int i = 0;
    5.         std::cout << "Gestion des connexions au serveur" << std::endl;
    6.         SocketGestion MySocket = SocketGestion::SocketGestion();
    7.         MySocket.SocketGestion::ListenSocket();
    8.         MySocket.SocketGestion::AcceptSocket();
    9.         std::cin >> quit;
    10.         return EXIT_SUCCESS;
    11. }


    J'ai le résultat suivant:
    Error: send, int error: -1


    D'abord petite question:
    Dans la documentation, il est indiqué qu'il y a plusieurs erreur possibles exemple:

    Citation : Documentation GNU


    The following errno error conditions are defined for this function:

    EBADF
    The socket argument is not a valid file descriptor.
    EINTR
    The operation was interrupted by a signal before any data was sent. See Interrupted Primitives.
    ENOTSOCK
    The descriptor socket is not a socket.
    EMSGSIZE
    The socket type requires that the message be sent atomically, but the message is too large for this to be possible.
    EWOULDBLOCK
    Nonblocking mode has been set on the socket, and the write operation would block. (Normally send blocks until the operation can be completed.)
    ENOBUFS
    There is not enough internal buffer space available.
    ENOTCONN
    You never connected this socket.
    EPIPE
    This socket was connected but the connection is now broken. In this case, send generates a SIGPIPE signal first; if that signal is ignored or blocked, or if its handler returns, then send fails with EPIPE.


    Comment savoir a quelle erreur correspond la valeur -1 renvoyée ?

    J'ai un problème au niveau de la fonction send...
    Est-ce parce que je travaille en local ?
    Est-ce parce que je ne l'envoie pas là où il faut ? (mauvaise socket ou configuration de socket ?)

    Merci à tous & bonne soirée.
    BloodyDark.
    • Partager sur Facebook
    • Partager sur Twitter
      4 mars 2008 à 22:57:25

      Bah d'abord essaie de change les phrase qui s'affichent pour chaque erreur, afin deja de voir dans quel partie se provoque l'erreur :
      std::cout << "Envoi Donnee : Error: send, int error: " << getError << std::endl;
      J'ai mis en rouge le bout de phrase que j'ai ajouté pour te donner un exemple
      • Partager sur Facebook
      • Partager sur Twitter
        4 mars 2008 à 22:59:54

        Et bien mes phrases sont différentes, là je sais qu'il y a une erreur sur la fonction send(), d'où le message 'Error: send', si ça aurait été sur l'acceptation, j'aurai eu 'Error: accept'.

        ^^
        • Partager sur Facebook
        • Partager sur Twitter
          4 mars 2008 à 23:17:38

          Ah oui c'est vrai mdr, l'erreur doit surement se trouver dans cette ligne, va voir on prototype ptet :
          getError = send(this->SG_socket, TextValue, sizeof(TextValue), 0);
          • Partager sur Facebook
          • Partager sur Twitter
            4 mars 2008 à 23:20:22

            Non, l'erreur est que la socket par laquelle le serveur renvoi au client n'est pas SG_SOCKET, mais c'est la socket renvoyée par la fonction accept...

            Autrement dit, côté serveur, tu dois avoir deux sockets...

            Dans tout les cas, tu dois récupérer la valeur de retour de accept(), et l'utiliser dans send() côté serveur...

            EDIT: dans la doc de MS:
            http://msdn2.microsoft.com/en-us/library/ms737526(VS.85).aspx
            • Partager sur Facebook
            • Partager sur Twitter
              4 mars 2008 à 23:26:40

              Merci beaucoup, j'en étais quasi sûr, je teste ça, merci ^^

              EDIT: J'aurai dû me fier à ce que j'avais compris !

              En tout cas merci beaucoup pour cette réponse ;)
              Petite question bonus...
              S'il y a plusieurs personnes qui se connectent, ... faut-il une socket 'connection' pour chaque client ? :euh:
              • Partager sur Facebook
              • Partager sur Twitter

              [C++] (wsock32) Envoie/Reception données textuelles

              × 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