# Socket problem, Errno::EINVAL. Can someone explain?

**URL:** <https://www.ruby-forum.com/t/socket-problem-errno-einval-can-someone-explain/130566>\
**Category:** Rails\
**Created:** [February 29, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/socket-problem-errno-einval-can-someone-explain/130566 "2008-02-29T13:02:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 29, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/socket-problem-errno-einval-can-someone-explain/130566/1 "2008-02-29T13:02:26Z")

</div>

Hi,  
I am having trouble with sockets on my development machine (windows &  
cygwin). I’ve done a quick test on a linux box and it doesn’t seem to  
show the same behaviour, i.e. it works.  
Can someone explain why the following error is thrown on windows and not  
on linux and if there is a workaround to fix it.

–environment.rb----  
##define a socket. I am defining it in environment.rb because I am  
going to use the socket in some of the models & also a script (running  
via script/runner)  
begin  
$socket = UDPSocket.open  
$socket.bind("",1680)  
rescue Errno::EADDRINUSE  
puts “Error: #{$!}”  
end

Open two consoles:  
----Console 1-----  
$ ./script/console  
Loading development environment (Rails 2.0.2)

> > $socket  
> > =\> #UDPSocket:0x2b5c71558e80

----Console 2-----  
$ ./script/console  
Loading development environment (Rails 2.0.2)  
Error: Address already in use - bind(2)

> > $socket  
> > =\> #UDPSocket:0x2b5c71558e80
> 
> > $socket.recvfrom(512)  
> > Errno::EINVAL: Invalid argument - recvfrom(2)  
> > from (irb):1:in `recvfrom’  
> > from (irb):1

Why can the second console see the socket but throw an error when I try  
and use it. Linux doesn’t seem to do this, i.e. I can use the socket in  
either window.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [February 29, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/socket-problem-errno-einval-can-someone-explain/130566/2 "2008-02-29T13:30:15Z")

</div>

On 29 Feb 2008, at 12:02, Tim C. wrote:

> –environment.rb----  
> Open two consoles:
> 
> > > $socket  
> > > =\> #UDPSocket:0x2b5c71558e80
> > 
> > > $socket.recvfrom(512)  
> > > Errno::EINVAL: Invalid argument - recvfrom(2)  
> > > from (irb):1:in `recvfrom’  
> > > from (irb):1

The exception occurs when you bind the socket, not when you create it,  
so $socket exists in both cases, it’s just not bound in the second  
console.

Fred
