Simple TCP Listener class This is my first winsock TCP listener ever written. I can use it for a small framework.

tcp_listener.h:

#pragma once
#include &lt;WinSock2.h&gt;
#include &lt;thread&gt;
class tcp_listener
{
public:
    tcp_listener(std::function&lt; void(SOCKET) &gt; callback);
    void start(unsigned short port);
private:
    SOCKET server;
    std::thread accept_thread;

    std::function&lt; void(SOCKET) &gt; callback;
};


tcp_listener.cpp:

#pragma comment(lib, "Ws2_32.lib")

#include "tcp_listener.h"
#include &lt;iostream&gt;
tcp_listener::tcp_listener(std::function&lt; void(SOCKET) &gt; callback)
{
    this-&gt;callback = callback;

    WSADATA wsa;

    if (WSAStartup(MAKEWORD(2, 2), &amp;wsa) == SOCKET_ERROR)
    {
        std::cerr &lt;&lt; "Cannot startup WSA: " &lt;&lt; WSAGetLastError() &lt;&lt; std::endl;
    }
    else
    {
        server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    }
}

void tcp_listener::start(unsigned short port)
{
    if (server != SOCKET_ERROR)
    {
        SOCKADDR_IN sock_addr;
        sock_addr.sin_port = htons(port);
        sock_addr.sin_family = AF_INET;
        sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);

        if (bind(server, (SOCKADDR*)&amp;sock_addr, sizeof(sock_addr)) == SOCKET_ERROR)
        {
            std::cerr &lt;&lt; "Cannot bind socket on port " &lt;&lt; port &lt;&lt; std::endl;
            return;
        }

        if (listen(server, 10) == SOCKET_ERROR)
        {
            std::cerr &lt;&lt; "Cannot listen to socket." &lt;&lt; std::endl;
            return;
        }

        accept_thread = std::thread([&amp;]() {
            SOCKET client = SOCKET_ERROR;

            while (client == SOCKET_ERROR)
            {
                client = accept(server, NULL, NULL);

                if (client != SOCKET_ERROR)
                {
                    callback(client);

                    client = SOCKET_ERROR;
                }
            }
        });

        accept_thread.detach();
    }
    else
    {
        std::cerr &lt;&lt; "Socket not set." &lt;&lt; std::endl;
    }
}


How to use it:

#include &lt;iostream&gt;
#include "tcp_listener.h"

void on_accept(SOCKET client)
{
    std::cout &lt;&lt; "blabla new connection" &lt;&lt; std::endl;

}

int main()
{
    tcp_listener* listener = new tcp_listener(on_accept);
    listener-&gt;start(1232);

    getchar();

    return 1;
}


I still have to make a new class where the SOCKET is stored and the IP etc. like system.net.sockets.Socket and use that as callback. 

Also I'd like to get a review on my naming convention.
Naming

I think you're naming convention is OK, it seems consistent which is the most important thing.  Some of your names could be a bit more specific:

std::function&lt; void(SOCKET) &gt; callback


callback doesn't tell me what the callback is for.  new_connection_callback might be more appropriate.

Shutdown

There's no obvious way to shutdown your server.  You just allow the object to be destroyed and hope for the best.  I'd usually add a shutdown to a class like this so that the listen thread can be shutdown cleanly.  The accept thread itself has a loop while (client == SOCKET_ERROR) which is a condition that is always true.  You may as well be saying while(1).

Startup

Since the class has no concept of state it doesn't know whether or not it's running.  This means you can't check if it's running when startup is called, so you get a strange error 'can't bind to socket' if you try to start the server on two different ports.  Usually you'd encounter this error if the port was already in use.  However, since the server port is an instance variable you get it because the SOCKET itself is already in use.

Constructor errors

This is a critical error:

if (WSAStartup(MAKEWORD(2, 2), &amp;wsa) == SOCKET_ERROR)


If you can't get a socket, your application is in trouble.  Don't just write to the log, throw an exception so that the caller is aware that there has been a problem and they can stop processing.

Callbacks

It's often useful to pass an additional piece of data into a callback method (for example a pointer to an instance of a collection to add the new socket to).  With that in mind, I'd consider adding an extra parameter (usually a void*) to the callback.  You also might want to consider typedefing the callback to make it easier to update in one place (if you decide to in the future).

Something like:

typedef std::function&lt;void(SOCKET)&gt; accept_callback;


makes the function signatures more descriptive and concise:

tcp_listener(accept_callback callback);