weird delay of UDP server (seems to be thread-scheduling issue)

C

chris guenther

Hi,

the UDP client/server progam below does not work as expected.
Given one input line ('df' see below) the server-thread does not (YET)
receive the UDP packet, although the client thread had sent it already.
ONLY after entering another bunch of empty lines (just hitting <ENTER>) the
receiver thread suddenly receives the packet.

It seems to me that the receiver thread is not scheduled at the time I
expect it to be scheduled (right after sending).

Can anyone throw some light on this issue ??

TIA,
Chris



#--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=
#!/usr/bin/env ruby

require 'socket'
require 'time'

STDOUT.sync=true
$port = 4321
def now; Time.new.strftime('%H:%M:%S') ;end

sThread = Thread.start do
puts "server : bound port #{$port} at #{now}\n"
server = UDPSocket.open
server.bind(nil, $port)
loop { puts "#{server.recvfrom(1024)[0]}\treceived at #{now}" }
end

sock = UDPSocket.open
sock.connect('localhost', $port)
loop {
print "--> "
case line = readline.chomp
when /^q/
exit 0
else
if line.length>0 ### Dont send EMPTY LINES
payload="'#{line}'\tsent at #{now}"
sock.send(payload, 0)
puts "sent ::\t#{payload}"
end
end
}

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

############ trace of execution ############

# ruby -v -w udp_test.rb
ruby 1.8.2 (2004-12-25) [i386-mswin32]
server : bound port 4321 at 11:01:02
--> df
sent :: 'df' sent at 11:01:05
-->
-->
-->
-->
-->
-->
-->
-->
-->
--> 'df' sent at 11:01:05 received at 11:01:09
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top