SocketTCP Class Reference

Implémentation d'une socket TCP. More...

#include <SocketTCP.h>

Inheritance diagram for SocketTCP:

Inheritance graph
[legend]
Collaboration diagram for SocketTCP:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SocketTCP ()
 SocketTCP (int socket)
 Constructeur par recopie, utilisé seulement par la méthode Accept afin de construire une nouvelle socket à partir du résultat de la primitive TCP accept().
 SocketTCP (unsigned int port) throw (SocketException)
 Construction typique d'une scoket utilisée par un serveur.
 SocketTCP (string ad, unsigned int port) throw (SocketException)
 Construction typique d'une scoket utilisée par un client.
 SocketTCP (Socket *s)
 Constructeur par copie d'un pointeur sur l'objet de base Socket.
 SocketTCP (const SocketTCP &s)
 Constructeur par recopie.
void operator= (const SocketTCP &s)
 Surcharge de = (Contructeur par recopie).
 ~SocketTCP ()
SocketAccept () throw (SocketException)
 Méthode bloquante attendant la connexion d'un client TCP Usage typique: Socket *s=new SocketTCP((unsigned int)port); Socket *ecoute=s->Accept(); //PENSER au delete car un new SocketTCP est fait dans cette méthode.
void Envoyer (string msg) const throw (SocketException)
 Envoie du message msg via la socket.
string Ecouter () throw (SocketException)
 Attent un message de l'extrémité de la socket et le renvoie.

Detailed Description

Implémentation d'une socket TCP.

Definition at line 36 of file SocketTCP.h.


Constructor & Destructor Documentation

SocketTCP::SocketTCP (  )  [inline]

Definition at line 38 of file SocketTCP.h.

Referenced by Accept().

00038 : Socket() {}

SocketTCP::SocketTCP ( int  socket  ) 

Constructeur par recopie, utilisé seulement par la méthode Accept afin de construire une nouvelle socket à partir du résultat de la primitive TCP accept().

Parameters:
socket 
Returns:

Definition at line 23 of file SocketTCP.cpp.

References Socket::mSocket.

00023                               {
00024         mSocket=socket;
00025 }

SocketTCP::SocketTCP ( unsigned int  port  )  throw (SocketException)

Construction typique d'une scoket utilisée par un serveur.


Ecoute toutes les interfaces locales

Parameters:
port Port d'écoute
Returns:

Definition at line 27 of file SocketTCP.cpp.

00027                                                              : Socket(port){
00028         int retour;
00029         struct sockaddr_in adresse;
00030         mSocket=socket(AF_INET,SOCK_STREAM,0);
00031         if (mSocket<0) throw(SocketException(1));
00032         adresse.sin_family=AF_INET;
00033         adresse.sin_port = htons((u_short)port);
00034         adresse.sin_addr.s_addr=INADDR_ANY;
00035         retour = bind(mSocket,(struct sockaddr *)&adresse,sizeof(adresse));
00036         if (retour<0) throw(SocketException(2));
00037         listen(mSocket,7);
00038 }

SocketTCP::SocketTCP ( string  ad,
unsigned int  port 
) throw (SocketException)

Construction typique d'une scoket utilisée par un client.


Se connecte sur ad:port

Parameters:
ad Adresse distante
port Port distant
Returns:

Definition at line 40 of file SocketTCP.cpp.

00040                                                                         : Socket(ad,port){
00041         struct sockaddr_in adresse;
00042         struct hostent *recup;
00043         mSocket=socket(AF_INET, SOCK_STREAM, 0);
00044         if (mSocket < 0) throw(SocketException(1));
00045         recup = gethostbyname(ad.c_str());
00046         if (recup == NULL) throw(SocketException(3));
00047         memcpy((char *)&adresse.sin_addr, (char *)recup->h_addr, recup->h_length);
00048         adresse.sin_family = AF_INET;
00049         adresse.sin_port = htons((u_short) port);
00050         if (connect(mSocket, (struct sockaddr *)&adresse, sizeof(adresse)) == -1) throw(SocketException(4));
00051 }

SocketTCP::SocketTCP ( Socket s  ) 

Constructeur par copie d'un pointeur sur l'objet de base Socket.

Parameters:
s Socket*
Returns:

Definition at line 63 of file SocketTCP.cpp.

00063                               : Socket(s){
00064 }

SocketTCP::SocketTCP ( const SocketTCP s  ) 

Constructeur par recopie.

Parameters:
s Objet de base
Returns:

Definition at line 67 of file SocketTCP.cpp.

References Socket::mCryptage, and Socket::mSocket.

00067                                       {
00068         mSocket=s.mSocket;
00069         mCryptage=s.mCryptage;  
00070 }

SocketTCP::~SocketTCP (  ) 

Definition at line 77 of file SocketTCP.cpp.

References Socket::mSocket.

00077                      {
00078         close(mSocket);
00079 }


Member Function Documentation

void SocketTCP::operator= ( const SocketTCP s  ) 

Surcharge de = (Contructeur par recopie).

Parameters:
s Objet de base

Definition at line 72 of file SocketTCP.cpp.

References Socket::mCryptage, and Socket::mSocket.

00072                                            {
00073         mSocket=s.mSocket;
00074         mCryptage=s.mCryptage;  
00075 }

Socket * SocketTCP::Accept (  )  throw (SocketException) [virtual]

Méthode bloquante attendant la connexion d'un client TCP Usage typique: Socket *s=new SocketTCP((unsigned int)port); Socket *ecoute=s->Accept(); //PENSER au delete car un new SocketTCP est fait dans cette méthode.

..

Returns:
Socket*

Implements Socket.

Definition at line 53 of file SocketTCP.cpp.

References Socket::mSocket, and SocketTCP().

00053                                                 {
00054         int socket;
00055         struct sockaddr_in socketEntrante;
00056         socklen_t taille=sizeof(socketEntrante);
00057         socket=accept(mSocket,(struct sockaddr*) &socketEntrante,&taille);
00058         if(socket==-1) throw(SocketException(5));
00059         Socket *s=new SocketTCP(socket);
00060         return s;
00061 }

Here is the call graph for this function:

void SocketTCP::Envoyer ( string  msg  )  const throw (SocketException) [virtual]

Envoie du message msg via la socket.

Parameters:
msg Le message à envoyer

Implements Socket.

Definition at line 81 of file SocketTCP.cpp.

00081                                                               {
00082         int s=send(mSocket,(mCryptage->Crypter(msg)).c_str(),msg.length(),0);
00083         if(s==-1){
00084                 throw(SocketException(6));
00085         }
00086 }

string SocketTCP::Ecouter (  )  throw (SocketException) [virtual]

Attent un message de l'extrémité de la socket et le renvoie.

Returns:
Le message reçu

Implements Socket.

Definition at line 88 of file SocketTCP.cpp.

References Cryptage::Decrypter(), Socket::mCryptage, and Socket::mSocket.

00088                                                 {
00089         char buffer[BUFSIZ];
00090         int s=recv(mSocket,buffer,BUFSIZ,0);
00091         if (s==-1) throw(SocketException(7));
00092         else if(s==0) throw(SocketException(9));
00093         else {
00094                 buffer[s]=0;
00095                 return mCryptage->Decrypter(buffer);
00096         }       
00097 }

Here is the call graph for this function:


The documentation for this class was generated from the following files:
Generated on Wed Jan 2 14:02:13 2008 for Pacman by  doxygen 1.5.1