Page 1 of 1

wxSocketServer

Posted: Tue Jul 17, 2012 6:00 pm
by ANtlord
Good day, wxDevelopers.
I began study networks, and decided to develop application on likest wxWidgets.
I read Julian Smart and research wxSocket's sample. But I got issue. My "server" set connection with client and it receive one command from client. My "server" don't get second, third, fourthy commands, etc. Maybe it receive, but it don't respond.

I use wxWidgets 2.9.3, Arch linux (kernel 3.4), CodeBlocks 10.05, gcc 4.7

Code in myframe's constructor:

Code: Select all

    wxIPV4address addr;
    addr.Hostname(_("localhost"));
    addr.Service(6667);

    Socket1 = new wxSocketServer(addr);

    Socket1->SetEventHandler(*this, SERVER_ID);
    Socket1->SetNotify(wxSOCKET_CONNECTION_FLAG);
    Socket1->Notify(true);

    Connect(SERVER_ID,wxEVT_SOCKET,(wxObjectEventFunction)&StilusServerFrame::OnServer);
    Connect(SOCKET_ID,wxEVT_SOCKET,(wxObjectEventFunction)&StilusServerFrame::OnSocket);
Socket1 is object of class "wxSocketServer", SERVER_ID = 666, SOCKET_ID = 100.

Event, when client tries to connect

Code: Select all

void StilusServerFrame::OnServer(wxSocketEvent &e){
    wxSocketServer* pServerSocket = (wxSocketServer*) e.GetSocket();

    wxSocketBase *CurSocket1 = pServerSocket->Accept(false);
    CurSocket1->SetEventHandler(*this, SOCKET_ID);
    CurSocket1->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG| wxSOCKET_OUTPUT_FLAG);
    CurSocket1->Notify(true);
    *Text1<<wxT("New connect\n");
}
Event, when client send command

Code: Select all

void StilusServerFrame::OnSocket(wxSocketEvent &e){
    *Text1<<wxT("New command\n");
}

Re: wxSocketServer

Posted: Tue Jul 17, 2012 11:30 pm
by doublemax
Did you check the "ipc" sample which does exactly what you're trying to do?

Re: wxSocketServer

Posted: Wed Jul 18, 2012 4:42 am
by ANtlord
No, I didn't. I looking now this samle, and I don't see events for connection. wxSocket has wxEVT_SOCKET, and I can't detect event for wxServer and wxClient.

Re: wxSocketServer

Posted: Wed Jul 18, 2012 8:36 am
by doublemax
Sorry, i actually meant the "sockets" sample. But I've never worked with sockets myself, so i can't help any further. Maybe someone else can step in.

Re: wxSocketServer

Posted: Thu Jul 19, 2012 6:05 pm
by ANtlord
So, this problem has next solution. In method of socket's event for new command I add next code

Code: Select all

wxSocketBase *sock = e.GetSocket();
unsigned char c;
sock->Read(&c, 1);
*Text1<<wxT("new command\n");
SOLVED!