# Socket errors

**URL:** <https://www.ruby-forum.com/t/socket-errors/82078>\
**Category:** Ruby\
**Created:** [January 17, 2007, 8:47am UTC](https://www.ruby-forum.com/t/socket-errors/82078 "2007-01-17T08:47:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [January 17, 2007, 8:47am UTC](https://www.ruby-forum.com/t/socket-errors/82078/1 "2007-01-17T08:47:18Z")

</div>

Howdy,

The documentation for Socket#accept gives an example of a ruby script  
to bind to a socket and listen for incoming connections. Unfortunately  
the script fails with the bind method.

Here is the script with the addition of the line to display the  
returned value from the Socket.pack\_sockaddr\_in call.

#!/usr/bin/env ruby

# In one script, start this first

require ‘socket’  
include Socket::Constants  
socket = Socket.new( AF\_INET, SOCK\_STREAM, 0 )  
sockaddr = Socket.pack\_sockaddr\_in( 2200, ‘localhost’ )  
p sockaddr  
socket.bind( sockaddr )  
socket.listen( 5 )  
client, client\_sockaddr = socket.accept  
puts “The client said, ‘#{socket.readline.chomp}’”  
client.puts "Hello from script one!"socket.close

Here is the output;

“\034\036\b\230\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001\000\000\000\000”  
./r.rb:9:in `bind’: Invalid argument - bind(2) (Errno::EINVAL)  
from ./r.rb:9

bruce:~/tmp$ uname -aDarwin carlpowerbook 8.8.0 Darwin Kernel Version  
8.8.0: Fri Sep 8 17:18:57 PDT 2006;  
root:xnu-792.12.6.obj~1/RELEASE\_PPC Power Macintosh powerpc  
bruce:~/tmp$ ruby -vruby 1.8.5 (2006-08-25) [powerpc-darwin8.8.0]

I tried the same code on a Solaris 9 box at work and got a similar  
error message.

However this code works with Linux (ubuntu) with the same version of  
ruyb (1.8.5)

Has anyone heard of this or similar problems before?

thanks,  
Bruce.

---

<div class="post-metadata">

**Author:** ![midnightcoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/edb3f5/32.png) [@midnightcoder](https://www.ruby-forum.com/u/midnightcoder)\
**Post date:** [January 17, 2007, 11:39am UTC](https://www.ruby-forum.com/t/socket-errors/82078/2 "2007-01-17T11:39:47Z")

</div>

thnx bruce…
