[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: KSocket usage (With attachment ths time...)
From:       Patrick Julien <freak () ideasandassociates ! com>
Date:       2002-03-02 20:37:38
[Download RAW message or body]

In order for Qt to generate the signals to call these slots, it must 
select()/poll() the socket you just created, and it probably does that 
in it's main event loop.  So, that means, if you delete the socket 
object, KSocket's destructor probably unregisters those notifiers for 
you, so it will never see data on that socket, i.e. never call your slots.

That means, you should be reading from the socket in the readyRead slot. 
 And you should definitely not delete your KSocket object, make it a 
member of your class or something.

Jean-Eric Cuendet wrote:

>
> Hi,
> I'm trying to make a KIO slave client for KDE to access phpGroupWare
> (Through XMLRPC).
> I use the KSocket class to write/read the XML stuff from/to the server.
> Attached is my code. That quite works but it's ugly... and can't be used
> mainstream!
> What I'd like is:
> - Open the socket
> - Check that I'm connected
> - write data
> - read response
> - parse it
> All the method calls can be blocked if I can have a timeout after, say,
> 30 secs.
>
> Now, I'm able to send/receive the data but my code is not right!
> Could someone tell me what I do wrong?
>
> What I don't understand:
> - Why aren't my slots called (readEvent -> readyRead, ...)
> - How can I loop until everything is written/read (a sleep() is not the
> best way...) 


Just use the return value from read with your socket in non-blocking 
mode :).  If you get back -1, and errno is EAGAIN, than no data is left 
on the socket, when the socket gets data again, readyRead() will be 
called again.

Finally, they are other socket classes in Qt/KDE, you might want 
something that completely hides you from the socket.

>
>
> Thanks a lot for any help with that.
> -jec
>
> PS: I already had a look at KXmlRpcD...
>
>
>
>
>
>
>------------------------------------------------------------------------
>
>bool KXmlRpcClient::call(const QString& method, KXmlRpcData& outData, KXmlRpcData& retData)
>{
>  outData.setMethod(method);
>  outData.setUrl(serverUrl_);
>  QString xmlContent = outData.getXmlContent();
>
>  log()<<"Got XML: "<<xmlContent<<endl;
>
>  log()<<"Open the socket on host: "<<serverUrl_.host()<<" / "<<serverUrl_.port()<<endl;
>  KSocket* socket = new KSocket(serverUrl_.host(), serverUrl_.port());
>
>  connect(socket, SIGNAL( readEvent(KSocket*) ), SLOT( readyRead(KSocket*) ));
>  connect(socket, SIGNAL( writeEvent(KSocket*) ), SLOT( readyWrite(KSocket*) ));
>  connect(socket, SIGNAL( closeEvent(KSocket*) ), SLOT( connectionClosed(KSocket*) ));
>
>  socket->enableWrite(true);
>
>  log()<<"Sleeping"<<endl;
>  sleep(5);
>
>
>  unsigned int bytes;
>  bytes = ::write( socket->socket(), xmlContent.ascii(), xmlContent.length() );
>  log()<<"Written bytes="<<bytes<<endl;
>
>  socket->enableWrite(false);
>  socket->enableRead(true);
>
>  log()<<"Sleeping"<<endl;
>  sleep(5);
>
>
>  char buffer[10000];
>  long len = 10000;
>  if (::read(socket->socket(), buffer, len) == -1)
>  {
>    log()<<"Error while reading"<<endl;
>  }
>  log()<<"Buffer: "<<buffer<<endl;
>
>  delete socket;
>  log()<<"Socket closed"<<endl;
>
>  return true;
>}
>


 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic