By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

Select on Win XP

P: n/a
Hi all,
I am trying to run a Python program
on my Win XP box, that I brought over from a Linux box.
Of course, on Linux it works fine, but not so on Win XP.

The program has a button to cause a packet to be sent to a server,
the program also has a thread to read packets from the server.
I send a packet, I get one back, pretty simple.

I am using select() to wait for packets
and it isn't functioning as expected.

select() doesn't return until I have sent a packet
even though I am using a short timeout (1 second)

So whats happening is I send a packet,
select() returns but I have no input yet,
then I have to send the packet again
then select returns and I can read the
response to the first packet.

Hoepfully I am doing something wrong on the Win system.
Any help would be appreciated.

Here's a snippet of the code

from sys import argv
from gtk import *
import socket
import time
import threading
import select
....
my_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
my_sock.bind(('', MY_PORT))

class readerthread(threading.Thread):
def __init__(self):
self._stopevent = threading.Event()
print "thread init"
threading.Thread.__init__(self, name="readerthread")

def run(self):
ilist = []
ilist.append(my_sock)
print "readerthread - my_sock %d" % my_sock.fileno()
while not self._stopevent.isSet():

print "about to select"
il,ol,el = select(ilist,[],[],1)

# read from the socket, etc
if il != []:
data, addr = my_sock.recvfrom(1024)
print "recv() Data length %d" %len(data)
print "recv() Data %s" % data
else:
print "No input from select"

def join(self,timeout=None):
"""
Stop the thread
"""
self._stopevent.set()
threading.Thread.join(self, timeout)
....
def button_cb(button):
my_sock.sendto(data, (SERVERADDR, SERVERPORT))
....
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
[snip description]
Hoepfully I am doing something wrong on the Win system.
Any help would be appreciated.

[snip code]

I checked the raw socket code on my machine and didn't have any issues.
You should check:
....
def button_cb(button):
my_sock.sendto(data, (SERVERADDR, SERVERPORT))
....
with a print statement to make sure that is actually getting called.

- Josiah
Jul 18 '05 #2

P: n/a
Josiah Carlson <jc******@nospam.uci.edu> wrote in message news:<c3**********@news.service.uci.edu>...
[snip description]
Hoepfully I am doing something wrong on the Win system.
Any help would be appreciated.

[snip code]

I checked the raw socket code on my machine and didn't have any issues.
You should check:
...
def button_cb(button):
my_sock.sendto(data, (SERVERADDR, SERVERPORT))
...
with a print statement to make sure that is actually getting called.

- Josiah

Hi,
Thanks for your response.
Yep, I have checked that.
I am running tcpdump, and can see the packet go out
and the response come back.
The python program also dumps the packet
but only after two packets have been sent
does select() return and I can read the first response packet.

Any other ideas why select() is hanging?

David Shifflett
Jul 18 '05 #3

P: n/a
> Hi,
Thanks for your response.
Yep, I have checked that.
I am running tcpdump, and can see the packet go out
and the response come back.
The python program also dumps the packet
but only after two packets have been sent
does select() return and I can read the first response packet.

Any other ideas why select() is hanging?


I'm not terribly familliar with the performance of UDP datagrams, but it
could be that the underlying network driver may be holding your data and
not sending it until it gets more.

My win 2k machine doesn't seem to be having issues, so I don't really know.

Simplifying the code, I got the following (which works on my machine).
Does the below work on your machine? If so, then it is probably not the
socket communication.

- Josiah

#begin code
import socket
import select
import threading
import time

MY_PORT = 9999
MY_ADDR = 'localhost'

timeout = 1

my_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
my_sock.bind((MY_ADDR, MY_PORT))

def server(count):
ilist = []
ilist.append(my_sock)
while count:
il,ol,el = select.select(ilist,[],[],timeout)
if il != []:
data, addr = my_sock.recvfrom(1024)
print "Got data:", data
count -= 1
else:
print "No input from select"

def client(count, data):
for i in xrange(count):
my_sock.sendto(data, (MY_ADDR, MY_PORT))
time.sleep(timeout)

threading.Thread(target=server, args=(10,)).start()
client(10, "hello")

Jul 18 '05 #4

P: n/a
sh*******@nps.navy.mil (D. Shifflett) wrote in message news:<da**************************@posting.google. com>...
Hi all,
I am trying to run a Python program
on my Win XP box, that I brought over from a Linux box.
...
select() doesn't return until I have sent a packet
even though I am using a short timeout (1 second)

ilist = []
ilist.append(my_sock)
print "readerthread - my_sock %d" % my_sock.fileno()
while not self._stopevent.isSet():

print "about to select"
il,ol,el = select(ilist,[],[],1)


One thing you might try this for your ilist:
ilist.append (my_sock.fileno())

In theory it shouldn't matter.
The socket docs say that socket.fileno() is useful for working with select.
It's confusing since docs for select.select() say you can pass in a list
of an objects supporting fileno() or you can pass integer filedescriptors, so
why would socket.fileno() says it's useful for working with select.select()?
I don't know, but comeing from a C background using select, I would eliminate
any confusion and just use select with an integer list.

Let me know if that helps at all.

Yours,
Noah
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.