Programming with threads and sockets
-
- 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
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
Thanks for your help
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: Programming with threads and sockets
The problem is that this is very implementation-depedant and really is mostly thread-related stuff and nothing to do with sockets.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
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]
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: Programming with threads and sockets
Maybe this helpsRyan Norton wrote:The problem is that this is very implementation-depedant and really is mostly thread-related stuff and nothing to do with sockets.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
Generally note that none of the wx classes are really thread safe, so be careful...
http://forums.wxwidgets.org/viewtopic.p ... highlight=
[Mostly retired moderator, still check in to clean up some stuff]
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
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
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
Hi!
I tried it with multiple inheritance, like
Now the event table could look like
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.
Not sure if I got the correct context (just having my first coffee today ), but I did something like this for a small threaded server.KaReL wrote: what if you create a thread, with a wxEvtHandler object inside... Will it be able to receive events?
I tried it with multiple inheritance, like
Code: Select all
class MyServer_Thread : public wxEvtHandler, public wxThread
Code: Select all
BEGIN_EVENT_TABLE(MyServer_Thread, wxEvtHandler)
EVT_SOCKET(SERVER_EVENT, MyServer_Thread::OnServerEvent)
END_EVENT_TABLE()
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
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
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
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]
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]
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
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be
-
- Earned some good credits
- Posts: 124
- Joined: Wed Mar 16, 2005 5:44 pm
- Location: Guadeloupe, French West Indies
- Contact:
Woww, I didn't expect so many answers , I'm surprised, thx .
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 .
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.
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 .
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.
-
- Earned some good credits
- Posts: 124
- Joined: Wed Mar 16, 2005 5:44 pm
- Location: Guadeloupe, French West Indies
- Contact:
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...
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...
-
- Earned some good credits
- Posts: 124
- Joined: Wed Mar 16, 2005 5:44 pm
- Location: Guadeloupe, French West Indies
- Contact:
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:
...then the part of the code dealing with the socket server:
...and the function which should be called on each socket event:
in the event table:
Code: Select all
EVT_SOCKET(SOCKET_ID, newProgramFrame::OnSocketEvent)
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.
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
-
- Earned some good credits
- Posts: 124
- Joined: Wed Mar 16, 2005 5:44 pm
- Location: Guadeloupe, French West Indies
- Contact:
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
Once I got how this works, I will add threads to the api
-
- Earned some good credits
- Posts: 124
- Joined: Wed Mar 16, 2005 5:44 pm
- Location: Guadeloupe, French West Indies
- Contact:
Hmmm I'm getting it slowly, I just made this modification in my code and it works:
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?
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;
}
-
- Earned some good credits
- Posts: 124
- Joined: Wed Mar 16, 2005 5:44 pm
- Location: Guadeloupe, French West Indies
- Contact:
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:
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.
Code: Select all
case wxSOCKET_CONNECTION : m_server->Accept();Sleep(3000);wxMessageBox("wxSOCKET_CONNECTION\n");break;
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm