[Symbian-hacks] Socket Programming sucks ass

Nick Burch
Tue Jan 17 10:26:18 EST 2006


On Tue, 17 Jan 2006, Christopher Schmidt wrote:
> After spending some quality time with my code yesterday afternoon, I 
> realized that all the GPS implementations yet seen suffer from the same 
> problem: tehy can fall far behind the current data. My code suffers much 
> more heavily than Nick's code to this trap, because I'm sleeping and 
> only updating every 10 seconds, so I fall way farther behind, whereas 
> his code may actually be keeping up with the GPS.

One possible route is to have threads:
* one thread reads the data from the socket, passes it, and sets the
    "update the screen" flag if a change is detected
* the other sleeps, waking up every second or two, and checks the "update
    the screen" flag. If this it set, do a redraw

> The way to fix this is to somehow "clear" the socket. One way to do this 
> would be to do some kind of "read all" function, using the select 
> module... however, 'm having lots of problems actually doing this. What 
> seems like it should work just doesn't.

Assuming your GPS is like mine, and always sends a complete sentence (or a 
few) at a time, then try something like:

to_ignore = sock.read(4096)

IIRC, that'll grab everything in the queue, up to 4096 bytes, which should 
clear it

If it doesn't end on a new line though, you'll need to discard the next 
(part) sentence you read normally, since it'll be missing its start

> I'll keep people updated, but just wanted to mention this as it's a
> relatively important issue for socket programming in general, and one
> that I completely forgot about.

It's one I ignored, because mine seemed to be keeping up :) Threading is 
the normal way around this though - split the data gathering from the data 
rendering

Nick


More information about the Symbian-hacks mailing list