[Solved] Hi guys! Needing help getting QT to play nice with WinSock



  • Hi all!

    Now I've been programming QT for little over a month now so forgive some of the more n00bish statements I may make however I have a rather annoying problem I can't sort.

    I cannot seem to get QUdpSocket and WinSock to play nice with each other. The idea is that a TCP/IP connection is established (which works fine) and then a test packet is sent from WinSock to QT on a remote server.

    Now just to rule out a few things to make sure it was me being a n00b and not a breakdown in communication I have also put a QString message in to ensure that something is being displayed.

    The data is being statically generated from WinSock and being picked up by a combination of QTcpServer and QTcpSocket where the data needs to be converted into a QString and then added as an item in a QListWidget.

    I've tried about 1000 combinations now and am beginning to lose any sense of logic if i'm honest, so I have no idea if the problem lies in the network communication itself, or the data types or whatever!

    Code Snippets;
    QT 4.7.1 - QT Creator

    @void Widget::ConnectNewClient()
    {
    QTcpSocket *clients = tcpserver->nextPendingConnection();
    connect(clients, SIGNAL(disconnected()), clients, SLOT(deleteLater()));

    ui->lstViewer->addItem("Client Connected: " + clients->peerAddress().toString());
    
    int newdatasize = clients->bytesAvailable();
    QByteArray newdata = clients->readData(clients->bytesToWrite(), newdatasize);
    QString passthrough=newdata;
    
    ui->lstViewer->addItem(tr("Packet Received: \"%1\"").arg(passthrough));
    clients->disconnectFromHost();
    ui->lstViewer->addItem("Client Disconnected");
    

    }@

    Visual Studio 2010 Ultimate Edition - C/C++ - Windows Application

    ServerChatter.h

    @//SeverChatter - This week bots and girls we will we mainly talking TCP

    #pragma once
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    #include <string.h>
    using namespace std;

    #include "WinTalk.h"

    #define MAIN_PORT "6688"

    class ServerChatter
    {
    public:
    ServerChatter(WinTalk *wintalk);
    ~ServerChatter(void);

    void SendMsg(void);

    private:
    WinTalk *mywin;

    WSADATA wsaData;
    struct addrinfo
    *result,
    *ptr,
    hints;
    SOCKET ConnectSocket;
    };@

    ServerChatter.cpp

    @ServerChatter::ServerChatter(WinTalk *wintalk)
    {
    //Grab the WinTalk instance and have our wikid way with it
    mywin = wintalk;

    //Declare useless stuff here
    int results;
    ConnectSocket = INVALID_SOCKET;

    //Initialise WinSock
    results=WSAStartup(MAKEWORD(2,2), &wsaData);
    if(results!=0)
    {
    //WinSock 2.2 not available - Should only be the case for emulators and pre-xp machines - neither of which are our concern
    wintalk->SystemText(std::wostringstream(L"Cannot find WinSock 2.2 - Are you using emulators you cheeky devil??"));
    }
    else
    {
    //Null that mother fuker!
    ZeroMemory( &hints, sizeof(hints) );

    //Fill structure with information (IPv4, Streaming Socket, TCP/IP)
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    //Now just make sure we are all alive and well before we go crazy connecting things
    results = getaddrinfo("69.10.53.10", MAIN_PORT, &hints, &result);
    if(results !=0)
    {
    //Could not find server - Will be common if people run Igma.exe during server downtime
    wintalk->SystemText(std::wostringstream(L"Cannot find server...Are you sure your connected to the tinterweb??"));
    }
    else
    {
    //Finally we can trust ourselves
    ptr = result;

    //Hit that socket up
    ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

    //Check for socket creation
    if(ConnectSocket==INVALID_SOCKET)
    {
    //Unable to create socket - somethings wrong - not really sure what could go wrong mind??
    wintalk->SystemText(std::wostringstream(L"Unable to use socket on port 6688 - Are you using this already??"));
    }
    else
    {
    //Right...Lets give this network thing a go (wheres .NET when you need it :-()
    results = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if(results==SOCKET_ERROR)
    {
    //If we can't connect to the server then close the socket down and walk away from the 100 car pile-up
    closesocket(ConnectSocket);
    ConnectSocket=INVALID_SOCKET;
    freeaddrinfo(result);
    wintalk->SystemText(std::wostringstream(L"Cannot connect to server..."));
    }
    }
    }
    }
    }

    void ServerChatter::SendMsg(void)
    {
    #define DEFAULT_BUFLEN 512

    int results;
    //string sendbuf("This is a test message");
    //char *array = new char[sendbuf.size()];
    //std::copy(sendbuf.begin(), sendbuf.end(), array);

    results=send( ConnectSocket, "Hello!",DEFAULT_BUFLEN, 0 );
    if (results==SOCKET_ERROR)
    {
    mywin->SystemText(std::wostringstream(L"Unable to send message - Don't know why - Cos its gay...That'll do"));
    }
    else
    {
    mywin->SystemText(std::wostringstream(L"Data Sent"));
    }
    //delete [] array;
    }@

    Now I realise that I'm using blocking sockets etc etc its more a case of getting this working so I can build on it - Please help!



  • Try to wait for data to be available (either via signal or via waitForReadyRead)



  • Thank You very much that seemed to work lovely - The packets that I am receiving after the first are a bit gobbledegook but that is another program for another day.

    Thanks again :-D


Log in to reply
 

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