Guidance Forums / wxWidgets (wxWindows) in C++ / sockets and threads

Search 搜索
Home Home

Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. sockets and threads
Posted by: lea 2003-12-31 23:31:48

I have big trouble with sockets, they does simply not work as expected(documentation).
There are two threads, thread one is the server
In the entry-methode:

wxIPV4address addr;
if(!addr.Service(1180)) {
        textArea->AppendText("No service..."); 

wxSocketServer ss(addr);
if(!ss.Ok()) {
    textArea->AppendText("Socket not ok...");

wxSocketBase * con = ss.Accept(true); //Waiting for data...
if (con->IsConnected() && con->WaitForRead(10)) {
    wxChar buffer[128] ;
    con->Read(buffer, 127);   
} else {
   textArea->AppendText("Error or timeout...");

That was the server and here comes the client:
In entry-methode 2:
wxIPV4address addr;

if(!addr.Hostname("localhost")) {
   textArea->AppendText("No service..."); 

if(!addr.Service(1180)) {
   textArea->AppendText("No host..."); 

wxSocketClient *m_sock = new wxSocketClient(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
m_sock->Connect(addr, true); 

if (m_sock->IsConnected()) {
    m_sock->Write("HI", 2);   
} else {
       textArea->AppendText("Not connected..."); 
OK, that work as expected, but, when the serversocket has accepted, and the retuned wxSocketBase* con tries to read (there has to be readable data, because accepted returned!), then there is nothing to read?!
It just times out.
What is wrong here?
Is there a bug again, that these methode are not working?
Or doesn't work threads and sockets together?
Or do i have to use events(following the documentation, this doesn't seem to be neccessary)?

Message2. Re: sockets and threads
Posted by: upCASE 2004-01-01 00:13:40
I'm not really an expert on this, but I implemented some chat client using wxSockets and a little TicTacToe game to be played vie the web. Both use the wxEVT_SOCKET classes and work fine.

I'd strongly suggest having a look at the events and handle things for wxSOCKET_INPUT and wxSOCKET_OUTPUT.
I remember some time ago there was a posting that the sockets classes don't seem to be perfectly threadsafe, so maybe there's another error.

Have a look a the socket example. It features a small server and client model, where the client runs test on the server.

If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Re: sockets and threads
Posted by: lea 2004-01-01 00:43:30
hm, thanks!
I did not want to use the events, but it seems that there is no choice for me.
It is a bit frustraiting with all these bug in wxWindows, i doesn't work for long with wx, but i encounted several bugs, some i guessed(like this above) some i know for sure(wxURL and threads, WinXP and native style, etc...).
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
© Mon 2024-6-17  Guidance Laboratory Inc. Hits:0