Frage zu CAsyncSocket



  • 1. Wie stelle ich beim Socket nonblocking-mode ein, habe es mit IOCtl(FIONBIO) versucht, aber das Accept blockiert weiterhin.
    2. Hab ich versucht die Klasse in einem Workerthread zu benutzen, aber das Prog ist mit ner Zugriffverletzung abgeraucht. Sind dafür weitere Deklarationen notwendig.
    Wäre schön wenn mir jemand Antworten könnte
    Thanx
    Baerbel



  • Also vorerst solltest du schreiben ob du überhaupt MFC verwendest.
    Wenn ja dann mit der Class CAsyncSocket

    int threadklasse::Run()
    {
    
    char hostname[20];
    CString daten;
    gethostname(hostname,20);
    int iRcvd;
    status = 0;
    
    CAsyncSocket sockSrvr;
    CAsyncSocket sockRecv;
    
    int ret = 0;
    while(1)
    {
        AfxSocketInit(NULL);
        sockSrvr.Create(TCP_PORT,SOCK_STREAM,NULL);
        sockSrvr.Listen(5);
        ret = sockSrvr.Accept(sockRecv);
    
        while (ret < 1)
        {
            if (status == 1) 
            {
                sockSrvr.Close();
                PostQuitMessage(0); 
                return 1;
            }
            ret = sockSrvr.Accept(sockRecv);
            ::Sleep(10);
        }
    
        sockSrvr.Close();
        char *pBuf = new char[81];
        iRcvd = sockRecv.Receive(pBuf, 81);
        pBuf[iRcvd] = NULL;
        daten = pBuf;
        delete []pBuf;
        sockRecv.Close();
    }
    
    }
    

    Ausug aus der MSDN:

    BOOL IOCtl( long lCommand, DWORD* lpArgument );

    Return Value

    Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

    WSANOTINITIALISED A successful AfxSocketInit must occur before using this API.

    WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed.

    WSAEINVAL lCommand is not a valid command, or lpArgument is not an acceptable parameter for lCommand, or the command is not applicable to the type of socket supplied.

    WSAEINPROGRESS A blocking Windows Sockets operation is in progress.

    WSAENOTSOCK The descriptor is not a socket.
    Parameters

    lCommand

    The command to perform on the socket.

    lpArgument

    A pointer to a parameter for lCommand.

    Remarks

    Call this member function to control the mode of a socket. This routine can be used on any socket in any state. It is used to get or retrieve operating parameters associated with the socket, independent of the protocol and communications subsystem. The following commands are supported:

    FIONBIO Enable or disable nonblocking mode on the socket. The lpArgument parameter points at a DWORD, which is nonzero if nonblocking mode is to be enabled and zero if it is to be disabled. If AsyncSelect has been issued on a socket, then any attempt to use IOCtl to set the socket back to blocking mode will fail with WSAEINVAL. To set the socket back to blocking mode and prevent the WSAEINVAL error, an application must first disable AsyncSelect by calling AsyncSelect with the lEvent parameter equal to 0, then call IOCtl.

    FIONREAD Determine the maximum number of bytes that can be read with one Receive call from this socket. The lpArgument parameter points at a DWORD in which IOCtl stores the result. If this socket is of type SOCK_STREAM, FIONREAD returns the total amount of data which can be read in a single Receive; this is normally the same as the total amount of data queued on the socket. If this socket is of type SOCK_DGRAM, FIONREAD returns the size of the first datagram queued on the socket.

    SIOCATMARK Determine whether all out-of-band data has been read. This applies only to a socket of type SOCK_STREAM which has been configured for in-line reception of any out-of-band data (SO_OOBINLINE). If no out-of-band data is waiting to be read, the operation returns nonzero. Otherwise it returns 0, and the next Receive or ReceiveFrom performed on the socket will retrieve some or all of the data preceding the “mark”; the application should use the SIOCATMARK operation to determine whether any data remains. If there is any normal data preceding the “urgent” (out-of-band) data, it will be received in order. (Note that a Receive or ReceiveFrom will never mix out-of-band and normal data in the same call.) The lpArgument parameter points at a DWORD in which IOCtl stores the result.
    This function is a subset of ioctl() as used in Berkeley sockets. In particular, there is no command which is equivalent to FIOASYNC, while SIOCATMARK is the only socket-level command which is supported.

    [ 04.10.2001: Beitrag editiert von: Unix-Tom ]



  • Ich benutze CAsyncSocket(Siehe Betreff) und dass die aus den MFC´s stammt brauch ich ja nicht nochmal zu betonen, oder?
    Trotzdem danke



  • Hoffe das Bsp. hilft dir.


Anmelden zum Antworten