wxSocketClient establish connection

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

wxSocketClient establish connection

Post by mael15 »

hi everyone,
so this basically works, it just sometimes needs a second run through to be successful. can I improve it?

Code: Select all

wxIPV4address addrSett;
addrSett.Hostname(wxString::Format(wxT("192.168.5.%i"), getLastIpNum()));
addrSett.Service(wxString::Format(wxT("%i"), portNr));
bool makeConn = sock->Connect(addrSett, false);		// normal: false
bool isConn = sock->IsConnected();					// normal: false
bool waitConn = sock->WaitOnConnect(2);				// normal: true
bool isConn2 = sock->IsConnected();					// normal: true
bool isInit = sock->Initialize();					// normal: true
thanx!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSocketClient establish connection

Post by ONEEYEMAN »

Hi,
Did you look at the sample?

Thank you.
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxSocketClient establish connection

Post by mael15 »

ONEEYEMAN wrote: Sun Feb 28, 2021 5:58 pm Hi,
Did you look at the sample?

Thank you.
yes, it all simply works in the sample, probably because it also has control over the server. That is not the case with me, so i guess my problem has to do with some non wxWidgets socket implementation in the device.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSocketClient establish connection

Post by ONEEYEMAN »

Hi,
Can you run the sample against your device and see if it works?

Thank you.
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxSocketClient establish connection

Post by mael15 »

good idea! this worked and I found out that the device sometimes needs longer to connect the socket. i used to have WaitOnConnect(2) and that sometimes was too little time.
what bugs me is that it might need up to 7 seconds per socket and i have 2 sockets per device and potentially 2 devices. can i somehow ping the ip address before i try to connect the socket with the annoying waiting time? or is there any other method to find out if waiting for a socket to connect is worth the time?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSocketClient establish connection

Post by ONEEYEMAN »

Hi,
Maybe try an asynchronous connection?
Or use thread and check if its connected in the main one.

Thank you.
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxSocketClient establish connection

Post by mael15 »

I would like to block the main app until either a connection is established or not. I just want this to be as fast as possible. It seems unnecessary to try to establish a wxSocket connection for a couple of seconds if one could somehow quickly check if there is a device at a certain ip adress at all.
I was playing around with this and it works:

Code: Select all

wxArrayString pingOut;
wxString ipToPing("192.168.178.10");
long zeroIsExecuteSuccess = wxExecute(wxT("ping ") + ipToPing + wxT(" -n 1 -4 -w 1000"), pingOut, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE);
if (zeroIsExecuteSuccess == 0) {
	//for (auto str : pingOut)
	//	OutputDebugString(str + wxT("\n"));
	bool pingSuccess = pingOut.size() > 2 && pingOut[2].Contains(ipToPing + wxT(":"));
	OutputDebugString(wxT("device at ") + ipToPing + (pingSuccess ? wxT(" found") : wxT(" not found")) + wxT("\n"));
} 
else
	OutputDebugString(wxT("wxExecute failed, no ping was sent\n"));
it just seems a bit unnecessarily complicated.

PS: my program runs on Windows 10 pro exclusively, i just have to make sure the ping (or whatever else is possible) works on systems with whatever language. ping return language specific strings.
Post Reply