TCPServer — send and recieve simultaneous

Discussion in 'Ruby' started by Benedikt Mueller, Jul 11, 2009.

  1. Benedikt Mueller, Jul 11, 2009
    1. Advertisements

  2. Benedikt Mueller

    Roger Pack Guest

    maybe add an
    sfm.skip in there
    though it shouldn't be ncessary.
    My guess is that somehow you don't quite have the right format. You
    could also try watching your packets with wireshark to see what's going
    Good luck!
    Roger Pack, Jul 13, 2009
    1. Advertisements

  3. No idea what protocol you're talking to, but in any case don't use recv
    on a TCP socket. Use either read or gets. will wait until exactly 100 bytes has been read, or the
    other end has closed the connection.

    sfm.gets will wait until a newline has been read, or the other end has
    closed the connection.

    What exactly you should do depends on the protocol - in particular, how
    it marks the end of a reply. Given that the request is terminated by a
    newline, it would make sense for the reply to be terminated by newline
    Brian Candler, Jul 13, 2009
  4. I tried it with but it still don't work. With netcat it
    works: echo "info %a : %t - %R" |nc "" "54311" returns the
    title, artist and time.
    Benedikt Mueller, Jul 13, 2009
  5. I think you probably need sfm.gets.

    If that doesn't work, try reading it byte by byte:

    while true
    ch = sfm.getc
    puts ch.inspect

    Then you can see whether the problem is that the command is not
    generating any response (which means that the problem is in the puts),
    or that the response is not terminated in the way you expect.

    It is possible, although not common, that the server is waiting for you
    to close the connection from your side before it sends a response. If
    so, you need to "half close" it:

    sfm.puts "some command"
    result =

    It's also possible that the server is expecting the command line to be
    terminated with \r\n instead of \n
    Brian Candler, Jul 13, 2009
  6. All 3 tips didn't help :(

    I made it in a standalone script for now:

    require 'socket'
    rescue EPIPE'localhost', 54311)
    # while line =
    sfm.puts "info %a : %t - %R\r\n"
    # sfm.close_write
    bla =
    puts bla

    I ran it with -r debug and here's it:

    ruby -r debug shellfm.rb
    Emacs support available.

    shellfm.rb:1:require 'socket'
    (rdb:1) n
    (rdb:1) n

    You can see: It didn't came until the puts anyway.
    Benedikt Mueller, Jul 13, 2009
  7. That makes no sense. You've put all the useful code under a rescue
    clause - which means it won't run until an exception is raised. That
    program should simply terminate.

    I suggest you use puts bla.inspect instead of puts bla, in case you're
    just getting an empty string back.

    In any case, on my machine (Ubuntu), EPIPE does not exist. The constant
    is Errno::EPIPE. Are you running on Windows perhaps? While you're at it,
    it would be helpful if you said the exact version of Ruby you're using

    If this is ruby-1.9 and Windows, then I don't use either...
    Brian Candler, Jul 13, 2009
  8. Sry I'm totaly new at ruby :)
    I thougt I use rescue bla to don't put an error message.
    Benedikt Mueller, Jul 13, 2009
  9. Oh sure, you need it this way round though:

    .. put the rest of your code here
    rescue Errno::EPIPE
    Brian Candler, Jul 13, 2009
  10. THX
    It works with gets now:
    require 'socket''localhost', 54321)
    sfm.puts "info %a : %t - %R\n"
    puts sfm.gets
    Benedikt Mueller, Jul 13, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.