Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > TCPServer — send and recieve simultaneous

Reply
Thread Tools

TCPServer — send and recieve simultaneous

 
 
Benedikt Mueller
Guest
Posts: n/a
 
      07-11-2009
Hi
I'm trying to make a
sublet(http://unexist.scrapping.cc/projects...e/wiki/Sublets) the
title, artist and time from shell-fm. My code:http://sprunge.us/QheF?rb
But I didn't recieve anything. With puts("skip") there is no problem, it
skips the track, so the connection cannot be breaken. Can you help me?
--
linopolus
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Roger Pack
Guest
Posts: n/a
 
      07-13-2009
Benedikt Mueller wrote:
> Hi
> I'm trying to make a
> sublet(http://unexist.scrapping.cc/projects...e/wiki/Sublets) the
> title, artist and time from shell-fm. My code:http://sprunge.us/QheF?rb
> But I didn't recieve anything. With puts("skip") there is no problem, it
> skips the track, so the connection cannot be breaken. Can you help me?


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
on.
Good luck!
=r
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      07-13-2009
Benedikt Mueller wrote:
> I'm trying to make a
> sublet(http://unexist.scrapping.cc/projects...e/wiki/Sublets) the
> title, artist and time from shell-fm. My code:http://sprunge.us/QheF?rb
> But I didn't recieve anything. With puts("skip") there is no problem, it
> skips the track, so the connection cannot be breaken. Can you help me?


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.

sfm.read(100) 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
too.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Benedikt Mueller
Guest
Posts: n/a
 
      07-13-2009
Brian Candler wrote:
> Benedikt Mueller wrote:
>> I'm trying to make a
>> sublet(http://unexist.scrapping.cc/projects...e/wiki/Sublets) the
>> title, artist and time from shell-fm. My code:http://sprunge.us/QheF?rb
>> But I didn't recieve anything. With puts("skip") there is no problem, it
>> skips the track, so the connection cannot be breaken. Can you help me?

>
> 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.
>
> sfm.read(100) 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
> too.

I tried it with sfm.read(100) but it still don't work. With netcat it
works: echo "info %a : %t - %R" |nc "127.0.0.1" "54311" returns the
title, artist and time.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-13-2009
Benedikt Mueller wrote:
> Brian Candler wrote:
>> Benedikt Mueller wrote:
>>> I'm trying to make a
>>> sublet(http://unexist.scrapping.cc/projects...e/wiki/Sublets) the
>>> title, artist and time from shell-fm. My code:http://sprunge.us/QheF?rb
>>> But I didn't recieve anything. With puts("skip") there is no problem, it
>>> skips the track, so the connection cannot be breaken. Can you help me?

>>
>> 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.
>>
>> sfm.read(100) 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
>> too.

> I tried it with sfm.read(100) but it still don't work. With netcat it
> works: echo "info %a : %t - %R" |nc "127.0.0.1" "54311" returns the
> title, artist and time.


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
end

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"
sfm.close_write
result = sfm.read(100)

It's also possible that the server is expecting the command line to be
terminated with \r\n instead of \n
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Benedikt Mueller
Guest
Posts: n/a
 
      07-13-2009
All 3 tips didn't help

I made it in a standalone script for now:

require 'socket'
begin
rescue EPIPE
sfm=TCPSocket.open('localhost', 54311)
# while line = sfm.read(100)
sfm.puts "info %a : %t - %R\r\n"
# sfm.close_write
bla = sfm.read(100)
puts bla
sfm.close
end

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

ruby -r debug shellfm.rb
Debug.rb
Emacs support available.

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

You can see: It didn't came until the puts anyway.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-13-2009
Benedikt Mueller wrote:
> All 3 tips didn't help
>
> I made it in a standalone script for now:
>
> require 'socket'
> begin
> rescue EPIPE
> sfm=TCPSocket.open('localhost', 54311)
> # while line = sfm.read(100)
> sfm.puts "info %a : %t - %R\r\n"
> # sfm.close_write
> bla = sfm.read(100)
> puts bla
> sfm.close
> end


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
too.

If this is ruby-1.9 and Windows, then I don't use either...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Benedikt Mueller
Guest
Posts: n/a
 
      07-13-2009
Brian Candler wrote:
> 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.

Sry I'm totaly new at ruby
I thougt I use rescue bla to don't put an error message.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-13-2009
Benedikt Mueller wrote:
> I thougt I use rescue bla to don't put an error message.


Oh sure, you need it this way round though:

begin
.. put the rest of your code here
rescue Errno::EPIPE
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Benedikt Mueller
Guest
Posts: n/a
 
      07-13-2009
THX
It works with gets now:
require 'socket'
sfm=TCPSocket.open('localhost', 54321)
sfm.puts "info %a : %t - %R\n"
puts sfm.gets
sfm.close
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TCPServer and IPV6 Une Bévue Ruby 0 11-19-2012 05:27 PM
encode send and recieve xml data across asp.net pages vicky ASP .Net 3 11-22-2010 10:16 AM
Re: logging via SocketHandler and TCPserver Vinay Sajip Python 1 07-15-2008 01:45 PM
SockerServer.TCPServer problem huy Python 3 08-17-2004 04:30 PM
newbie help...is it possible to send and recieve fax transmissions using my PC? the niner nation Computer Support 5 04-15-2004 06:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57