wxSocket Event problem

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
wangbo_coldwind
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 09, 2005 1:44 am
Location: china

wxSocket Event problem

Post by wangbo_coldwind »

Does anybody know when wxSocket_LOST event be generated?
I used this code:
M_socket->Connect(server,false);
M_socket->WaitOnConnect(100,10);

Then I always get the lost event while the socket is really open, I can read and write information to that socket. By the way, I used a other thread to create and open this socket, not in the main thread.

Thanks!
Bo
wangbo_coldwind
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 09, 2005 1:44 am
Location: china

Post by wangbo_coldwind »

If I use socket->Connect(server,true), Can I receive the Socket_LOSt event if connect failed??
toxicBunny
Super wx Problem Solver
Super wx Problem Solver
Posts: 424
Joined: Tue Jul 12, 2005 8:44 pm
Location: Alabama, USA

Post by toxicBunny »

You can specify the events you want to receive by using wxSocketBase::Notify().

http://www.wxwidgets.org/manuals/2.6.2/ ... esetnotify

From the documentation
wxSocketClient::Connect
bool Connect(wxSockAddress& address, bool wait = true)

Connects to a server using the specified address.

If wait is true, Connect will wait until the connection completes. Warning: This will block the GUI.

If wait is false, Connect will try to establish the connection and return immediately, without blocking the GUI. When used this way, even if Connect returns false, the connection request can be completed later. To detect this, use WaitOnConnect, or catch wxSOCKET_CONNECTION events (for successful establishment) and wxSOCKET_LOST events (for connection failure).
So, using

Code: Select all

socket->Connect(server,true);
will return true if successful and false if not successful, but no events will be received. Instead of using this or WaitOnConnect(), simply use

Code: Select all

socket->Connect(server, false);
and handle the wxSOCKET_CONNECTION and wxSOCKET_LOST events. Also, use IsConnected() in the wxSOCKET_CONNECTION event to insure that the client is truly connected.

-Scott
wxMSW 2.6.2, VS 2002, 2003 and 2005, Code::Blocks and mingw, Windows XP Pro
Post Reply