Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCP IP communication
Forum Updated to NodeBB v4.3 + New Features

TCP IP communication

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 3.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnilReddy
    wrote on 30 Apr 2017, 15:41 last edited by
    #1

    I am working on a project with Qt C++ ubuntu. My project need to access the data from Visual studio express 2012 win32 console application project. Is it possible to transmit the data from VS to Qt by using TCPIP protocol ??

    Thanks in advance.

    M 1 Reply Last reply 30 Apr 2017, 16:02
    0
    • A AnilReddy
      30 Apr 2017, 15:41

      I am working on a project with Qt C++ ubuntu. My project need to access the data from Visual studio express 2012 win32 console application project. Is it possible to transmit the data from VS to Qt by using TCPIP protocol ??

      Thanks in advance.

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 30 Apr 2017, 16:02 last edited by mrjj
      #2

      @AnilReddy
      Yes that should work fine.

      http://doc.qt.io/qt-5/qtnetwork-programming.html

      1 Reply Last reply
      2
      • A Offline
        A Offline
        AnilReddy
        wrote on 30 Apr 2017, 16:08 last edited by
        #3

        thank you for the reply. So in this case By creating client application in visual studio to transmit the data and server application the QT linux (Ubuntu). Would The data be successfully transmitted right??

        M 1 Reply Last reply 30 Apr 2017, 16:11
        0
        • A AnilReddy
          30 Apr 2017, 16:08

          thank you for the reply. So in this case By creating client application in visual studio to transmit the data and server application the QT linux (Ubuntu). Would The data be successfully transmitted right??

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 30 Apr 2017, 16:11 last edited by
          #4

          @AnilReddy
          Yes it should transfer fine.

          Notice, that between platform there can be issues with Endianness
          https://en.wikipedia.org/wiki/Endianness
          ( Linux <-> Windows often is not affected)

          Also some of the Qt class have a header that the other side must skip.

          So there is some small details but overall it should just work.

          1 Reply Last reply
          2
          • A Offline
            A Offline
            AnilReddy
            wrote on 2 May 2017, 08:08 last edited by
            #5

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

            Both codes are working individually within their IDEs . But to build communication between VS and QT it is not working. May the both sockets are not compatible. Could you please check the code and help me out to find the mistake I have done.

            1 Reply Last reply
            0

            1/5

            30 Apr 2017, 15:41

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved