Historique des modifications - Message

Message #5433

Sujet: Lib reseau


Type Date Auteur Contenu
Création du message 04-01-2009 12:30:56 Magun
si tu veut faire un serveur pour plusieurs client il faut accepter le client a chaque boucle via accept(int sock, struct sockaddr *adresse, socklent_t *longueur);
seul problème sa mais le serveur en attente d'une connexion .... ou alors crée un thread pour cette fonction ... ?
alors jais mal coder mon truc :p

#include <iostream>
#include <pthread.h>

#include <CompileConfig.h>

IrrlichtDevice *device;
IGUIListBox *information;
SOCKET sock;
SOCKADDR_IN sain;
SOCKET csock;
SOCKADDR_IN csin;
char buffer[BUFFERSIZE];

void *socketThread(void*);

int main(void)
{
    int width = 600, height = 300;
    SIrrlichtCreationParameters param;
        param.AntiAlias = false;
        param.Bits = 16;
        param.DriverType = EDT_SOFTWARE;
        param.Fullscreen = false;
        param.Stencilbuffer = false;
        param.WindowSize = dimension2d<s32>(width, height);
    device = createDeviceEx(param);
    device->setResizeAble(true);
    device->setWindowCaption(L"serveur v0.01");
    IVideoDriver *driver = device->getVideoDriver();
    IGUIEnvironment *guienv = device->getGUIEnvironment();
    information = guienv->addListBox(rect<s32>(10,10,width-10,height-10),0,0,true);
    #if defined (WIN32)
        WSADATA WSAData;
        int erreur = WSAStartup(MAKEWORD(2,0), &WSAData);
    #else
        int erreur = 0;
    #endif
    sock = socket(AF_INET,SOCK_STREAM,0);
    stringw tx  = L"Socket ";
            tx += sock;
            tx += L" is open with TCP/IP";
    information->addItem(tx.c_str());
    sain.sin_addr.s_addr    = htonl(INADDR_ANY);
    sain.sin_family         = AF_INET;
    sain.sin_port           = htons(PORT);
    erreur = bind(sock, (SOCKADDR *) &sain, sizeof(sain));
            tx  = L"> listen port  ";
            tx += PORT;
            tx += L" ...";
    information->addItem(tx.c_str());
    listen(sock,2);
    information->addItem(L"OK");
    information->addItem(L"serveur is now ready");
    information->addItem(L"");

    pthread_t thread;
    pthread_create(&thread,NULL,socketThread,NULL);

    while(device->run())
    {
        driver->beginScene(true,true,SColor(100,100,100,100));
        guienv->drawAll();
        device->sleep(10,true);
        driver->endScene();
    }
    device->drop();
    pthread_cancel(thread);
    exit(0);
}
void *socketThread(void*)
{
    while(1)
    {
        int sinsize = sizeof(csin);
        if((csock = accept(sock,(SOCKADDR*)&csin,&sinsize)))
        {
            stringw tx  = L"> client conected : Sock ( ";
                    tx += csock; tx += L" ), IP ( ";
                    tx += inet_ntoa(csin.sin_addr);
                    tx += L" : ";
                    tx += htons(csin.sin_port);
                    tx += L" )";
            information->addItem(tx.c_str());
        }
        recv(csock, buffer, sizeof(buffer), 0);
        information->addItem(stringw(buffer).c_str());

        if(strstr(buffer,"LOG = "))
        {
            char log[50] = "";
            char pass[50] = "";
            sscanf(buffer,"LOG = %s", &log);
            recv(csock, buffer, sizeof(buffer), 0);
            information->addItem(stringw(buffer).c_str());

            sscanf(buffer,"PASS = %s", &pass);
            IXMLReader *readAccount = device->getFileSystem()->createXMLReader("accounts/accounts.xml");
            stringw XMLTEXT;
            bool ok = false;
            bool ko = false;
            while(readAccount && readAccount->read())
            {
                switch(readAccount->getNodeType())
                {
                    case EXN_TEXT:
                        XMLTEXT = readAccount->getNodeData();
                    break;
                    case EXN_ELEMENT:
                        if(!strcmp(("account"),stringc(readAccount->getNodeName()).c_str()))
                            if(stringc(log) == stringc(readAccount->getAttributeValue(L"name")))
                            {
                                information->addItem(stringw(readAccount->getAttributeValue(L"name")).c_str());
                                ok = true;
                            }
                        if(!strcmp(("Pass"),stringc(readAccount->getNodeName()).c_str()))
                            if(stringc(pass) == stringc(readAccount->getAttributeValue(L"name")))
                            {
                                information->addItem(stringw(readAccount->getAttributeValue(L"name")).c_str());
                                ko = true;
                            }
                    break;
                    default:
                    break;
                }
            }
            readAccount->drop();
            if(ok && ko)
            {
                information->addItem(stringw(buffer).c_str());
                sprintf(buffer,"CONNECTION_ESTABLED\
");
                send(csock,buffer,sizeof(buffer),0);
            }
            else if(ok && !ko)
                information->addItem(L"UNCORRECTLED_PASSWORD");
            else
                send(csock,"ACCOUNT_NOT_FOUND OR UNCORRECTLED_PASSWORD\
",sizeof(buffer),0);
        }
    }
}

mais de cette façons le client de peut plus envoyer après avoir déjà envoyer LOG et PASS , je sais pas pourquoi hmm
si je ne reconnecte pas le client après je ne peut plus rien faire de même que le serveur ne peut pas envoyer indéfiniment au client il finie toujours par ne l'envoyer que 3 fois :]

fin bref sa fait qu'une semaine ou 2 que je travaille dessus, et pas a temps plein smile

Retour

Options Liens officiels Caractéristiques Statistiques Communauté
Préférences cookies
Corrections
irrlicht
irrklang
irredit
irrxml
Propulsé par Django
xhtml 1.0
css 2.1
884 membres
1440 sujets
11337 messages
Dernier membre inscrit: Saidov17
542 invités en ligne
membre en ligne: -
RSS Feed