Test.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Pierre Schmitt, Ludovic Giacomello, Nhi Ly   *
00003  *      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef TEST_H
00022 #define TEST_H
00023 
00024 
00025 #include <iostream>
00026 #include <cstdlib>
00027 
00028 #include "Socket.h"
00029 #include "SocketUDP.h"
00030 #include "SocketTCP.h"
00031 
00035 class Test{
00036 public:
00037         Test(){}
00038         void TestUDP() const{
00039                 cout<<"Test Client/Serveur UDP (fork)"<<endl;
00040                 unsigned int port=7777;
00041                 if(fork()){
00042                         //Père (SERVEUR)
00043                         ServeurUDP(port);
00044                 } else {
00045                         //Fils (CLIENT)
00046                         sleep(5); //On laisse le temps au serveur de se lancer
00047                         ClientUDP(port);
00048                 }
00049         }
00050 
00051         void TestTCP() const{
00052                 cout<<"Test Client/Serveur TCP (fork)"<<endl;
00053                 unsigned int port=7777;
00054                 if(fork()){
00055                         //Père (SERVEUR)
00056                         ServeurTCP(port);
00057                 } else {
00058                         //Fils (CLIENT)
00059                         sleep(5); //On laisse le temps au serveur de se lancer
00060                         ClientTCP(port);
00061                 }
00062         }
00063 private:
00064         //UDP
00065         void ClientUDP(unsigned int port) const{
00066                 Socket *c=new SocketUDP("localhost",(unsigned int)port);
00067                 cout<<"Client: >>:Bonjour"<<endl;
00068                 c->Envoyer("Bonjour");
00069                 cout<<"Client: <<: "<<c->Ecouter()<<endl;
00070                 cout<<"Client: >>:Très bien et vous?"<<endl;
00071                 c->Envoyer("Très bien et vous?");
00072                 delete c;
00073         }
00074 
00075         void ServeurUDP(unsigned int port) const{
00076                 Socket *s=new SocketUDP((unsigned int)port); //+bind
00077                 cout<<"Serveur: Attente d'une connexion"<<endl;
00078                 cout<<"Serveur: <<: "<<s->Ecouter()<<endl;
00079                 s->Envoyer("ça va?");
00080                 cout<<"Serveur: >>: ça va?"<<endl;
00081                 cout<<"Serveur: <<: "<<s->Ecouter()<<endl;
00082                 delete s;
00083         }
00084         
00085         //TCP
00086         void ClientTCP(unsigned int port) const{
00087                 Socket *c=new SocketTCP("localhost",(unsigned int)port);
00088                 sleep(1);
00089                 unsigned int i=0;
00090                 while(1){
00091                 cout<<i<<endl;
00092                 cout<<"Client: >>:Bonjour"<<endl;
00093                 c->Envoyer("Bonjour");
00094                 cout<<"Client: <<: "<<c->Ecouter()<<endl;
00095                 cout<<"Client: >>:ien et vous?"<<endl;
00096                 c->Envoyer("vvvvvvvvvvvvvvvvvvvvvvvvvvffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
00097                 i++;
00098                 }
00099                 delete c;
00100         }
00101         void ServeurTCP(unsigned int port) const{
00102                 Socket *s=new SocketTCP((unsigned int)port); //+bind+listen
00103                 cout<<"Serveur: Attente d'une connexion"<<endl;
00104                 Socket *ecoute=s->Accept();
00105                 while(1){
00106                 cout<<"Serveur: <<: "<<ecoute->Ecouter()<<endl;
00107                 ecoute->Envoyer("ça vssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
00108                 cout<<"Serveur: >>: ça va?"<<endl;
00109                 cout<<"Serveur: <<: "<<ecoute->Ecouter()<<endl;
00110                 }
00111                 delete ecoute;
00112                 delete s;
00113         }
00114 
00115 };
00116 
00117 
00118 #endif

Generated on Wed Jan 2 14:01:41 2008 for Pacman by  doxygen 1.5.1