Page principale | Liste des namespaces | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Membres des namespaces | Composants | Déclarations

# ExCSocket.cpp

Aller à la documentation de ce fichier.

    00001 // Socket.cpp: implementation of the ExCSocket class.
    00002 //
    00004 #include "ExCSocket.h"
    00006 // Construction/Destruction
    00008
    00009 ExCSocket::ExCSocket(){Guard(ExCSocket::ExCSocket())
    00010 #if _WIN32
    00011         WSADATA WSAData;
    00012         WSAStartup(MAKEWORD(2,0), &WSAData);
    00013 #endif
    00014         SetSocketPort(5000);
    00015         m_hSocket=socket(AF_INET,SOCK_STREAM,0);
    00016         if(m_hSocket==-1)
    00017         {
    00018                 std::cout<<"Socket error"<<std::endl;
    00019         }
    00020 UnGuard
    00021 }
    00022
    00023 ExCSocket::ExCSocket(int Handler)
    00024 {
    00025 Guard(ExCSocket::ExCSocket(int handler))
    00026         SetHandler(Handler);
    00027 UnGuard
    00028 }
    00029
    00030 ExCSocket::ExCSocket(int * Handler)
    00031 {
    00032 Guard(ExCSocket::ExCSocket(int handler))
    00033         SetHandler(*Handler);
    00034 UnGuard
    00035 }
    00036
    00037 ExCSocket::ExCSocket(ExCSocket& socket)
    00038 {
    00039 Guard(ExCSocket::ExCSocket(ExCSocket& socket))
    00040         SetSocketPort(socket.GetSocketPort());
    00041         SetHandler(socket.GetHandler());
    00042         SetIp(socket.GetIp());
    00043 UnGuard
    00044 }
    00045
    00046 ExCSocket::ExCSocket(int domaine,int protocole)
    00047 {
    00048 Guard(ExCSocket::ExCSocket(int domaine,int protocole))
    00049         m_hSocket=socket(domaine,protocole,0);
    00050         if(m_hSocket==-1)
    00051         {
    00052                 std::cout<<"Erreur socket"<<std::endl;
    00053         }
    00054 UnGuard
    00055 }
    00056
    00057 ExCSocket::~ExCSocket()
    00058 {
    00059 Guard(ExCSocket::~ExCSocket())
    00060         Close();
    00061 UnGuard
    00062 }
    00063
    00065 // Function
    00067
    00068 void ExCSocket::Init(void)
    00069 {
    00070 Guard(ExCSocket::Init(void))
    00071 #if _WIN32
    00072         //m_AdresseSocket.sin_addr.s_addr                       = inet_addr("192.168.0.1");
    00073         m_AdresseSocket.sin_addr.s_addr                 = inet_addr("80.200.104.109");
    00074 #else
    00075         //if((m_pInfosHost=gethostbyname("lore.starfleet.org"))==0)
    00076         if((m_pInfosHost=gethostbyname("hobbes.homelinux.net"))==0)
    00077         {
    00078                 Trace("Erreur d'aquisition sur le host");
    00079                 EXC
    00080         }
    00081         memcpy(&m_AdresseIP,m_pInfosHost->h_addr,m_pInfosHost->h_length);
    00082         SetIp(inet_ntoa(m_AdresseIP));
    00083         memset(&m_AdresseSocket,0,sizeof(struct sockaddr_in));
    00084         memcpy(&m_AdresseSocket.sin_addr,m_pInfosHost->h_addr,m_pInfosHost->h_length);
    00085 #endif
    00086         m_AdresseSocket.sin_family= AF_INET;//le domaine
    00087         m_AdresseSocket.sin_port=htons(m_Port);
    00088 UnGuard
    00089 }
    00090
    00091
    00092 void ExCSocket::Close(void){Guard(void ExCSocket::Close(void))
    00093 #if _WIN32
    00094         closesocket(m_hSocket);
    00095         WSACleanup();
    00096 #else
    00097         close(m_hSocket);
    00098 #endif
    00099 UnGuard
    00100 }
    00101
    00102 void ExCSocket::SendMessage(ExCMessage &OutMessage){Guard(void ExCSocket::SendMessage(ExCMessage OutMessage))
    00103         char *buffer;
    00104         buffer= new char[500];
    00105         sprintf(buffer,"%d#%s%s",OutMessage.GetType(),OutMessage.GetMessage().data(),"\0");
    00106         if(send(m_hSocket,buffer,500,0)==-1)
    00107         {Trace(Error on send Message)}
    00108 UnGuard
    00109 }
    00110
    00111 void ExCSocket::RecvMessage(ExCMessage &InMessage)
    00112 {
    00113 Guard(void ExCSocket::RecvMessage(ExCMessage InMessage))
    00114
    00115         char * buffer;
    00116         buffer = new char[500];
    00117         if(recv(m_hSocket,buffer,500,0)==-1)
    00118         {
    00119                 Trace(Error on Recv Message)
    00120         }
    00121         //extract type
    00122         std::string bufftype;
    00123         for(unsigned int i=0;i<4;i++)
    00124         {
    00125                 bufftype=bufftype+buffer[i];
    00126         }
    00127         InMessage.SetType(atoi(bufftype.data()));
    00128         //extract msg
    00129         std::string  bufferMsg;
    00130         for(unsigned int j=5;j<500;j++)
    00131         {
    00132                 bufferMsg=bufferMsg+buffer[j];
    00133                 if(strcmp("\0",&buffer[j])==0)break;
    00134         }
    00135         InMessage.SetMessage(bufferMsg);
    00136 UnGuard
    00137 }
    00138