[C] WinSock und WaitForMultipleObjects

Buba235

Erfahrenes Mitglied
Hallo Leute!


Ich hab da mal eine Frage an euch. Vielleicht könnt ihr mir ja helfen. Ich hab folgendes "Problem": Ich möchte mit Hilfe der Funktion WaitForMultibleObjects auf ein Event warten. Wenn ein Event eintrifft, dann soll auch dementsprechend reagiert werden. So z.B. soll ein Client mit meinem Server verbinden, wenn eben WaitForMultipleObjects signalisiert, dass ein Event das anfordert. Nur hab ich ein Problem - ich weiß nicht wie ich damit umgehen soll. Mein Code sieht so aus:

(Das ist der Anfang, in dem ich auch die Events kreiere)
Code:
/* Includes */
#include <stdio.h>
#include <WinSock2.h>
#include <stdlib.h>
#include <Mswsock.h>

/* Defines */
#define CONNECT             0x00000001 
#define READ                0x00000002 

#define INSTANCES           4 
#define MSGBUF              4096

#define USE_FILE_IO         1   /* States: 0 or 1 - Use this to switch between * 
                                 * ReadFile/WriteFile and WSASend/WSARecv      */

/* Typedefs */
typedef struct 
{ 
   OVERLAPPED   oOverlap; 
   HANDLE       hSockInst; 
   CHAR         chRequest[MSGBUF]; 
   DWORD        cbRead;
   CHAR         chReply[MSGBUF];
   DWORD        cbToWrite; 
   BOOL         bPendingIO; 
   BOOL         bUsed;
   BOOL         bActualUsed;
} SOCKINST, *LPSOCKINST; 

/* global Variables */
SOCKINST    Sock[INSTANCES]; 
HANDLE      hEvents[INSTANCES]; 


/* Prototypes */
HANDLE  createSocket(VOID);
VOID    createInstances(VOID);
BOOL    mainLoop(HANDLE);
BOOL    acceptConnection(HANDLE);

/*===========================================================================*\
 | Main
\*===========================================================================*/
int main(int argc, char *argv[])
{
    /* functioncall */
    createInstances();

    /* Clean up */
    WSACleanup();

    return EXIT_SUCCESS;

}/* End of main */

/*===========================================================================*\
 | create Instances for the different clients
\*===========================================================================*/
VOID createInstances()
{
    /* Variables */
    DWORD   i;
    HANDLE  hListenSocket;

    
    /* call createSocket */
    hListenSocket = createSocket();

    /* create Instances */
    for (i = 0; i < INSTANCES; i++) 
    { 
        /* Create an event object for this instance. */
        hEvents[i] = CreateEvent(  NULL,    /* default security attribute */
                                   TRUE,    /* manual-reset event */
                                   TRUE,    /* initial state = signaled */
                                   NULL);   /* unnamed event object */
       if (hEvents[i] == NULL) 
       {
           printf("CreateEvent failed with %d.\n", GetLastError()); 
           exit(0);
       }

       Sock[i].oOverlap.hEvent = hEvents[i];
       Sock[i].bPendingIO      = TRUE; 
       
    }

    /* Announcement for accepting all incoming requets*/
    printf("\nWaiting for client to connect ...\n\n");

    /* Call mainLoop */
    mainLoop(hListenSocket);

}/* End of createInstances() */

(Hier befindet sich die Serverhauptschleife, in der ich auch auf die Events reagieren soll)
Code:
BOOL mainLoop(HANDLE hListenSocket)
{
    /* Variables */
    BOOL    bSuccess;
    DWORD   dwWait;
    DWORD   cbRet       = 0;
    DWORD   i           = 0;
    DWORD   dwErr       = 0;


    /* Server main loop */
    while (1) 
    { 
        if (Sock[i].bUsed != TRUE)
        {
            Sock[i].bPendingIO      = TRUE;
            Sock[i].oOverlap.hEvent = CONNECT;
        }

        /********************************************************
         * Wait for the event object to be signaled, indicating *
         * completion of an overlapped read, write, or          *
         * connect operation.                                   */   
        dwWait = WaitForMultipleObjects(INSTANCES,    /* number of event objects */
                                        hEvents,      /* array of event objects  */
                                        FALSE,        /* does not wait for all   */
                                        INFINITE);    /* waits indefinitely      */

        /* dwWait shows which socket completed the operation. */
	    i = dwWait - WAIT_OBJECT_0;  /* determines which socket */
		if (i < 0 || i > (INSTANCES - 1)) 
        {
		    printf("Index out of range.\n"); 
			return 0;
		}

        if (Sock[i].bPendingIO)
        {
            bSuccess = GetOverlappedResult( Sock[i].hSockInst,  /* handle to socket */
                                            &Sock[i].oOverlap,  /* OVERLAPPED structure */
                                            &cbRet,             /* bytes transferred */
                                            FALSE);             /* do not wait */
            acceptConnection(hListenSocket);
        }/* if */

     }/* while */ 

     return TRUE;

}/* End of mainLoop() */

/*===========================================================================*\
 | accept incomming Connection
\*===========================================================================*/
BOOL acceptConnection(HANDLE hListenSocket)
{
    /* Variables */
    BOOL                bResetEvent;
    DWORD               i                   = 0;
    struct sockaddr     saClient;
    int                 iClientSize         = sizeof(saClient);

    
    /* Accept */
    for (i = 0; i < INSTANCES; i++)
    {
        if (Sock[i].bUsed != TRUE)
        {
            /* Accept Connection to new Client */
            (SOCKET)Sock[i].hSockInst = WSAAccept((SOCKET)hListenSocket, &saClient, &iClientSize, NULL, NULL);
            if ((SOCKET)Sock[i].hSockInst == INVALID_SOCKET)
            {
                printf("WSAAccept faild with %d \n", WSAGetLastError());
                return FALSE;
            }
            
            Sock[i].bActualUsed = FALSE;
            Sock[i].bPendingIO  = FALSE;
            bResetEvent = ResetEvent(Sock[i].oOverlap.hEvent);
            if (bResetEvent != 0)
            {
                printf("ResetEvent failed with %d \n", GetLastError());
            }
        }
    }

    return TRUE;

}/* End of acceptConnection() */


Mein Problem ist nun, dass ich nicht weiß, wie genau ich Events auslösen kann oder muss und wie genau ic dann darauf abfrage. Ein kleines Beispliel dazu würde mir reichen um weiter zu kommen. Bitte helft mir, ich bin nämlich schon total verzweifelt.

Danke schon mal im Vorraus

Gruß
Buba
 
Zurück