Reputation: 2934
Can someone provide just a simple example of how to use wxSockets write, because every time I try to use it to write to a connection I get windows coming up with this program has stopped working crap and the only thing I'm doing is using wxSocketBase::Write. Or does anyone know why it is failing?
I have even tried to hardcode the buffer that is supposed to be written with something like this:
wxSocketClient * sockConn = new wxSocketClient();
wxString url = "localhost";
wxIPV4address addr;
addr.Hostname(url);
addr.Service(6667);
sockConn->Connect(addr);
wxString test = wxT("testing");
sockConn->Write(test.mb_str(), wxStrlen(test));
sockConn->Close();
delete sockConn;`
And every time I call write it just fails out
Also is there an easy way to get wxString to work with wxSocket, or should it be easy after I figure out what's going on here?
Right now its a lot easier to just use winsock to do all this stuff and well that's not why I have wxWidgets, I got it to make life easier.
Upvotes: 1
Views: 2854
Reputation: 168716
Can someone provide just a simple example of how to use wxSockets write?
Yes, I can:
#include <wx/wx.h>
#include <wx/socket.h>
#include <iostream>
int main () {
wxInitialize();
wxSocketClient * sockConn = new wxSocketClient(wxSOCKET_WAITALL);
wxString url = wxT("localhost");
wxIPV4address addr;
addr.Hostname(url);
addr.Service(6667);
if(!sockConn->Connect(addr, true)) {
std::cout << "Connect failed\n";
return 1;
}
wxString test = wxT("testing\n");
sockConn->Write(test.mb_str(), wxStrlen(test));
sockConn->Close();
delete sockConn;
wxUninitialize();
return 0;
}
Upvotes: 2
Reputation: 846
std::string test("testing");
sockConn->Write(test.c_str(), test.length);
Simpler to use std::string instead of wxString.
Upvotes: 0
Reputation: 20576
I think you may be misusing wxString::mb_str().
The docs give us this:
wxString::mb_str const char* mb_str(wxMBConv& conv) const
const wxCharBuffer mb_str(wxMBConv& conv) const
Returns multibyte (C string) representation of the string. In Unicode build, converts using conv's cWC2MB method and returns wxCharBuffer. In ANSI build, this function is same as c_str. The macro wxWX2MBbuf is defined as the correct return type (without const).
This suggest to me that you should write something like
wxMBConv conv;
sockConn->Write(test.mb_str(conv), wxStrlen(test));
Upvotes: 0
Reputation: 20576
I do not think you should be using wxSocketBase. You should use either wxSocketClient or wxSocketServer. Here is a simple example of wxSocketClient
// connect to a server on the same machine, listening on port 3000
wxSocketClient mySocket;
wxIPV4address addr;
addr.Hostname(L"localhost");
addr.Service(3000);
mySocket.Connect(addr,true) ); // should check return for success
// write something
char sentence[100];
sprintf(sentence,"$SDDBT,%5.1f,0,0,,0*00\x13\n",depth/10.0);
mySocket.Write(sentence,strlen(sentence));
Looking at the code you posted, I would investigate a couple of things.
First, you should check the return from Connect() to ensure that you actually get a connection.
Second, are you using a unicode or multibyte build? Tt looks to me like it might be unicode
Upvotes: 0