java - Receiving Data On Sinatra Server -
i able send data sinatra server (strings) i'm not sure how receive in sinatra code. help?
java client (what i'm using send data) code:
private static void contactserver() { try { string text = "this text please work"; socket sock = new socket("localhost", 4567); outputstream os = sock.getoutputstream(); url url = new url("http://localhost:4567/hello"); printwriter writer = new printwriter(os); writer.flush(); writer.write(text); url.openstream(); system.out.println("done"); string strtemp = ""; /*while(null != (strtemp = br.readline())){ system.out.println(strtemp); }*/ } catch (exception e) { e.printstacktrace(); } }
that's not going work. sinatra's server understands , speaks language called the http protocol, sending string:
"this text please work"
through socket sinatra app hopeless prayer.
a protocol set of rules specify how client , server speak each other--then each party can understand other party saying. http protocol, clients send known a request
, , servers reply known a response
. request , response must formatted precisely according rules specified http protocol. gory details request here:
http://www.w3.org/protocols/rfc2616/rfc2616-sec5.html
so java program needs send a request
sinatra's server, string formatted in precise manner. here simple request looks like:
get /path/to/page http/1.1 host: localhost:4567
if want make request page:
http://localhost:4567/page1
(i.e. hit sinatra route get '/page1'
)
...then simple request page like:
get /page1 http/1.1 host: localhost:4567
also, must end every line in http request "\r\n" no matter os using. 2 characters part of http protocol. furthermore, after last header there must blank line signified "\r\n", this:
get /page1 http/1.1\r\nhost: localhost:4567\r\n\r\n
here java:
import java.io.*; import java.net.*; public class sinatra { private static void contactserver() { try { socket sock = new socket("localhost", 4567); outputstream os = sock.getoutputstream(); printwriter writer = new printwriter(os); string[] text = { "get /page1 http/1.1", "host: localhost:4567", }; string request = ""; for(int i=0; < text.length; ++i) { request += text[i] + "\r\n"; } request += "\r\n"; system.out.println(request); writer.write(request); writer.flush(); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { sinatra.contactserver(); } }
note: first few times ran java program, server(which started $ ruby myapp.rb
) threw following error:
[2013-08-19 20:10:11] error errno::econnreset: connection reset peer /users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:80:in `eof?' /users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:80:in `run' /users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
but when ran java program few more times, server behaved. have no idea causing error. when entered url in firefox, server never threw error. used firebug @ request firefox sends; used same request headers in java program, server still threw error.
edit: can server error go away making java program sleep 1 second before closing socket. socket closes when explicitly close socket or when program ends. without sleep, think socket closes while server still processing request. because browser keeps socket open, browser never causes server throw error.
the same server error occurs ruby client:
require 'socket' port = 4567 host = 'localhost' s = tcpsocket.new host, port req = [ "get /page1 http/1.1", "host: localhost:4567", "accept: */*", ] req = req.join("\r\n") << ("\r\n" * 2) print req s.write req s.flush #sleep(1) s.close
and fix same. niggling detail why curl
unix command doesn't cause server throw error:
$ curl -v http://localhost:4567/page1 * connect() localhost port 4567 (#0) * trying 127.0.0.1... connected * connected localhost (127.0.0.1) port 4567 (#0) > /page1 http/1.1 > user-agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 openssl/0.9.8r zlib/1.2.3 > host: localhost:4567 > accept: */* > < http/1.1 200 ok < content-type: text/html;charset=utf-8 < content-length: 0 < x-xss-protection: 1; mode=block < x-content-type-options: nosniff < x-frame-options: sameorigin < server: webrick/1.3.1 (ruby/1.9.3/2012-04-20) < date: tue, 20 aug 2013 04:59:16 gmt < connection: keep-alive < * connection #0 host localhost left intact * closing connection #0
with -v option, curl prints out request , response. using curl make request, never saw sever throw error. wonder if curl sleep too?
Comments
Post a Comment