I want to send a string from a client to a server via socket. I tried doing this three ways (write, puts and print)
hostname = 'localhost'
port = 3000
s = TCPSocket.new(hostname, port)
s.write("hello")
s.puts("hello")
s.print("hello")
s.close
but it gives me an error (the error does not appear on the browser but on the terminal):
ERROR bad Request-Line 'hello'.
Is it possible to send strings from the client to the server or is it only from server to client?
I am using Ruby 1.8.7 and Rails 3.0.1
It looks like the server doesn’t like your
Helloline. You mention Ruby and Rails, I guess you want to talk toWEBrickHTTP server – why don’t you speak HTTP then?If you want to implement HTTP client, take a look at http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm
Under
A Tiny Web Browsersection you’ll find a working HTTP client usingTCPSocket, as well as Net::HTTP example.If HTTP is what you need,
Net::HTTPis the way to go, implementing HTTP sounds like reinventing the wheel.