none
Are Windows TCP sockets with others like QT tcp sockets?? RRS feed

  • Question

  • I am trying to connect client from VS and Server from Qt , but the communication is unable to establish. But the individual Communication is working fine.

    I am working on QT c++ Project in Ubuntu . My Project needs to get the data from Sdk which runs only in Visual studio express 2012 windows. I am trying to establish communication between VS windows and QT Ubuntu via TCP/IP protocol. But it is not working.

    Are they not Compatible ?? Qt TCP Sockets and Visual Studio TCP Socket....

    Client code from Visual studio express 2012

    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
    
    #define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "27015"
    
    int __cdecl main(int argc, char **argv)
    {
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
    *ptr = NULL,
    hints;
    char *sendbuf = "this is a test";
    char recvbuf[DEFAULT_BUFLEN];
    int iResult;
    int recvbuflen = DEFAULT_BUFLEN;
    
    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s server-name\n", argv[0]);
        return 1;
    }
    
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }
    
    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    
    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    
    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
    
        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }
    
        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }
    
    freeaddrinfo(result);
    
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }
    
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    
    printf("Bytes Sent: %ld\n", iResult);
    closesocket(ConnectSocket);
    WSACleanup();
    
    return 0;
    }
    

    Server code from QT creator 4

    server.h
    
    #include <QtNetwork>
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    
    class Server: public QObject
    {
    Q_OBJECT
    public:
    Server(QObject * parent = 0);
    ~Server();
    public slots:
    void acceptConnection();
    void startRead();
    
    //void Server::sendData(QTcpSocket* client, QString data);
    
    private:
    QTcpServer server;
    QTcpSocket* client;
    };
    
    main.cpp
    
    #include "server.h"
    #include <qcoreapplication.h>
    
    int main(int argc, char** argv)
    {
    QCoreApplication app(argc, argv);
    Server server;
    return app.exec();
    }
    
    server.cpp
    
    #include "server.h"
    #include <iostream>
    using namespace std;
    
    Server::Server(QObject* parent): QObject(parent)
    {
    connect(&server, SIGNAL(newConnection()),
    this, SLOT(acceptConnection()));
    
    server.listen(QHostAddress::Any, 27015);
    }
    
    Server::~Server()
    {
    server.close();
    }
    
    void Server::acceptConnection()
    {
    client = server.nextPendingConnection();
    
    connect(client, SIGNAL(readyRead()),
    this, SLOT(startRead()));
    }
    
    void Server::startRead()
    {
    char buffer[512] = {0};
    client->read(buffer, client->bytesAvailable());
    cout << buffer << endl;
    client->close();
    }
    • Moved by Hart Wang Friday, May 5, 2017 2:40 AM
    Tuesday, May 2, 2017 1:16 PM

All replies

  • Hi Reddy33,

    Thank you for posting here.

    Did you use the wireshark tool to check the Network.  you could use the tool to check whether it can get data. then you could check whether the QT Network can get data.

    Since your issue is related to third product(QT), you could consult the support of QT.

    I will move the case to off-topic forum.

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 3, 2017 3:23 AM