Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:15:25+00:00 2026-06-15T23:15:25+00:00

The connect method fails giving an EINVAL when I am trying to connect from

  • 0

The connect method fails giving an EINVAL when I am trying to connect from blue.example.edu to green.example.edu

I do not get this error when I connect from blue to blue itself.

The Ruby documentation under connect suggests

Errno::EINVAL – the address length used for the sockaddr is not a
valid length for the address family or there is an invalid family in
sockaddr

But since the same parameter is working correctly when I try connecting blue to another port on itself, this might be wrong ?

Here is the code snippet:

MY_NAME = Socket.gethostname
SERVER_NAME = "blue"    # Socket.gethostname returns 'blue' on the server
SERVER_IP = Socket::getaddrinfo(SERVER_NAME, 'www', nil, Socket::SOCK_STREAM)[0][3]  
#--> Gives me A.B.C.27 (see note on ipconfig below)

SERVER_LISTENING_PORT = 34900

MY_NAME = Socket.gethostname
MY_PORT_FOR_REQUESTING_SERVER = 40000

# other stuff

#CREATE
client_socket = Socket::new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
client_socket_addr = Socket.pack_sockaddr_in(MY_PORT_FOR_REQUESTING_SERVER, MY_NAME)

#BIND
client_socket.bind(client_socket_addr)

#CONNECT
server_addr = Socket.pack_sockaddr_in(SERVER_LISTENING_PORT, SERVER_NAME)
i_tried_with_ip_address_also = Socket.pack_sockaddr_in(SERVER_LISTENING_PORT, SERVER_IP)

#The line below raises the error
client_socket.connect(server_addr)

# more code below

Please note that hitting ifconfig on blue yields the same A.B.C.27 what the SERVER_IP contains

Under eth1 - inet addr:A.B.C.27, Bcast:A.B.C.255 Mask:255.255.255.0

I’m guessing this is related to the issue ?

Using Ruby 1.8.7 on Ubuntu machines

Please advice.


EDIT:

I used TCPSocket as suggested by zed_0xff in order to get this working temporarily.
However I wish to know why the regular socket way is not working.

The only difference I can see is that using the Socket class, I am binding the client_socket_addr to a port from which I wish to send my data.
TCPSocket sends data from a random port to the specified listening port on the server.
What do I do if I wish to bind the sending to a specific port ?


EDIT2: Changed question title from:

Ruby Sockets: Errno::EINVAL: Invalid argument – connect(2)

So the answer from @zed_0xff has helped me tackle one of my problems of actually establishing a connection between the client and the server (Thank you!)

However, with that method, TCPSocket chooses a random sending port for sending data. Trying to binding the sending port to a particular port number fails.


PS: Please let me know if modifying the question name is against the community guidelines. Since my issue was only partially resolved, I edited the title it here itself and explained the edits in the “Edit” sections.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:15:27+00:00Added an answer on June 15, 2026 at 11:15 pm

    Getting EINVAL for bind() indicates that the address to bind to might still be in use.

    To get around this

    • either use a different address (ip-address and/or port)
    • or if you are wanting to reuse a address, set the socket option SO_REUSEADDR on the socket prior to using it
    • or do not bind … 😉
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When a user tries to connect via this method and it fails. How do
I can't figure out why this is. I have a connect method that works
Here is my method to connect to server and get the response in MyUtilitiesActivity
I'm trying to connect an app I have built to a MongoHQ Database. This
Trying to connect with Gmail smtp server fails. It just hangs there ~20 seconds
I'm trying write a test for a method that returns a java.sql.Connection to connect
I want to connect to a URL with the socket method ( http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=VS.96).aspx );
At my company, our current method of updating the database is to connect using
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I'm trying to connect to a Zend-PHP service within a pure ActionScript program. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.