Sockets in Windows

I am 'relativly' new to C++ and especially to Windows-Programming, although i have a lot of C and Unix experience. So please spare me.

Now I am faced with the really disturbing design mistakes of the MFC class CSockets (which i have experienced once i used them). For further details look here: http://tangentsoft.net/wskfaq/articles/csocket.html

Finally my Question: Is there any well-proven wrapper class for Win-sockets, thats free (fav. open source) ?

I already looked up at some classes, but they all seemed pretty amateurish (that's not always bad, but not sure if good also).

Maybe there is one very famous alternative i've missed? I dont want to take something like QT, because I really want to take an opportunity to discover MFC. In worst case i have to wrap winsock's in my own amateurish way, but i am sure you can help me!

Thanks alot

Maikel
fromyourlink wrote:
Winsock blocking sockets are dead easy to program.


gogo... expierence the mighty might of the WinAPI...
(I dont know any good Socket-libs, too -always do it on my own)...
Is there any well-proven wrapper class for Win-sockets, thats free (fav. open source) ?


Yes, there is one - wxWidgets
Your instinct is correct, don't use the MFC Sockets class.

ACE has a sockets class you may want to take a look at.
i played a little and now i just made a streambuf for SOCKETs. With streambufs i can use a socket with iostream, istream or ostream, without doing much (because i just use iostreams functions). like this:

1
2
3
4
5
6
7
8
9
10
11
        sockstream iocli(cli);   
        for (int i = 0; i < 10; i++) {
            std::string msg;
            iocli << "Type a line: ";
            getline(iocli, msg);
            iocli << "You typed: " << msg << std::endl;
            float f;
            iocli << "Type a floating value: ";
            iocli >> f;
            iocli << "You typed: " << f;
        }


Here is the code snippet for sockstream:

1
2
3
4
5
6
    class sockstream : public std::iostream {
    protected:
        sockbuf buf;
    public:
        explicit sockstream(SOCKET s) : buf(s), std::iostream(&buf) {}
    };


And here is for sockbuf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    class sockbuf : public std::streambuf {
    protected:
        SOCKET s;
        static const int bufSize = 1024;
        char buf[bufSize];
    public:
        explicit sockbuf(SOCKET sock) : s(sock) {
            setg(buf+4, buf+4, buf+4);
        }
    protected:
        virtual 
        int_type overflow(int_type c) {
            if (c == EOF)
                return EOF;
            int rv = send(s, (char*)&c, 1, 0);
            if (rv != 1)
                return EOF;
            return rv;
        }
        virtual
        int_type underflow() {
            if (gptr() < egptr())
                return *gptr();
            int numPutBack = std::min(4, gptr() - eback());
            std::memcpy(buf+(4-numPutBack), gptr()-numPutBack, numPutBack);            
            int num = recv(s, buf+4, bufSize-4, 0);
            if (num == SOCKET_ERROR || num == 0)
                return EOF;
            setg(buf+(4-numPutBack), buf+4, buf+4+num);
            return *gptr();
        }
        virtual 
        std::streamsize xsputn(const char *buf, std::streamsize num) {
            int rv = send(s, buf, num, 0);
            if (rv == SOCKET_ERROR)
                return EOF;
            return rv;
        }
    };


Was a good way to explore the I/O part of STL with this example. (you can use this with different socket classes if they have a type conversion to int or SOCKET - in fact the "cli" in the first snippet is a ext::socket class with an operator SOCKET() method, return the FD for the connection)

There is surely a way to implement this for all filedescriptors using write and read, but this is just an example. Nicolai M. Josuttis did so (with FDs) in his book "C++ Standard Library, The: A Tutorial and Reference"


Thanks to KBW and Modjo. I will test ACE / wxWidgets a little.

Maikel
Last edited on
I would rather have a look at Boost.Asio instead of ACE.

Boost is a lot more modern than ACE, and btw has network iostreams aswell.
I have to work with ACE at work (some legacy projects) and its not fun, the documentation is pretty much only doxygen...



Akolyt0r wrote:

I would rather have a look at Boost.Asio instead of ACE.


Its funny, but thats what i am now really using.

Thanks,
Maikel
I have to work with ACE at work (some legacy projects) and its not fun

I agree, and under Windows it can introduce some tricky errors. But for large systems you tend to rewrite what ACE already does and it's unmatched in its flexibility at a system level (using Streams).

Boost.asio looks pretty cool, but I have seen unanswered questions on this forum when using asio directly on Linux. If Boost sorts out the messy dettails, that'd be great.
Topic archived. No new replies allowed.