# Help with Socket (TCPServer) requested!

**URL:** <https://www.ruby-forum.com/t/help-with-socket-tcpserver-requested/114209>\
**Category:** Ruby\
**Created:** [September 9, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/help-with-socket-tcpserver-requested/114209 "2007-09-09T22:19:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 9, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/help-with-socket-tcpserver-requested/114209/1 "2007-09-09T22:19:09Z")

</div>

Hello Team,

I need your help with sockets.

I read the PickAxe information on Socket on Apendix A. I also read an  
article “Sockets programming in Ruby” By: M. Tim Jones. All the examples  
provided worked within their context.  
However, I am new to Socket programming and to Ruby but I am trying to  
establish a simple connection between a TCPServer and a client.

Basically, I would like the server side to start and block itself  
waiting  
for a request from a client.  
The client will connect and request a service such as execute a command  
on  
the servers side and return the output.  
I am having heck of a time trying to get a simple sample program to  
work.  
The connection is actually established, but I don’t see the string that  
I  
sent the server and I don’t get anything back on the client either.

Here is my “server”, please don’t laugh!

require ‘socket’  
port = 19557  
server = TCPServer.new(‘localhost’, port)

while (session = server.accept)  
puts “Inside while…”  
puts session.to\_s  
session.close  
end

This is suppose to be the client:

require ‘socket’  
streamSock = TCPSocket.new( ‘localhost’, 19557 )

streamSock.send( “Hello\n” )  
str = streamSock.recv( 100 )  
print str  
streamSock.close

I am willing to read any documentation recommended.

Thank you

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 9, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/help-with-socket-tcpserver-requested/114209/2 "2007-09-09T22:53:13Z")

</div>

> streamSock.close
> 
> I am willing to read any documentation recommended.
> 
> Thank you

i understand that server should send some data to client? if that’s the  
case then:

> while (session = server.accept)  
> puts “Inside while…”  
> puts session.to\_s # what this line actually does? o\_O probably want  
> to do something like this:  
> session.write(“100 character long array”)  
> session.close  
> end

also sing something else then recv on client end is good idea

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 9, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/help-with-socket-tcpserver-requested/114209/3 "2007-09-09T23:23:16Z")

</div>

From: “Victor R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> This is suppose to be the client:
> 
> require ‘socket’  
> streamSock = TCPSocket.new( ‘localhost’, 19557 )
> 
> streamSock.send( “Hello\n” )  
> str = streamSock.recv( 100 )  
> print str  
> streamSock.close

NOTE: It appears your server neither attempts to send, nor receive  
any data. (For example: data = session.gets; session.puts “got it”)

If your goal is to specifically learn more about the sockets  
API, there are several topics you’ll want to explore:

- low-level (send/recv) vs. high-level (read/write/gets/puts) calls

- blocking vs. non-blocking io semantics

- determining when IO is ready for reading or writing

However, if your goal is to just start _using_ sockets without  
necessarily being burdened by some of the esoteric details and  
trade-offs, you might be interested in EventMachine:

[http://rubyforge.org/projects/eventmachine](http://rubyforge.org/projects/eventmachine)

EventMachine not only makes network I/O easier to deal with,  
it’s also a high performance library. So you won’t be sacrificing  
speed for ease of use.

EventMachine also supports protocol helper modules that you can  
use to mix-in higher level functionality than just sending and  
receiving byte stream bursts.

For example, if you were writing a line-based protocol, like  
POP3, SMTP, or FTP, you could start with:

class MyProtocol \< EventMachine::Protocols::LineAndTextProtocol

```
def receive_line(line)
  # eventmachine LineAndTextProtcol will call your
  # receive_line method when a full line is received
  # for you to process
end

def send_line(line)
  line += "\n" unless line[-1] == ?\n # add EOL if not present
  send_data(line)
end

```

end

EventMachine::start\_server host, port, MyProtocol

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [September 10, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/help-with-socket-tcpserver-requested/114209/4 "2007-09-10T12:48:44Z")

</div>

Thank you. I will try the suggestions provided.

Victor
