From: Brian Candler Date: 2009-07-13T21:30:42+09:00 Subject: Re: TCPServer — send and recieve simultaneous Benedikt Mueller wrote: > Brian Candler wrote: >> Benedikt Mueller wrote: >>> I'm trying to make a >>> sublet(http://unexist.scrapping.cc/projects/subtle/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/.