# TCP Socket Programming

**URL:** <https://www.ruby-forum.com/t/tcp-socket-programming/147887>\
**Category:** Ruby\
**Created:** [September 6, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/tcp-socket-programming/147887 "2008-09-06T15:48:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mac20](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mac20](https://www.ruby-forum.com/u/mac20)\
**Post date:** [September 6, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/tcp-socket-programming/147887/1 "2008-09-06T15:48:35Z")

</div>

Hi

I am trying to establish communication between 2 applications.  
On executing the below piece of code on client side application, I get  
this error

socket\_client.rb:3:in `initialize': Bad file descriptor - connect(2) (Errno::EBADF) from socket_client.rb:3:in`new’

#########  
code:  
#########

require ‘socket’

client\_side = TCPSocket.new(‘localhost’,1045)  
puts "Enter some text: "  
text=gets()

Server side code works fine and is implemented as displayed below.  
require ‘socket’

###########  
code:  
###########

server\_side=TCPServer.new(‘localhost’,‘1025’)  
port = server\_side.accept  
data = port.recvfrom(256)[0]  
puts data

Any suggestions on what could be the possible problem?  
PLease Help

Macman

---

<div class="post-metadata">

**Author:** ![mac20](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mac20](https://www.ruby-forum.com/u/mac20)\
**Post date:** [September 6, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/tcp-socket-programming/147887/2 "2008-09-06T19:52:26Z")

</div>

Mac Man wrote:

> client\_side = TCPSocket.new(‘localhost’,1045)  
> server\_side=TCPServer.new(‘localhost’,‘1025’)

Different ports 🙂 And I’m not sure the string ‘1025’ will be  
interpreted as a numeric port–maybe so.

Also, it seems like you are not doing anything with the client\_side  
variable after you assign to it.

---

<div class="post-metadata">

**Author:** ![mac20](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19b73/32.png) [@mac20](https://www.ruby-forum.com/u/mac20)\
**Post date:** [September 6, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/tcp-socket-programming/147887/3 "2008-09-06T20:56:38Z")

</div>

sorry…that was a mistake…typo error.  
both client and server are pointing to the same port 1045.

Actually the code goes this way…

client\_side = TCPSocket.new(‘localhost’,‘1045’)  
print "Enter some text: "  
text=gets()  
client\_side.write(text)

I am writing the input text to the server side.

An issue I found was that this error occurs only in version 1.8.6.  
I tried the same code on version 1.8.1 and it worked, but got a  
different error as below.

in ‘recvfrom’ : getnameinfo: ai\_family not supported. (SocketError)
