/* test_socket.cc - Programme de test des sockets. */ /* Simulotron - Programme de simulation de robot {{{ * * Copyright (C) 2005 Nicolas Haller * * Robot APB Team/Efrei 2005. * Web: http://assos.efrei.fr/robot/ * Email: robot AT efrei DOT fr * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ #include "socket/address.hh" #include "socket/socket_server.hh" #include "socket/socket_client.hh" #include "utils/errno_exception.hh" #include // Adress useful Address adr; Address adr2; //Chaine et char de test const std::string strDepart = "J'aime la prog système."; std::string strDest; const char a = 'f'; char b; //threads pthread_t threadId[2]; void * retour; int stateTh[2]; void * testSockServ(void * CaSertARien); void * testSockClient(void * CaSertARien); int main(void) { int ret; ret = pthread_create(& threadId[0], NULL, testSockServ, NULL); if(ret != 0) { std::cout << "CHIER: Echec thread serveur" << std::endl; return EXIT_FAILURE; } ret = pthread_create(& threadId[1], NULL, testSockClient, NULL); if(ret != 0) { std::cout << "CHIER: Echec thread client" << std::endl; return EXIT_FAILURE; } pthread_join(threadId[0], &retour); stateTh[0] = (int) retour; pthread_join(threadId[1], &retour); stateTh[1] = (int) retour; if(stateTh[0] == 0 && stateTh[1] == 0) return 0; else return 1; } void * testSockServ(void * CaSertARien) { try { // Création de la socket serveur; SocketServer sockServ(Address(4242)); // Mise en écoute du socket serveur sockServ.listen(12); // Acceptation d'une connection SocketClient sockFork(sockServ); // Reception d'un message strDest = sockFork.read(); if(strDepart != strDest) { std::cout << "CHIER!! Le message d'origine est altéré en ecrivant du client au serveur!!" << std::endl; std::cout << "Le message d'origine: " << strDepart << std::endl; std::cout << "Message reçu: " << std::endl; return (void *) 1; } // Ecriture du serveur au client sockFork.write(strDepart); // Reception d'un char b = sockFork.getChar(); if(a != b) { std::cout << "CHIER!! Le char d'origine est altéré en ecrivant du client au serveur!!" << std::endl; std::cout << "Le char d'origine: " << a << std::endl; std::cout << "Le char d'arrivé: " << b << std::endl; return (void *) 1; } // Ecriture du serveur vers le client sockFork.putChar(a); } catch(errno_exception & chier) { std::cout << "CHIER !! Une exception a été lancé coté serveur!!" << std::endl; std::cout << chier.what() << std::endl; return (void *) 1; } return (void *) 0; } void * testSockClient(void * CaSertARien) { try { // Création de la socket client SocketClient sockClient(adr); // Demande de connection sockClient.connect(Address(std::string("localhost"), 4242)); // Ecriture du client dans le serveur sockClient.write(strDepart); // Reception du message serveur strDest = sockClient.read(); if(strDepart != strDest) { std::cout << "CHIER!! Le message d'origine est altéré en ecrivant du serveur au client!!" << std::endl; std::cout << "Le message d'origine: " << strDepart << std::endl; std::cout << "Message reçu: " << std::endl; return (void *) 1; } // Ecriture d'un char du client au serveur sockClient.putChar(a); // Reception d'un char b = sockClient.getChar(); if(a != b) { std::cout << "CHIER!! Le char d'origine est altéré en ecrivant du serveur au client!!" << std::endl; std::cout << "Le char d'origine: " << a << std::endl; std::cout << "Le char d'arrivé: " << b << std::endl; return (void *) 1; } } catch(errno_exception & chier) { std::cout << "CHIER !! Une exception a été lancé coté client!!" << std::endl; std::cout << chier.what() << std::endl; return (void *) 1; } return (void *) 0; }