TCPSocket, send always transmit an even length message

B

Borna N.

Hello all :)

Just a quick and obscure bug report

Console 1:

$ irb
require 'socket' => true
s = TCPSocket.open('localhost', 2000)
=> # said:
s.send('ab', 2) => 2
s.send('cde', 3)
=> 3

Console 2:
$ nc -l 2000
abcd

Write seems to work OK and puts too, but I could swear I had the same
issue with puts on another Ubuntu box with same specs and ruby 1.8.7.
I'm using Ubuntu 10.10, 2.6.35-27 kernel and ruby 1.9.2-p180 and ruby
1.8.7.
 
B

Borna N.

...this is not a flushing issue btw, adding flush at the end yields the
same result (letter e never shows up).
 
B

Brian Candler

I think you're using the wrong method.

'send' is a inherited method of BasicSocket, and calls 'sendto' in the
underlying O/S. And the second argument is actually flags, not length.

/*
* call-seq:
* basicsocket.send(mesg, flags [, dest_sockaddr]) => numbytes_sent
*
* send _mesg_ via _basicsocket_.
*
* _mesg_ should be a string.
*
* _flags_ should be a bitwise OR of Socket::MSG_* constants.
*
* _dest_sockaddr_ should be a packed sockaddr string or an addrinfo.
*
* TCPSocket.open("localhost", 80) {|s|
* s.send "GET / HTTP/1.0\r\n\r\n", 0
* p s.read
* }
*/

Try setting flags to zero instead and it'll probably work:
require 'socket' => true
rx = TCPServer.new('127.0.0.1',2000)
=> # said:
tx = TCPSocket.new('127.0.0.1',2000)
=> # said:
s = rx.accept
=> # said:
tx.send('ab', 0) => 2
tx.send('cde', 0) => 3
s.readpartial(1024) => "abcde"
RUBY_DESCRIPTION => "ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]"

But really you should just be using 'write' not 'send', unless you're
trying to do something funky like sending out-of-band data (MSG_OOB)
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top