Archive for July, 2009

Python: send/receive strings/base types over a socket

import sys
import socket
import struct


def recvSimple(socket, typeDesc) :
 data = socket.recv(struct.calcsize(typeDesc))
 return struct.unpack(typeDesc, data)[0]

def sendSimple(socket, typeDesc, value) :
 data = struct.pack(typeDesc, value);

def recvString(socket):
 slen = recvSimple(socket, 'i')
 return s.recv(slen)

def sendString(socket, stringdata):
 sendSimple(s, 'i', len(stringdata))

HOST = 'localhost' # Symbolic name meaning all available interfaces
PORT = 25250 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

print "Connected to", HOST, ":", PORT

subscribeMessage = {}
subscribeMessage['type'] = SUBSCRIBE
subscribeMessage['id'] = 'IO'
subscribeMessage['property'] = 'myVeryCoolIO'
sendSimple(s, 'i', subscribeMessage['type'])
sendString(s, subscribeMessage['id'])
sendString(s, subscribeMessage['property'])
sendSimple(s, 'd', 0)
sendSimple(s, 'd', 0)
sendSimple(s, 'd', 0)

while 1:
 message = {}
 message['type'] = recvSimple(s, 'i')
 message['id'] = recvString(s)
 message['property'] = recvString(s)
 print "type: ", message['type']
 print "id: ", message['id']
 print "prop: ", message['property']
 d = recvSimple(s, 'd')
 d = recvSimple(s, 'd')
 d = recvSimple(s, 'd')

Saturday, July 11th, 2009 Uncategorized No Comments

How do I fix this high CPU usage with svchost?

Sometimes in Windows the svchost.exe process uses the entire CPU. Often this is related to the automatic windows update service.

A workaround is to disable the automatic windows update service.  (via Control panel->Administrative tools->Services)

via How do I fix this high CPU usage svchost virus or whatever it is? – Ask Leo!.

Friday, July 10th, 2009 Uncategorized No Comments

Glade (GTK+): adding a new page to a notebook

Adding a new page to an existing notebook in glade is quite tricky the first time. One way to do it:

  • Select the top level widget of the page before/after which you want to add the new page.
  • Right click this widget and you will see two menu entries: “Insert page after” and “Insert page before”

Another way to do it:

  • Cut the label of the tab after/before which you want to insert the new page.
  • Right click the notebook and you will see two menu entries: “Insert page after” and “Insert page before”
  • Once you added the page, paste back the label where it belongs.
Friday, July 10th, 2009 Uncategorized No Comments

Updating GTK+ widgets in a multithreaded program

Normally, GTK+ widgets may only be touched from the main thread.

If you want to be able to update a GTK+ widget from a subthread, you have to do some extra setup work. In your main:

main (int argc, char *argv[])
  GtkWidget *window;

  g_thread_init (NULL);
  gdk_threads_init ();
  gdk_threads_enter ();

  gtk_init (&argc, &argv);

  window = create_window ();
  gtk_widget_show (window);

  gtk_main ();
  gdk_threads_leave ();

  return 0;

And in the thread:

gdk_threads_enter ();

          // update your widget here,
          // e.g. gtk_label_set_text (myWidget, "sometext");

	  /* release GTK thread lock */
	  gdk_threads_leave ();

And don’t forget to add -lgthread-2.0 to your linker line.


Thursday, July 9th, 2009 Uncategorized 1 Comment