<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Solved] Hi guys! Needing help getting QT to play nice with WinSock]]></title><description><![CDATA[<p dir="auto">Hi all!</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">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!</p>
<h1>Code Snippets;<br />
QT 4.7.1 - QT Creator</h1>
<p dir="auto">@void Widget::ConnectNewClient()<br />
{<br />
QTcpSocket *clients = tcpserver-&gt;nextPendingConnection();<br />
connect(clients, SIGNAL(disconnected()), clients, SLOT(deleteLater()));</p>
<pre><code>ui-&gt;lstViewer-&gt;addItem("Client Connected: " + clients-&gt;peerAddress().toString());

int newdatasize = clients-&gt;bytesAvailable();
QByteArray newdata = clients-&gt;readData(clients-&gt;bytesToWrite(), newdatasize);
QString passthrough=newdata;

ui-&gt;lstViewer-&gt;addItem(tr("Packet Received: \"%1\"").arg(passthrough));
clients-&gt;disconnectFromHost();
ui-&gt;lstViewer-&gt;addItem("Client Disconnected");
</code></pre>
<p dir="auto">}@</p>
<p dir="auto">Visual Studio 2010 Ultimate Edition - C/C++ - Windows Application</p>
<h1>ServerChatter.h</h1>
<p dir="auto">@//SeverChatter - This week bots and girls we will we mainly talking TCP</p>
<p dir="auto">#pragma once<br />
#include &lt;WinSock2.h&gt;<br />
#include &lt;WS2tcpip.h&gt;<br />
#include &lt;string.h&gt;<br />
using namespace std;</p>
<p dir="auto">#include "WinTalk.h"</p>
<p dir="auto">#define MAIN_PORT "6688"</p>
<p dir="auto">class ServerChatter<br />
{<br />
public:<br />
ServerChatter(WinTalk *wintalk);<br />
~ServerChatter(void);</p>
<p dir="auto">void SendMsg(void);</p>
<p dir="auto">private:<br />
WinTalk *mywin;</p>
<p dir="auto">WSADATA wsaData;<br />
struct addrinfo<br />
*result,<br />
*ptr,<br />
hints;<br />
SOCKET ConnectSocket;<br />
};@</p>
<h1>ServerChatter.cpp</h1>
<p dir="auto">@ServerChatter::ServerChatter(WinTalk *wintalk)<br />
{<br />
//Grab the WinTalk instance and have our wikid way with it<br />
mywin = wintalk;</p>
<p dir="auto">//Declare useless stuff here<br />
int results;<br />
ConnectSocket = INVALID_SOCKET;</p>
<p dir="auto">//Initialise WinSock<br />
results=WSAStartup(MAKEWORD(2,2), &amp;wsaData);<br />
if(results!=0)<br />
{<br />
//WinSock 2.2 not available - Should only be the case for emulators and pre-xp machines - neither of which are our concern<br />
wintalk-&gt;SystemText(std::wostringstream(L"Cannot find WinSock 2.2 - Are you using emulators you cheeky devil??"));<br />
}<br />
else<br />
{<br />
//Null that mother fuker!<br />
ZeroMemory( &amp;hints, sizeof(hints) );</p>
<p dir="auto">//Fill structure with information (IPv4, Streaming Socket, TCP/IP)<br />
hints.ai_family = AF_INET;<br />
hints.ai_socktype = SOCK_STREAM;<br />
hints.ai_protocol = IPPROTO_TCP;</p>
<p dir="auto">//Now just make sure we are all alive and well before we go crazy connecting things<br />
results = getaddrinfo("69.10.53.10", MAIN_PORT, &amp;hints, &amp;result);<br />
if(results !=0)<br />
{<br />
//Could not find server - Will be common if people run Igma.exe during server downtime<br />
wintalk-&gt;SystemText(std::wostringstream(L"Cannot find server...Are you sure your connected to the tinterweb??"));<br />
}<br />
else<br />
{<br />
//Finally we can trust ourselves<br />
ptr = result;</p>
<p dir="auto">//Hit that socket up<br />
ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);</p>
<p dir="auto">//Check for socket creation<br />
if(ConnectSocket==INVALID_SOCKET)<br />
{<br />
//Unable to create socket - somethings wrong - not really sure what could go wrong mind??<br />
wintalk-&gt;SystemText(std::wostringstream(L"Unable to use socket on port 6688 - Are you using this already??"));<br />
}<br />
else<br />
{<br />
//Right...Lets give this network thing a go (wheres .NET when you need it :-()<br />
results = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);<br />
if(results==SOCKET_ERROR)<br />
{<br />
//If we can't connect to the server then close the socket down and walk away from the 100 car pile-up<br />
closesocket(ConnectSocket);<br />
ConnectSocket=INVALID_SOCKET;<br />
freeaddrinfo(result);<br />
wintalk-&gt;SystemText(std::wostringstream(L"Cannot connect to server..."));<br />
}<br />
}<br />
}<br />
}<br />
}</p>
<p dir="auto">void ServerChatter::SendMsg(void)<br />
{<br />
#define DEFAULT_BUFLEN 512</p>
<p dir="auto">int results;<br />
//string sendbuf("This is a test message");<br />
//char *array = new char[sendbuf.size()];<br />
//std::copy(sendbuf.begin(), sendbuf.end(), array);</p>
<p dir="auto">results=send( ConnectSocket, "Hello!",DEFAULT_BUFLEN, 0 );<br />
if (results==SOCKET_ERROR)<br />
{<br />
mywin-&gt;SystemText(std::wostringstream(L"Unable to send message - Don't know why - Cos its gay...That'll do"));<br />
}<br />
else<br />
{<br />
mywin-&gt;SystemText(std::wostringstream(L"Data Sent"));<br />
}<br />
//delete [] array;<br />
}@</p>
<p dir="auto">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!</p>
]]></description><link>https://forum.qt.io/topic/1704/solved-hi-guys-needing-help-getting-qt-to-play-nice-with-winsock</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 04:16:51 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/1704.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 11 Nov 2010 23:11:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solved] Hi guys! Needing help getting QT to play nice with WinSock on Fri, 12 Nov 2010 01:24:05 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">Thanks again :-D</p>
]]></description><link>https://forum.qt.io/post/59844</link><guid isPermaLink="true">https://forum.qt.io/post/59844</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2010 01:24:05 GMT</pubDate></item><item><title><![CDATA[Reply to [Solved] Hi guys! Needing help getting QT to play nice with WinSock on Thu, 11 Nov 2010 23:23:57 GMT]]></title><description><![CDATA[<p dir="auto">Try to wait for data to be available (either via signal or via waitForReadyRead)</p>
]]></description><link>https://forum.qt.io/post/59838</link><guid isPermaLink="true">https://forum.qt.io/post/59838</guid><dc:creator><![CDATA[DenisKormalev]]></dc:creator><pubDate>Thu, 11 Nov 2010 23:23:57 GMT</pubDate></item></channel></rss>