问题描述
我想为我的OUnit测试写一个小的服务器,当客户端连接到套接字时,将数据发送到客户端并退出.
I wanted to write for my OUnit tests a little server that when a client connect on a socket, send data to the client and quit.
因为我想在测试中使用它,所以我认为应该在线程中创建服务器,并在线程中创建客户端.所以这是代码:
Because I wanted to use this in my tests, I thought that I should create a server in a thread and create a client in a thread. So here is the code:
open Sys
open Unix
open Lwt
(* a simple server that send a message when a client connects
* compile with
* ocamlfind ocamlc -o lwt_server -package lwt,lwt.unix,unix -linkpkg -g lwt_server.ml
* http://baturin.org/code/lwt-counter-server/
* https://mirage.io/wiki/tutorial-lwt
* http://aubedesheros.blogspot.fr/2011/05/ocaml-lwt-mini-tutorial.html
*)
let host = Unix.inet_addr_loopback (* 127.0.0.1 *)
let port = 6600
let max_pending_request = 10
(* Initialize logging capabilities at INFO level *)
let () = Lwt_log.add_rule "*" Lwt_log.Info
let send_message oc =
Lwt_io.write_line oc "test"
let accept_connection conn =
let fd, _ = conn in
let oc = Lwt_io.of_fd Lwt_io.Output fd in
Lwt.on_failure (send_message oc ) (fun e -> Lwt_log.ign_error (Printexc.to_string e));
Lwt_log.info "New connection" >>= return
let create_server sock =
let serve () =
Lwt_unix.accept sock >>= accept_connection
in serve
let sock_recv sock maxlen =
let str = Bytes.create maxlen in
let recvlen = recv sock str 0 maxlen [] in
String.sub str 0 recvlen
let sock_read sock =
let answer = sock_recv sock 512 in
Lwt_io.write_line Lwt_io.stdout answer
let create_socket () =
let sock = Lwt_unix.socket PF_INET SOCK_STREAM 0 in
Lwt_unix.bind sock @@ ADDR_INET(host, port);
Lwt_unix.listen sock max_pending_request;
sock
let () =
let sock = create_socket () in
let threads = Lwt.join [create_server sock; sock_read sock] in
Lwt_main.run threads
这是我在oder中用来编译它的命令和错误消息
Here is the command I use in oder to compile it and the error message
ocamlfind ocamlc -o lwt_server -package lwt,lwt.unix,unix -linkpkg -g lwt_server.ml
File "lwt_server.ml", line 53, characters 28-46:
Error: This expression has type unit -> unit Lwt.t
but an expression was expected of type unit Lwt.t
第53行是指(不开玩笑,我已经检查了两次):
The line 53 refers to (no joke, I have checked twice):
let threads = Lwt.join [create_server sock; sock_read sock] in
这是我第一次使用Lwt,我不确定这是否是我应该与OUnit一起使用的解决方案.
It is the first time I use Lwt, I am not sure if it is the solution I should use with OUnit.
推荐答案
问题在于,create_server sock
返回类型为unit -> unit Lwt.t
的另一个函数,而不是类型错误所指示的Lwt线程(unit Lwt.t
):
The issue is that create_server sock
returns another function of type unit -> unit Lwt.t
rather than an Lwt thread (unit Lwt.t
) as indicated in the type error:
let create_server sock =
let serve () =
Lwt_unix.accept sock >>= accept_connection
in serve (* serve is a function, not a thread *)
您可以重写此代码以返回线程,而不是用于修复此类型错误的函数:
You can rewrite this to return a thread rather than a function to fix your this type error:
let create_server sock =
Lwt_unix.accept sock >>= accept_connection
我还没有对代码进行整体测试,但这应该可以解决类型错误.
I haven't tested the code as a whole, but this should fix the type error.
这篇关于OCaml:预期Lwt表达的类型单位为Lwt.t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!