Ubuntu – Opening a port for listening

networking

I have a small program I created. Here is a relevant piece of it:

void TcpSocket::ConnectSocket()
{
    socket = new QTcpSocket(this);
    socket->connectToHost("localhost", 77);

    if(socket->waitForConnected(3000)){
        qDebug() << "Connected";

        socket->write("Hello Server\n\r\n");
        socket->waitForBytesWritten(1000);
        socket->waitForReadyRead(3000);
        qDebug() << "Reading: " << socket->bytesAvailable();
        qDebug() << socket->readAll();
        socket->close();
    }
    else {
        qDebug() << "Could not connect";
    }
}

Basically, I want to open a tcp socket at localhost:77. I want to write some data to it, get a response, and then output the response. However, right now that port is closed, as most ports are in ubuntu by default. So right now it cannot connect to that port to listen on. Well from what I researched, iptables seems to be the only way to open a port for listening. So I run this line on my machine:

sudo iptables -A INPUT -p tcp --dport 77 -j ACCEPT

However, the port 77 is still not open.

sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      1199/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1004/cupsd      
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      1157/postgres   
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1121/mysqld     
tcp6       0      0 ::1:631                 :::*                    LISTEN      1004/cupsd      
udp        0      0 127.0.0.1:53            0.0.0.0:*                           1199/dnsmasq    
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1070/dhclient   
udp        0      0 0.0.0.0:39617           0.0.0.0:*                           990/avahi-daemon: r
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           990/avahi-daemon: r
udp6       0      0 :::57021                :::*                                990/avahi-daemon: r
udp6       0      0 :::5353                 :::*                                990/avahi-daemon: r

netstat doesnt show it to be open. Also I still have the same problem with my program: it cannot connect to 77.

Best Answer

You seem to be a little confused as to what "listening" on a port means. In order to listen for incoming connections on a port, an application must explicitly tell the operating system that it is willing to accept connections on that port. As Bert mentioned in his answer, listening on port 77 will (with the default configuration) require root privileges.

If you want to create a simple "test" server that can be set listen on a specified port and that you can interact with, take a look at the netcat command Manpage icon. Basically you want to do this:

sudo nc -l 77

Now try to run your Qt application. You should see "Hello Server" displayed in the terminal that is running netcat. If you change the timeout in QTcpSocket::waitForReadyRead() to something large (like 30000, equal to 30 seconds), you can experiment with sending replies back to your application.

For example, launch the netcat command again and run your Qt app. After the terminal displays "Hello Server", type something into the terminal and press the Return key. Your Qt app should print whatever you typed to the console.

Related Question