Programming with threads and sockets

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.
GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Programming with threads and sockets

Post by GianT » Wed Jun 15, 2005 8:18 pm

Hi :!: I want my application to perform both client and server actions. I've done a timer and associated a function in which I will put the code of the client part, and I guess that I have to make a thread to put the code of the server part. I would just like to have a complete example of sending and receiving datas with wxSocketServer and wxSocketClient. I found examples on sockets on the internet, but none about wxSockets :? .

Thanks for your help

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: Programming with threads and sockets

Post by Ryan Norton » Wed Jun 15, 2005 9:38 pm

GianT wrote:Hi :!: I want my application to perform both client and server actions. I've done a timer and associated a function in which I will put the code of the client part, and I guess that I have to make a thread to put the code of the server part. I would just like to have a complete example of sending and receiving datas with wxSocketServer and wxSocketClient. I found examples on sockets on the internet, but none about wxSockets :? .

Thanks for your help
The problem is that this is very implementation-depedant and really is mostly thread-related stuff and nothing to do with sockets.

Generally note that none of the wx classes are really thread safe, so be careful...
[Mostly retired moderator, still check in to clean up some stuff]

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: Programming with threads and sockets

Post by Ryan Norton » Wed Jun 15, 2005 9:46 pm

Ryan Norton wrote:
GianT wrote:Hi :!: I want my application to perform both client and server actions. I've done a timer and associated a function in which I will put the code of the client part, and I guess that I have to make a thread to put the code of the server part. I would just like to have a complete example of sending and receiving datas with wxSocketServer and wxSocketClient. I found examples on sockets on the internet, but none about wxSockets :? .

Thanks for your help
The problem is that this is very implementation-depedant and really is mostly thread-related stuff and nothing to do with sockets.

Generally note that none of the wx classes are really thread safe, so be careful...
Maybe this helps

http://forums.wxwidgets.org/viewtopic.p ... highlight=
[Mostly retired moderator, still check in to clean up some stuff]

KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL » Thu Jun 16, 2005 2:53 am

mmm, what i am just thinking about is this:

what if you create a thread, with a wxEvtHandler object inside... Will it be able to receive events?

-- edit --
Non est possibil
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Jun 16, 2005 6:50 am

Hi!
KaReL wrote: what if you create a thread, with a wxEvtHandler object inside... Will it be able to receive events?
Not sure if I got the correct context (just having my first coffee today :D), but I did something like this for a small threaded server.

I tried it with multiple inheritance, like

Code: Select all

class MyServer_Thread : public wxEvtHandler, public wxThread
Now the event table could look like

Code: Select all

BEGIN_EVENT_TABLE(MyServer_Thread, wxEvtHandler)
	EVT_SOCKET(SERVER_EVENT, MyServer_Thread::OnServerEvent)
END_EVENT_TABLE()
Now the thread itself does nothing but calling sleep and loops until it has to be destroyed. The eventhandling works though. I didn't try with GUI interaction, but for the socket part it worked.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Thu Jun 16, 2005 7:32 am

Oh yeah... I should note that for WikiServer I was able to make a very decent threaded server. For even a medium-sized app like WikiServer it was kind of tough because it was never designed to be threaded (in fact in the original author's comments he noted that it would never be a threaded server, lol).

So, my advice would be to design your program from the start to use threads... some basics like don't use global variables. Ever.

However, the toughest thing for me were files - I created a new thread for each user connection, but I needed to change files on disk that could be edited but any one of them at time. Something that's very helpful for this is the WINAPI ::LockFile API, also unix has fcntrl which can do this too.[/list]
[Mostly retired moderator, still check in to clean up some stuff]

KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL » Thu Jun 16, 2005 3:11 pm

Or you can use wxMutex/Semaphore ;)
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Thu Jun 16, 2005 3:18 pm

Woww, I didn't expect so many answers :shock: , I'm surprised, thx :D .
But I'm gonna be more precise. I would like to deal with those problems one after the other.

First of all, I would like an example of how creating sockets with wxSocketServer and Client. I mean, when I was in school, we add examples of sockets ( creating sockets structure, binding ports, waiting for connection ...) and an example of sending and receiving datas. Then we just had to copy paste this and modify the code for the sending/receiving of datas. But actually, with wxSockets, I have no idea how to do this, I can't even make a wxSocketServer object because I don't know how to give the address (it waits for a wxSockAddress& object). I'm just wondering if I shouldn't use normal sockets instead of wx ones :evil: .

When I solve the problem of sockets, than I will deal with threads. I'm still searching on the net and trying stuffs by myself.

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Thu Jun 16, 2005 4:30 pm

Here is one stuff which seems to work, though I don't know how to handle connections for the moment:

