Friday, June 17, 2011

Select on boost asio tcp socket

Boost asio does not provide a method of doing select on tcp::socket directly, but I found this example on the web:

    // socket here is:  boost::shared_ptr a_socket_ptr

        // Set up a timed select call, so we can handle timeout cases.

        fd_set fileDescriptorSet;
        struct timeval timeStruct;

        // set the timeout to 30 seconds
        timeStruct.tv_sec = 30;
        timeStruct.tv_usec = 0;
        FD_ZERO(&fileDescriptorSet);

        // We'll need to get the underlying native socket for this select call, in order
        // to add a simple timeout on the read:

        int nativeSocket = a_socket_ptr->native();

        FD_SET(nativeSocket,&fileDescriptorSet);

        select(nativeSocket+1,&fileDescriptorSet,NULL,NULL,&timeStruct);

        if(!FD_ISSET(nativeSocket,&fileDescriptorSet)){ // timeout

                std::string sMsg("TIMEOUT on read client data. Client IP: ");

                sMsg.append(a_socket_ptr->remote_endpoint().address().to_string());

                throw MyException(sMsg);
        }

        // now we know there's something to read, so read
        boost::system::error_code error;
        size_t iBytesRead = a_socket_ptr->read_some(boost::asio::buffer(myVector), error);

        ...

A little ugly, since it uses the native socket, but should be okay on most systems.

Thursday, June 16, 2011

GNU Screen

I've been using VIM a lot (on the command line too), and am always bummed when I have to switch over to the arrow keys to switch between open terminal windows. GNU Screen is a really nice option (thanks, Peter!!).

GNU Screen Homepage

Handy shortcuts:
Ctrl-a, [0-9] - Switch to a numbered window
Ctrl-a, [n] [p] - next or previous window
Ctrl-a, c - create a new window
Ctrl-a, a - toggle between this and the last window
Ctrl-a, Ctrl-A - give a new name to this window
Ctrl-a, d - detach from this window

screen -ls - list screens
screen -x - reattach to screens