Help with Socket (TCPServer) requested!

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

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

From: “Victor R.” [email protected]

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

    • for instance: send & recv are not guaranteed to process as many
      bytes as you ask, but will tell you how many they DID process
  • blocking vs. non-blocking io semantics

  • determining when IO is ready for reading or writing

    • select() vs. threads

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

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

Thank you. I will try the suggestions provided.

Victor

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs