I am writing a C++ server program and a Qt client program. The problem is, recv() keeps waiting forever when tcpdump clearly shows a tcp packet from client is delivered to my network interface.
Server program is ran on Centos 7.7 and compiled with g++ 4.8.5
while (true) {
sockaddr_in client_addr{0};
socklen_t client_len = sizeof(sockaddr_in);
newsockfd = accept(sockfd, (sockaddr*)&client_addr, &client_len);
cout << "New connection from " << inet_ntoa(client_addr.sin_addr) << endl;
long bytes;
char buf[1000];
while (true) {
bytes = recv(newsockfd, buf, 1000, 0);
if (bytes < 1) {
if (bytes < 0) { cout << "Error: " << errno << ", " << strerror(errno) << endl; }
break;
}
ProcessRequest(buf, bytes);
}
close(newsockfd);
cout << "Host " << inet_ntoa(client_addr.sin_addr) << " disconnected..." << endl;
}
Client program is ran on Windows and compiled with msvc 2017 and Qt:
QTcpSocket* loginClient = new QTcpSocket(this);
loginClient->connectToHost(ipAddr, Port);
if (!loginClient->waitForConnected(30000)) { return; }
char data[100] = "12345678";
int sendLen = loginClient->write(data, 9);
if (sendLen == -1) { return; }
The server is able to detect connection from client, so ip and port issues are out of the way. Furthermore, the server is not able to detect disconnection from this Qt client properly. Disconnection detection is usually postponed by numerous minutes or indefinitely.
An interesting thing is, I wrote another client program ran on another Centos, with which the server worked Perfectly. So I assume the server program is fine?
Thanks for any help.