TCP connected but doesn't receive



  • Hi there.
    I got two sets of TCP-server/clients.
    One written in Qt5 and the other written in C.

    Qt5-server works with Qt5-client,
    Qt5-server works with C-client,
    but,
    C-server doesn't work with Qt5-client.

    And unfortunately this is the combination I need.
    Everything works properly when looking from the clients perspective. Client and server is succesfully connected and the client sends the data.
    From the servers perspective the connection is established but it doesn't receive anything.

    Can anyone see what goes wrong?

    Qt5-Clientclass:

    @#include "dataserver.h"

    DataServer::DataServer(QObject *parent) :
    QObject(parent)
    {
    }

    void DataServer::update()
    {
    socket = new QTcpSocket(this);

    socket->connectToHost("127.0.0.1", PORT);
    
    if (socket->waitForConnected(3000))
    {
        qDebug() << "Connected!";
    
        i = socket->write("test \n");
        qDebug() << i;
        
        emit this->serverUpdateStatus(true);
        socket->close();
    }
    else
    {
        qDebug() << "Didn't connect";
        emit this->serverUpdateStatus(false);
    }
    

    }@

    C-Server:
    @void TCP::socketConnect()
    {
    //create Intenet domain socket
    serversock_ = socket (AF_INET, SOCK_STREAM, 0);
    if (serversock_ < 0)
    {
    error("ERROR opening socket");
    }

    //Set REUSEADDR at socket level to allow reuse of a local port
    int seton = 1;
    int opt = setsockopt(serversock_, SOL_SOCKET, SO_REUSEADDR, &seton, sizeof(seton));
    if(opt < 0)
    cout<<"WAENING!!! reuse of local port not enabled"<<endl;

    //Fill in socket structure
    //sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddr.sin_port = htons(port_);

    //Bind socket to port
    bind(serversock_, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr));

    //Set number of connection that will be queued
    listen(serversock_, 5);
    }

    void TCP::msgServer()
    {
    //Server start
    for(;;)
    {
    cout<<""<<endl;
    cout<<"Server is ready for connection"<<endl;
    cout<<"Waiting for client"<<endl;
    cout<<"
    "<<endl;

      //Wait for a client to connect and accept;
      sockaddr_in clientAddr;
      socklen_t sin_size = sizeof(struct sockaddr_in);
      clientsock_ = accept(serversock_, (sockaddr*)&clientAddr, &sin_size);
      cout<<"Client connected"<<endl;
      if(clientsock_ > 0)
        {
        cout<<"Ready to receive"<<endl;
        cout<<"______________________________"<<endl;
        }
    
      //Message receive from client
      string clientID = readTextTCP(clientsock_);
    
      cout<<"Message from client: "<<'\n';
      cout<<clientID<<'\n';
    
      //Close socket descriptor
      shutdown(clientsock_, 2);
      close(clientsock_);
    
      }
    

    //Close socket
    shutdown(serversock_, 2);
    close(serversock_);
    }@



  • Hi, I've got some ideas,

    1. shutdown(...) discards any data waiting in the socket's pipe, the application maybe has not received yet.

    @
    shutdown(clientsock_, 2);
    close(clientsock_);
    @

    1. We should have a look into your receiver method, how do you care about errors (i.e. pipe closed, no data ready)?

    @
    //Message receive from client
    string clientID = readTextTCP(clientsock_);
    @

    1. Possibly it's a good idea to close the socket from the receiver-side when every data has arrived in the application, not when the other peer has sent it.

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.