Thursday, February 22, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  19] [ 0]  / answers: 1 / hits: 97866  / 3 Years ago, wed, june 9, 2021, 1:14:12

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);

qDebug() << "Connected";

socket->write("Hello Server

qDebug() << "Reading: " << socket->bytesAvailable();
qDebug() << socket->readAll();
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* LISTEN 1199/dnsmasq
tcp 0 0* LISTEN 1004/cupsd
tcp 0 0* LISTEN 1157/postgres
tcp 0 0* LISTEN 1121/mysqld
tcp6 0 0 ::1:631 :::* LISTEN 1004/cupsd
udp 0 0* 1199/dnsmasq
udp 0 0* 1070/dhclient
udp 0 0* 990/avahi-daemon: r
udp 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.

More From » networking


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.

[#30972] Wednesday, June 9, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 223
Total Questions: 111
Total Answers: 119

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago