Hi Alan,

When I put in the bind as you suggested it works perfectly.

With my (meager) knowledge of how Jython sockets has to be implemented in Java, I can understand that it would be quite difficult to allow the recvfrom to timeout rather than get an unbound error without the bind statement.  It works that way in Python, but I don't know that this being different would be that much of a problem.  As you said, you cannot receive packets anyway.

But, the following also does not timeout:

#-=-=-=-=-=-=-=-=
host =  'www.jython.org'  # <--- Note the host in this case is the sendto host, not the local host

port = 10000

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.sendto("testdata", (host, port))  # <--I am using this instead of the bind, I believe that this is valid with UDP
s.settimeout(5.0)

#
-=-=-=-=-=-=-=-=

Note that the sendto host and port for this test is not setup.

Shouldn't the sendto cause the local port to bind to whatever local port the OS decides to use?  I don't see why the script needs to know which actual port s is bound to.  I do understand that with the Jython implementation the port does need to be bound because of the way Java handles(or doesn't handle) file descriptors, but I don't think it makes any difference which actual port it is bound to.  The sendto host will be told which host,port the packet came from (in its recvfrom) so that it can send back packets to s.    .
 
The test script should send the packet to he other host and then the recvfrom should wait for the timeout period and then exit if it receives no response.  With UDP you don't know, indeed you can't know, whether the other host is up or not.

Am I completely missing something??

I will open a problem for doc purposes.

Thanks,
  Bob
 

-----Original Message-----
From: Alan Kennedy <jython-dev@xhaus.com>
To: boblusebob@aim.com
Cc: jython-dev@lists.sourceforge.net
Sent: Tue, 4 Mar 2008 11:46 am
Subject: Re: [Jython-dev] UDP Timeout

[Bob]
> I am getting a UDP timeout problem. This happens on release 2.2.1 as well
> as 2.2 maint and trunk. It works as I would expect in Python 2.5.

Hi Bob,

Your script is not working because you are not binding your UDP
socket. Your socket cannot receive datagrams if it is not bound to an
address.

Your socket creation code should look like this

#-=-=-=-=-=-=-=-=
host = 'localhost'

port = 10000

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.bind( (host, port) ) # <-- this is the line you are missing
s.settimeout(5.0)

#
-=-=-=-=-=-=-=-=

However, there IS a flaw in the reporting of this bug; a proper
exception should be raised.

The reason for the failure is the failure of an assertion, like so

#-=-=-=-=-=-=-=-=
def recvfrom(self, num_bytes, flags=None):
try:
assert self.sock_impl # <-- This assertion is failing,
because the socket is not bound
return self.sock_impl.recvfrom(num_bytes, flags)
except java.lang.Exception, jlx:
raise _map_exception(jlx)
#-=-=-=-=-=-=-=-=

You will see this in your code if you modify your try statement to
look like this

#-=-=-=-=-=-=-=-=-=
try:

start = time.clock()

buf, addr = s.recvfrom(2048)

except socket.timeout, to:

print '\nsocket timeout exception', to

print '\nTimer expired in:', time.clock() - start, ' seconds'

break

except socket.error, se:

print '\nsocket.error', se

except AssertionError, ae: # Add this
print '\nAssertion error: %s' % str(ae) # Add this
except Exception, x:

print '\nOther error: %s' % str(x)

#-=-=-=-=-=-=-=-=-=

In order to handle this situation correctly, the _udpsocket.recvfrom
code should probably look something like this

#-=-=-=-=-=-=-=-=
def recvfrom(self, num_bytes, flags=None):
try:
if not self.sock_impl:
raise error(errno.ENOTCONN, 'Datagram socket not bound')
return self.sock_impl.recvfrom(num_bytes, flags)
except java.lang.Exception, jlx:
raise _map_exception(jlx)
#-=-=-=-=-=-=-=-=

I will make this change shortly, and check other circumstances where
assertions are used.

In the meantime, it would be good if you could create a bug for this,
and assign it to me (amak).

Cheers,

Alan.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Jython-dev mailing list
Jython-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-dev