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

Discussion in 'Ruby' started by chris guenther, Apr 10, 2005.

  1. 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
     
    chris guenther, Apr 10, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    3,007
    David Van de Voorde
    Jan 21, 2005
  2. Tony Proctor

    ASP Thread Scheduling Anomalies

    Tony Proctor, Mar 2, 2005, in forum: ASP General
    Replies:
    6
    Views:
    277
    Tony Proctor
    Mar 16, 2005
  3. Roeland Moors

    UDP socket delay

    Roeland Moors, Apr 5, 2005, in forum: Ruby
    Replies:
    0
    Views:
    174
    Roeland Moors
    Apr 5, 2005
  4. David Holroyd
    Replies:
    3
    Views:
    527
    David Holroyd
    May 25, 2005
  5. vincent
    Replies:
    0
    Views:
    172
    vincent
    Jun 22, 2005
Loading...

Share This Page