wxSocketServer *m_server;
wxIPV4address addr;
addr.LocalHost();
addr.Service(3050);
m_server = new wxSocketServer(addr, wxSOCKET_WAITALL);
if (! m_server->Ok())
{

wxMessageBox("Could not listen at the specified port !", _T("Error"),
wxOK | wxICON_INFORMATION, this);

return;
}
else
{

wxMessageBox(wxString::Format("Server listening on port [%u]\n\n", (unsigned int)(addr.Service())),"Listening...",wxOK);


}

There are still some dark points, for example I don't know if addr.Service(3050) is the good way to attribuate a port number, and now I'm gonna search how to create a wxsocketclient object and send requests to the server... :-k

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Mon Jun 20, 2005 3:38 am

I managed to create a client and make a connection to the server. But now, my problem is that I made event in the event_table so that in each socket event, a message box tells me about it. But I only got the message for the first connexion and not for the others! What can be the problem? Here are parts of the code if this can help understand the problem:

in the event table:

Code: Select all

EVT_SOCKET(SOCKET_ID,     newProgramFrame::OnSocketEvent)
...then the part of the code dealing with the socket server:

Code: Select all

   addr.LocalHost();
    addr.Service(3050);
    m_server = new wxSocketServer(addr, wxSOCKET_WAITALL);
    m_server->SetEventHandler(*this, SOCKET_ID);
    m_server->SetNotify(wxSOCKET_CONNECTION_FLAG |
                    wxSOCKET_INPUT_FLAG |
                    wxSOCKET_LOST_FLAG);
    m_server->Notify(true);

    if (! m_server->Ok())
    {
      
        wxMessageBox("Could not listen at the specified port !", _T("About Minimal"),
                                wxOK | wxICON_INFORMATION, this);

      return;
    }
    else
    {

             wxMessageBox(wxString::Format("Server listening on port [%u]\n\n",
                          (unsigned int)(addr.Service())),"Listening...");
    }

...and the function which should be called on each socket event:

Code: Select all

void newProgramFrame::OnSocketEvent(wxSocketEvent& event)
{
     
  switch(event.GetSocketEvent())
  {
    case wxSOCKET_INPUT      : wxMessageBox("wxSOCKET_INPUT\n"); break;
    case wxSOCKET_LOST       : wxMessageBox("wxSOCKET_LOST\n"); break;
    case wxSOCKET_CONNECTION : wxMessageBox("wxSOCKET_CONNECTION\n"); break;
    default                  : wxMessageBox("Unexpected event !\n"); break;
  }

  
}
Last edited by GianT on Mon Jun 20, 2005 3:41 am, edited 1 time in total.

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Mon Jun 20, 2005 3:41 am

Where's the thread(s)?
[Mostly retired moderator, still check in to clean up some stuff]

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Mon Jun 20, 2005 3:47 am

Errr, I just manage to create and make a thread run...Actually, I wanted to understand the behaviour of wxSocket first, as I read somewhere that it was useless to make threads to accept socket connexions (or something like that). I don't understand at all the behaviour of wxSockets, for example, where should I put the Accept method? I'm some kind of lost...
Once I got how this works, I will add threads to the api

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Mon Jun 20, 2005 3:58 am

Hmmm I'm getting it slowly, I just made this modification in my code and it works:

Code: Select all

  switch(event.GetSocketEvent())
  {
    case wxSOCKET_INPUT      : wxMessageBox("wxSOCKET_INPUT\n"); break;
    case wxSOCKET_LOST       : wxMessageBox("wxSOCKET_LOST\n"); break;
    case wxSOCKET_CONNECTION : wxMessageBox("wxSOCKET_CONNECTION\n");m_server->Accept();break;
    default                  : wxMessageBox("Unexpected event !\n"); break;
  }
But I have a question: I don't know how to check it, but it seems that the connexion are queued. Is that the case, or is each connexion treated like a new thread, that's to say that I can have several functions working at the same time if I get several connexions at the same time?

GianT
Earned some good credits
Earned some good credits
Posts: 124
Joined: Wed Mar 16, 2005 5:44 pm
Location: Guadeloupe, French West Indies
Contact:

Post by GianT » Mon Jun 20, 2005 4:09 am

I made a small modification in my code (Sleep) in order to check whether it's treated like a thread or not, and it seems that it isn't:

Code: Select all

    case wxSOCKET_CONNECTION : m_server->Accept();Sleep(3000);wxMessageBox("wxSOCKET_CONNECTION\n");break;
Actually, I send multiple requests of connexions to the server within a second. What happens is that the GUI is freezed within the 3seconds after the connexion...I guess that if these ones were treated like threads, the GUI wouldn't be freezed. That's my opinion, if someone doesn't agree, well just let me know. I will now add the thread part to my project.

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Mon Jun 20, 2005 4:21 am

Hang on... I'm making a little tutorial :)
[Mostly retired moderator, still check in to clean up some stuff]

Post Reply