Difference between TCP Listener and Socket

TcpListener is a convenient wrapper for TCP communications. This allows you to use TcpClient for accepted connections–although you can accept sockets instead of clients to use Socket instead of TcpClient. You can do the same thing with Socket; but you have to deal with some of the TCP-specifics (like SocketType.Stream, ProtocolType.Tcp). TCP is a stream-based … Read more

Nginx cannot find unix socket file with Unicorn (no such file or directory)

After many hours and a grand total of 3 beers, I’ve managed to figure out the problem. After hours of digging, I finally came across this Server Fault answer In layman terms, it appears that programs that create files in /tmp (or /var/tmp as I have discovered) are the only programs that are able to … Read more

How does zmq poller work?

When you need to listen on different sockets in the same thread, use a poller: ZMQ.Socket subscriber = ctx.socket(ZMQ.SUB) ZMQ.Socket puller = ctx.socket(ZMQ.PULL) Register sockets with poller (POLLIN listens for incoming messages) ZMQ.Poller poller = ZMQ.Poller(2) poller.register(subscriber, ZMQ.Poller.POLLIN) poller.register(puller, ZMQ.Poller.POLLIN) When polling, use a loop: while( notInterrupted()){ poller.poll() //subscriber registered at index ‘0’ if( poller.pollin(0)) … Read more

What does it mean to bind() a socket to any address other than localhost?

Binding of a socket is done to address and port in order to receive data on this socket (most cases) or to use this address/port as the source of the data when sending data (for example used with data connections in FTP server). Usually there are several interfaces on a specific machine, i.e. the pseudo-interface … Read more

Explain http keep-alive mechanism

There is overhead in establishing a new TCP connection (DNS lookups, TCP handshake, SSL/TLS handshake, etc). Without a keep-alive, every HTTP request has to establish a new TCP connection, and then close the connection once the response has been sent/received. A keep-alive allows an existing TCP connection to be re-used for multiple requests/responses, thus avoiding … Read more

Why is bind() used in TCP? Why is it used only on server side and not in client side?

It assigns the “local” end’s port number. For a server socket, this is the ultimate way to go – it is exactly what is needed: have your socket be bound to port 80 for a web server, for example. For a client socket, however, the local address and port is normally not of importance. So … Read more

How to bind to any available port?

Another option is to specify port 0 to bind(). That will allow you to bind to a specific IP address (in case you have multiple installed) while still binding to a random port. If you need to know which port was picked, you can use getsockname() after the binding has been performed.