CSocket



  • hi,ich mache heute meine ersten "gehversuche" mit csocket, und stellte
    fest, dass folgender code immer die MessageBox "Kann Socket nicht erzeugen werden" bringt.
    ich bin lt. msdn vorgegangen (auszug):
    *

    To set up communication between a server socket and a client socket

    Construct aCSocket object.

    Use the object to create the underlying SOCKET handle.
    For a CSocket client object, you should normally use the default parameters toCreate, unless you need a datagram socket. For a CSocket server object, you must specify a port in the Create call.

    Note CArchive doesn’t work with datagram sockets. If you want to use CSocket for a datagram socket, you must use the class as you would CAsyncSocket — without an archive. Because datagrams are unreliable (not guaranteed to arrive and may be repeated or out of sequence), they aren’t compatible with serialization via an archive. You expect a serialization operation to complete reliably and in sequence. If you try to use CSocket with a CArchive object for a datagram, an MFC assertion fails.

    If the socket is a client, callCAsyncSocket::Connect to connect the socket object to a server socket.

    vielleicht kann mir jemand helfen, da die suchfunktion tot ist, und ich in der Faq nicht fündig wurde !

    #include <afxwin.h>
    #include <afxsock.h>
    
    class CMyFrame : public CFrameWnd
    {
    public:
        CMyFrame()
        {
            Create(NULL,"bla");
        }
    };
    
    class CMyApp : public CWinApp
    {
    private:
        CSocket m_socket;
    public:
        int ExitInstance()
        {
            WSACleanup();
            return 0;
        }
        BOOL InitInstance()
        {
    
            m_pMainWnd=new CMyFrame();
            m_pMainWnd->ShowWindow(SW_SHOW);
            m_pMainWnd->UpdateWindow();
    
            if(!m_socket.Create())
                AfxMessageBox("Kann socket nicht erzeugen");
    
            if(!m_socket.Connect("127.0.0.1",12345))
                AfxMessageBox("Verbindung nicht möglich");
    
            return TRUE;
        }
    };
    
    CMyApp theapp;
    


  • Hallo, ich denke man muss die winSocket-Schnittstelle vorher noch mit AfxSocketInit(..) initialisieren, vielleicht liegt es daran. Dann am besten gleich in InitInstance.

    Gruß mathi


Anmelden zum Antworten