Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
Are you running the program as the root user (or using sudo)? You have to in order to listen on ports below 1024. It seems that you would get a permission denied error opening the port. Are you checking for errors after opening it? –  Bert May 30 '13 at 2:22
    
I ran "sudo iptables" so yes I was super user while running it. However, it did not show on netstat that the port was open. The port was never opened. I just want a port open so my program can listen on it. How do I open a port?\ –  JohnMerlino May 30 '13 at 2:38

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.