Erlang和Ruby的Socket通讯
生活随笔
收集整理的這篇文章主要介紹了
Erlang和Ruby的Socket通讯
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
server.erl?
Java代碼?-module(server).?? -export([start/0,start/1,process/1]).?? -define(defPort,?8888).?? ?? start()?->?start(?defPort).?? ?? start(Port)?->?? ??case?gen_tcp:listen(Port,?[binary,?{packet,?0},?{active,?false}])?of?? ????{ok,?LSock}?->?server_loop(LSock);?? ????{error,?Reason}?->?exit({Port,Reason})?? ??end.?? ?? %%?main?server?loop?-?wait?for?next?connection,?spawn?child?to?process?it?? server_loop(LSock)?->?? ??case?gen_tcp:accept(LSock)?of?? ????{ok,?Sock}?->?? ??????spawn(?MODULE,process,[Sock]),?? ??????server_loop(LSock);?? ????{error,?Reason}?->?? ??????exit({accept,Reason})?? ??end.?? ?? %%?process?current?connection?? process(Sock)?->?? ??Req?=?do_recv(Sock),?? ??Resp?=?"Hello,?"?++?Req?++?"!",?? ??do_send(Sock,Resp),?? ??gen_tcp:close(Sock).?? ?? %%?send?a?line?of?text?to?the?socket?? do_send(Sock,Msg)?->?? ??case?gen_tcp:send(Sock,?Msg)?of?? ????ok?->?ok;?? ????{error,?Reason}?->?exit(Reason)?? ??end.?? ?? %%?receive?data?from?the?socket?? do_recv(Sock)?->?? ??case?gen_tcp:recv(Sock,?0)?of?? ????{ok,?Bin}?->?binary_to_list(Bin);?? ????{error,?closed}?->?exit(closed);?? ????{error,?Reason}?->?exit(Reason)?? ??end.??
client.rb?
Java代碼?require?'socket'?? ?? client?=?TCPSocket.open('localhost',?8888)?? client.send("hideto",?0)?? puts?client.readlines?? client.close??
Erlang服務(wù)器編譯運(yùn)行:?
Java代碼?Eshell?>?c(server.erl).?? Eshell?>?server:start().??
Ruby客戶端解釋運(yùn)行:?
Java代碼?>?ruby?client.rb?? Hello,?hideto!??
Java代碼?
client.rb?
Java代碼?
Erlang服務(wù)器編譯運(yùn)行:?
Java代碼?
Ruby客戶端解釋運(yùn)行:?
Java代碼?
轉(zhuǎn)載于:https://www.cnblogs.com/orez88/articles/1800787.html
總結(jié)
以上是生活随笔為你收集整理的Erlang和Ruby的Socket通讯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP超级链接和HTML函数正则表达式
- 下一篇: 过简单的研究生生活