/tutorial/net/sockets.e

http://github.com/tybor/Liberty · Specman e · 109 lines · 80 code · 8 blank · 21 comment · 3 complexity · ee06075f6ce0106ad0642560e4240f89 MD5 · raw file

  1. class SOCKETS
  2. --
  3. -- This is the first tutorial class to look at, because it gives the most simple examples.
  4. --
  5. create {ANY}
  6. make
  7. feature {}
  8. make
  9. local
  10. tcp: TCP_ACCESS; host: HOST; ip: IPV4_ADDRESS
  11. do
  12. -- The example just tries to connect to a well-known ftp server
  13. create host.make(once "ftp.funet.fi")
  14. create tcp.make(host, 21, True)
  15. test_socket(tcp)
  16. -- Now the following lines start a real server, listening on port 2001.
  17. create ip.make(127, 0, 0, 1)
  18. create tcp.make(ip, 2001, True)
  19. test_server(tcp)
  20. end
  21. test_socket (access: TCP_ACCESS)
  22. -- First tutorial: simple network connection
  23. local
  24. ios: SOCKET_INPUT_OUTPUT_STREAM
  25. do
  26. -- Create a stream connected to the address
  27. ios := access.stream
  28. if ios = Void then
  29. std_error.put_line(access.error)
  30. elseif ios.is_connected then
  31. from
  32. -- actively wait for enough characters to be available
  33. until
  34. ios.can_read_line
  35. loop
  36. end
  37. ios.read_line
  38. io.put_string(ios.last_string)
  39. io.put_new_line
  40. ios.disconnect
  41. else
  42. std_error.put_line("Socket not connected!")
  43. end
  44. end
  45. test_server (access: TCP_ACCESS)
  46. -- Second tutorial: create a server. This one is trivial, see MULTIPLEX_SERVER for a real-life
  47. -- example
  48. local
  49. server: SOCKET_SERVER; job: SOCKET_SERVER_JOB
  50. do
  51. -- About the stack and the sequencer: see the sequencer tutorial
  52. create stack.make
  53. -- Create a server listening at the given address
  54. server := access.server
  55. -- Must test if the server is effectively listening (otherwise there was an error: address in use and
  56. -- so on)
  57. if server.can_connect then
  58. -- Create a job that uses this server
  59. create job.make(server, True)
  60. -- Add a handler: the agent is called by the server when an incoming connection happens
  61. job.when_connect(agent handle_connect(?))
  62. -- Add the job to the sequencer stack
  63. stack.add_job(job)
  64. io.put_string(once "Accepting connections on port ")
  65. io.put_integer(access.port)
  66. io.put_new_line
  67. -- And we start the sequencer loop
  68. stack.run
  69. end
  70. end
  71. feature {}
  72. stack: LOOP_STACK
  73. handle_connect (ios: SOCKET_INPUT_OUTPUT_STREAM)
  74. -- Handle a connection to the server. Acts as an echo server.
  75. local
  76. stop: BOOLEAN; s: STRING
  77. do
  78. from
  79. io.put_string(once "(new connection accepted)%N")
  80. until
  81. stop
  82. loop
  83. from
  84. -- active wait
  85. until
  86. ios.can_read_line
  87. loop
  88. end
  89. ios.read_line
  90. s := ios.last_string
  91. if not s.is_empty then
  92. stop := s.same_as(once "stop")
  93. ios.put_line(s)
  94. end
  95. end
  96. ios.disconnect
  97. io.put_string(once "(connection cancelled)%N")
  98. -- This last line will stop the SERVER!
  99. stack.break
  100. end
  101. end -- class SOCKETS