thrift - address already in use <p>I use Ubuntu 14.04 LTS. Here is an example code which works:</p> <pre><code>boost::shared_ptr&lt;TestHandler&gt; handler(new TestHandler()); boost::shared_ptr&lt;TProcessor&gt; processor(new TestProcessor(handler)); boost::shared_ptr&lt;TServerTransport&gt; serverTransport(new TServerSocket(9090)); boost::shared_ptr&lt;TTransportFactory&gt; transportFactory(new TBufferedTransportFactory()); boost::shared_ptr&lt;TProtocolFactory&gt; protocolFactory(new TBinaryProtocolFactory()); thriftServerThread = std::thread(&amp;TSimpleServer::serve, TSimpleServer(processor, serverTransport, transportFactory, protocolFactory)); </code></pre> <p>When I run program <code>netstat -a | grep 9090</code> returns:</p> <pre><code>tcp6 0 0 [::]:9090 [::]:* LISTEN </code></pre> <p>I want to use thrift server only within a localhost so I have changed:</p> <pre><code> boost::shared_ptr&lt;TServerTransport&gt; serverTransport(new TServerSocket(9090)); </code></pre> <p>to</p> <pre><code>boost::shared_ptr&lt;TServerTransport&gt; serverTransport(new TServerSocket("127.0.0.1:9090")); </code></pre> <p>When I have run a program first time I have got following info from netstat:</p> <pre><code>unix 2 [ ACC ] STREAM LISTENING 30808 127.0.0.1:9090 </code></pre> <p>When I try to run program again even after system reboot I get an exception:</p> <blockquote> <blockquote> <p>Thrift: Tue Mar 3 13:31:40 2015 TServerSocket::listen() PATH 127.0.0.1:9090 terminate called after throwing an instance of 'apache::thrift::transport::TTransportException' what(): Could not bind: Address already in use</p> </blockquote> </blockquote> <p>Netstat doesn't find that address. What is going on ? I have debugged thrift and a function </p> <pre><code>extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) __THROW; </code></pre> <p>declared in <strong>/usr/include/x86_64-linux-gnu/sys/socket.h</strong> returns value other than zero and next an exception is thrown. How to fix that ? When I change a port my program works but only once, then again even after system reboot I get that exception. </p>