Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / bench / shootout / echo.ruby
blob3e6362c655efe0031a9d39b12662fc2b02da7e23
1 #!/usr/bin/ruby
2 # -*- mode: ruby -*-
3 # $Id: echo.ruby,v 1.1.1.1 2004-05-19 18:09:37 bfulgham Exp $
4 # http://www.bagley.org/~doug/shootout/
6 require "socket"
8 DATA = "Hello there sailor\n"
10 def echo_client(n, port)
11 sock = TCPsocket.open('127.0.0.1', port)
12 n.times do
13 sock.write(DATA)
14 ans = sock.readline
15 if ans != DATA then
16 raise sprintf("client: \"%s\" \"%s\"", DATA, ans)
17 end
18 end
19 sock.close
20 end
23 def echo_server(n)
24 ssock = TCPserver.open('127.0.0.1', 0)
25 port = ssock.addr[1]
26 if pid = fork then
27 # parent is server
28 csock = ssock.accept
29 n = 0
30 while str = csock.gets
31 n += csock.write(str)
32 end
33 Process.wait
34 printf "server processed %d bytes\n", n
35 else
36 # child is client
37 echo_client(n, port)
38 end
39 end
41 echo_server(Integer(ARGV.shift || 1))