Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / sockets and threads

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

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

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...");
    return;
} 

wxSocketBase * con = ss.Accept(true); //Waiting for data...
if (con->IsConnected() && con->WaitForRead(10)) {
    wxChar buffer[128] ;
    con->Read(buffer, 127);   
    textArea->AppendText(buffer);
} 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()) {
    textArea->AppendText("Writing..."); 
    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)?

thanks
Message2. Re: sockets and threads
#3151
Posted by: upCASE 2004-01-01 00:13:40
Hi!
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.

upcase
upCASE
-----------------------------------
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
#3152
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
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-4-18  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0