- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
include std/socket.e as s
include std/error.e as err
constant PORT = 5050
constant BACKLOG = 5
procedure die(object result, sequence action)
printf(2, "Error %d in %s", {result, action})
err:crash(action)
end procedure
procedure ensure_zero(integer result, sequence action)
if result != 0 then
die(result, action)
end if
end procedure
function ensure_seq(object result, sequence action)
if atom(result) then
die(result, action)
end if
return result
end function
enum SOCKET, READ, WRITE, ERROR
constant PHRASE = "Hello from Euphoria\n"
procedure client_proc(sequence socket)
while 1 do
for i = 1 to length(PHRASE) do
sequence result = s:select({}, socket, {}, 0, 100)
sequence result_s = result[1]
if result_s[WRITE] = 1 then
s:send(socket, {PHRASE[i]}, 0)
end if
task_yield()
end for
end while
end procedure
constant client_proc_id = routine_id("client_proc")
sequence server = ensure_seq(s:create(s:AF_INET, s:SOCK_STREAM, 0), "create")
ensure_zero(s:bind(server, "127.0.0.1", PORT), "bind")
ensure_zero(s:listen(server, BACKLOG), "listen")
while 1 do
sequence listen_result = s:select(server, {}, {}, 0, 100)
if listen_result[1][READ] = 1 then
sequence client = ensure_seq(s:accept(server), "accept")
printf(1, "Here comes %s\n", {client[2]})
atom task = task_create(client_proc_id, {client[1]})
task_schedule(task, {0.5, 0.5})
end if
task_yield()
end while