{"id":82,"date":"2007-05-07T14:40:33","date_gmt":"2007-05-07T14:40:33","guid":{"rendered":"http:\/\/newblog.mix1009.net\/?p=82"},"modified":"2007-05-07T14:40:33","modified_gmt":"2007-05-07T14:40:33","slug":"ocaml-event%eb%a5%bc-%ec%9d%b4%ec%9a%a9%ed%95%9c-echo-server","status":"publish","type":"post","link":"https:\/\/mix1009.net\/?p=82","title":{"rendered":"ocaml-event\ub97c \uc774\uc6a9\ud55c echo server"},"content":{"rendered":"<p><a href=\"http:\/\/www.xs4all.nl\/%7Emmzeeman\/ocaml\/\">ocaml-event<\/a>(<a href=\"http:\/\/monkey.org\/%7Eprovos\/libevent\/\">libevent<\/a> ocaml wrapper)\ub97c \uc774\uc6a9\ud558\uc5ec echo server\ub97c OCaml \uc5b8\uc5b4\ub85c \uc791\uc131\ud574 \ubd24\uc2b5\ub2c8\ub2e4. libevent C API\uc5d0 \uc775\uc219\ud574\uc11c ocaml \ub2f5\uc9c0 \uc54a\uac8c \uc9dc\uc9c4\uac70 \uac19\uc740 \ub290\ub08c\ub3c4 \ub4e4\uc9c0\ub9cc \uc6cc\ub099 \ud504\ub85c\uadf8\ub7a8\uc774 \uac04\ub2e8\ud558\ub2e4\ubcf4\ub2c8 \ud06c\uac8c \ud2c0\ub824\uc9c8 \ubd80\ubd84\uc740 \uc5c6\uc5b4\ubcf4\uc774\ub294\uad70\uc694. \ub2e4\ub978\uac74 \ubb38\uc81c\uac00 \uc5c6\uc5c8\ub294\ub370 Makefile\uc5d0\uc11c ocamlopt\ub85c native \ucef4\ud30c\uc77c\ud560\ub54c C \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \ub9c1\ud06c\uac00 \uc548\ub418\uc11c \uc57d\uac04 \ud5e4\ub9f8\uace0, printf \ud6c4\uc5d0 flush stdout\uc744 \ud558\ub294 \ubd80\ubd84\uc774 \uc788\ub294\ub370, stdout\uc774 Unix \ub124\uc784\uc2a4\ud398\uc774\uc2a4\uc5d0 \uc788\ub294 stdout\uc73c\ub85c \uc778\uc2dd\uc774 \ub418\uc5b4 \ucef4\ud30c\uc77c\uc774 \uc548\ub418\ub354\uad70\uc694. \uadf8\ub798\uc11c Pervasives.stdout\uc73c\ub85c \uc9c0\uc815\ud558\ub2c8 \ucef4\ud30c\uc77c \uc798\ub418\ub124\uc694.<\/p>\n<p>\uc544\ub798\ub294 echo_server_event.ml \uc18c\uc2a4\uc785\ub2c8\ub2e4.<\/p>\n<p>[CODE type=ocaml]<br \/>\n(*<br \/>\n * echo server using libevent<br \/>\n *<br \/>\n * Copyright (c) 2007 Chun-Koo Park<br \/>\n * All rights reserved.<br \/>\n *)<\/p>\n<p>open Unix<\/p>\n<p>let rec sock_write sock buf offset = function<br \/>\n&nbsp; | 0 -&gt; ()<br \/>\n&nbsp; | len -&gt;<br \/>\n &nbsp; &nbsp;&nbsp; let nwritten = write sock buf offset len in<br \/>\n &nbsp; &nbsp; &nbsp;&nbsp; sock_write sock buf (offset + nwritten) (len &#8211; nwritten)<\/p>\n<p>let echo_callback event fd event_type =<br \/>\n&nbsp; let buf = String.create 64 and nread = ref 1 in<br \/>\n &nbsp;&nbsp; while !nread &gt; 0 do<br \/>\n &nbsp; &nbsp;&nbsp; nread := read fd buf 0 64;<br \/>\n &nbsp; &nbsp;&nbsp; sock_write fd buf 0 !nread;<br \/>\n &nbsp;&nbsp; done;<br \/>\n &nbsp;&nbsp; if !nread &gt; 0 then<br \/>\n &nbsp; &nbsp;&nbsp; Libevent.add event None<br \/>\n &nbsp;&nbsp; else<br \/>\n &nbsp; &nbsp;&nbsp; Printf.printf &#8220;connection closed\\n&#8221;;<br \/>\n &nbsp; &nbsp;&nbsp; flush Pervasives.stdout<\/p>\n<p>let accept_callback event fd event_type =<br \/>\n&nbsp; let asock, addr = accept fd in<br \/>\n&nbsp; let evnew = Libevent.create () in<\/p>\n<p>&nbsp; Libevent.set evnew asock [Libevent.READ] false (echo_callback evnew);<br \/>\n&nbsp; Libevent.add evnew None;<\/p>\n<p>&nbsp; Libevent.add event None<\/p>\n<p>let tcp_server_sock port =<br \/>\n&nbsp; let ssock = socket PF_INET SOCK_STREAM 0<br \/>\n&nbsp; and addr = inet_addr_any in<br \/>\n &nbsp;&nbsp; bind ssock (ADDR_INET (addr, port));<br \/>\n &nbsp;&nbsp; setsockopt ssock SO_REUSEADDR true;<br \/>\n &nbsp;&nbsp; listen ssock 5;<br \/>\n &nbsp;&nbsp; ssock<\/p>\n<p>let _ =<br \/>\n&nbsp; let listenport = 2007 in<br \/>\n&nbsp; let acceptfd = tcp_server_sock listenport in<br \/>\n&nbsp; let acceptev = Libevent.create () in<\/p>\n<p>&nbsp; Libevent.set acceptev acceptfd [Libevent.READ] false (accept_callback acceptev);<br \/>\n&nbsp; Libevent.add acceptev None;<\/p>\n<p>&nbsp; Libevent.dispatch ()<br \/>\n[\/HTML][\/CODE]<\/p>\n<p>ocaml-event\ub97c \uc774\uc6a9\ud55c echo \uc11c\ubc84\uc640 thread\uc640 fork\ub85c \uc9e0 echo \uc11c\ubc84\uc758 Makefile\uc785\ub2c8\ub2e4. ocaml findlib\ub97c \uc774\uc6a9\ud588\uc2b5\ub2c8\ub2e4. <a href=\"http:\/\/mix1009.net\/71\">ocaml findlib\uc5d0 \ub300\ud574\uc11c \uc5bc\ub9c8\uc804\uc5d0 \uc62c\ub9b0\uae00<\/a>\uc744 \ucc38\uace0\ud558\uc138\uc694.<\/p>\n<p>[CODE type=makefile]<br \/>\n# Makefile<br \/>\n#<br \/>\n# Copyright (c) 2007 Chun-Koo Park<br \/>\n# All rights reserved.<\/p>\n<p>OCAMLC=ocamlfind ocamlc<br \/>\n#OCAMLC=ocamlfind ocamlopt<\/p>\n<p>EXEC=echo_server_fork echo_server_thread echo_server_event<br \/>\nall: $(EXEC)<\/p>\n<p>echo_server_fork: echo_server_fork.ml<br \/>\n &nbsp;&nbsp; $(OCAMLC) -o $@ -package unix -linkpkg $@.ml<\/p>\n<p>echo_server_thread: echo_server_thread.ml<br \/>\n &nbsp;&nbsp; $(OCAMLC) -o $@ -thread -package &#8220;threads unix&#8221; -linkpkg $@.ml<\/p>\n<p>echo_server_event: echo_server_event.ml<br \/>\n &nbsp;&nbsp; $(OCAMLC) -o $@ -package &#8220;libevent unix&#8221; -linkpkg $@.ml -ccopt -L\/usr\/local\/lib -cclib -levent<\/p>\n<p>clean:<br \/>\n &nbsp;&nbsp; rm -f *.cmo *.cmi *.cmx *.o $(EXEC)<\/p>\n<p># vi:set noet:<br \/>\n[\/HTML][\/CODE]<\/p>\n<p>thread \uc0ac\uc6a9\ud560\ub54c\ub294 -thread \uc635\uc158\uc744 \uc8fc\uace0, ocamlopt\ub85c native \ucef4\ud30c\uc77c\ud560 \uacbd\uc6b0\ub97c \uc704\ud574\uc11c -cclib\ub85c libevent C \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc9c0\uc815\ud574\uc8fc\uc5c8\uc2b5\ub2c8\ub2e4. -cclib event\ub85c \uc9c0\uc815\ud558\uba74 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \ucc3e\uc744\uac70\ub77c \uc0dd\uac01\ud588\ub294\ub370 -cclib -levent \ud615\ud0dc\ub85c \ub77c\uc774\ube0c\ub7ec\ub9ac \uc9c0\uc815\uc744 \ud574\uc57c\ub418\ub354\uad70\uc694. ocamlc\ub85c byte compile\ud560\ub54c\ub294 -cclib \ub97c \uc9c0\uc815\ud558\uc9c0 \uc54a\uc544\ub3c4 \ub429\ub2c8\ub2e4.<\/p>\n<p>Ocaml\ub85c \uc791\uc131\ud55c \uc138\uac00\uc9c0 echo server \uc18c\uc2a4\uc785\ub2c8\ub2e4. ( libevent \/ fork \/ thread )<\/p>\n<p><a href=\"https:\/\/mix1009.net\/wp-content\/uploads\/1\/1402904692.tgz\" class=\"alignleft\"  \/>1402904692.tgz<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ocaml-event(libevent ocaml wrapper)\ub97c \uc774\uc6a9\ud558\uc5ec echo server\ub97c OCaml \uc5b8\uc5b4\ub85c \uc791\uc131\ud574 \ubd24\uc2b5\ub2c8\ub2e4. libevent C API\uc5d0 \uc775\uc219\ud574\uc11c ocaml \ub2f5\uc9c0 \uc54a\uac8c \uc9dc\uc9c4\uac70 \uac19\uc740 \ub290\ub08c\ub3c4 \ub4e4\uc9c0\ub9cc \uc6cc\ub099 \ud504\ub85c\uadf8\ub7a8\uc774 \uac04\ub2e8\ud558\ub2e4\ubcf4\ub2c8 \ud06c\uac8c \ud2c0\ub824\uc9c8 \ubd80\ubd84\uc740 \uc5c6\uc5b4\ubcf4\uc774\ub294\uad70\uc694. \ub2e4\ub978\uac74 \ubb38\uc81c\uac00 \uc5c6\uc5c8\ub294\ub370 Makefile\uc5d0\uc11c ocamlopt\ub85c native \ucef4\ud30c\uc77c\ud560\ub54c C \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \ub9c1\ud06c\uac00 \uc548\ub418\uc11c \uc57d\uac04 \ud5e4\ub9f8\uace0, printf \ud6c4\uc5d0 flush stdout\uc744 \ud558\ub294 \ubd80\ubd84\uc774 \uc788\ub294\ub370, stdout\uc774 Unix \ub124\uc784\uc2a4\ud398\uc774\uc2a4\uc5d0 \uc788\ub294 stdout\uc73c\ub85c \uc778\uc2dd\uc774 \ub418\uc5b4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[38,79],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-19","tag-ocaml","tag-ocaml-event"],"_links":{"self":[{"href":"https:\/\/mix1009.net\/index.php?rest_route=\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mix1009.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mix1009.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mix1009.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mix1009.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":0,"href":"https:\/\/mix1009.net\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/mix1009.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mix1009.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mix1009.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}