forked from elixir-lang/elixir
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
% Start the server in examples/gen_tcp_server.ex and then run this file | ||
Code.require "gen_tcp" | ||
|
||
{'ok, tcp} = GenTCP.connect("localhost", 5678, ['binary, {'packet, 0}]) | ||
'ok = tcp.send("Some Data") | ||
'ok = tcp.close | ||
|
||
'ok = tcp.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
Code.require "gen_tcp" | ||
|
||
module Server | ||
|
||
def do_recv(tcp, sock, bs) | ||
case tcp.recv(sock,0) | ||
match {'ok, b} | ||
do_recv(tcp, sock, [bs, b]) | ||
match {'error, 'closed} | ||
{'ok, Erlang.list_to_binary(bs)} | ||
end | ||
end | ||
|
||
def start | ||
{'ok, tcp} = GenTCP.listen(5678, ['binary, {'packet, 0}, {'active, false}]) | ||
|
||
{'ok, sock} = tcp.accept | ||
{'ok, bin} = do_recv(tcp, sock, []) | ||
{'ok, bin} = recv(tcp, sock, []) | ||
'ok = tcp.close | ||
IO.puts bin.to_s | ||
end | ||
|
||
private | ||
|
||
def recv(tcp, sock, bs) | ||
case tcp.recv(sock,0) | ||
match {'ok, b} | ||
recv(tcp, sock, [bs, b]) | ||
match {'error, 'closed} | ||
{'ok, bs.to_bin} | ||
end | ||
end | ||
end | ||
|
||
Server.start | ||
Server.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,103 @@ | ||
% Interface to Erlang's gen_tcp. Here is a simple example of client/server: | ||
% | ||
% Code.require "gen_tcp" | ||
% | ||
% module Server | ||
% def start | ||
% {'ok, tcp} = GenTCP.listen(5678, ['binary, {'packet, 0}, {'active, false}]) | ||
% {'ok, sock} = tcp.accept | ||
% {'ok, bin} = recv(tcp, sock, []) | ||
% 'ok = tcp.close | ||
% IO.puts bin.to_s | ||
% end | ||
% | ||
% private | ||
% | ||
% def recv(tcp, sock, bs) | ||
% case tcp.recv(sock,0) | ||
% match {'ok, b} | ||
% recv(tcp, sock, [bs, b]) | ||
% match {'error, 'closed} | ||
% {'ok, bs.to_bin} | ||
% end | ||
% end | ||
% end | ||
% | ||
% Server.start | ||
% | ||
% After starting the server file above, add the client below to a file (or run it | ||
% directly from iex): | ||
% | ||
% Code.require "gen_tcp" | ||
% {'ok, tcp} = GenTCP.connect("localhost", 5678, ['binary, {'packet, 0}]) | ||
% 'ok = tcp.send("Some Data") | ||
% 'ok = tcp.close | ||
% | ||
% You will see the server will print <<"Some Data">> (as binary) and then exit. | ||
object GenTCP | ||
module Mixin | ||
def listen(port, options) | ||
case Erlang.gen_tcp.listen(port, options) | ||
match { 'ok, sock } | ||
{ 'ok, GenTCP.new(sock) } | ||
match other | ||
other | ||
end | ||
init_socket Erlang.gen_tcp.listen(port, options) | ||
end | ||
def listen(port, options, timeout) | ||
case Erlang.gen_tcp.listen(port, options, timeout) | ||
match { 'ok, sock } | ||
{ 'ok, GenTCP.new(sock) } | ||
match other | ||
other | ||
end | ||
init_socket Erlang.gen_tcp.listen(port, options, timeout) | ||
end | ||
def connect(address, port, options) | ||
connect(address, port, options, 0) | ||
end | ||
init_socket Erlang.gen_tcp.connect(address.to_char_list, port, options) | ||
end | ||
def connect(address, port, options, timeout) | ||
case Erlang.gen_tcp.connect(address.to_char_list, port, options, timeout) | ||
match { 'ok, sock } | ||
{ 'ok, GenTCP.new(sock) } | ||
init_socket Erlang.gen_tcp.connect(address.to_char_list, port, options, timeout) | ||
end | ||
private | ||
def init_socket(result) | ||
case result | ||
match { 'ok, socket } | ||
{ 'ok, GenTCP.new(socket) } | ||
match other | ||
other | ||
end | ||
end | ||
|
||
end | ||
end | ||
def constructor(sock) | ||
{'sock: sock} | ||
def constructor(socket) | ||
{'socket: socket} | ||
end | ||
|
||
def controlling_process(pid) | ||
Erlang.gen_tcp.controlling_process(@sock, pid) | ||
Erlang.gen_tcp.controlling_process(@socket, pid) | ||
end | ||
|
||
def recv(sock, length) | ||
Erlang.gen_tcp.recv(sock, length) | ||
def recv(socket, length) | ||
Erlang.gen_tcp.recv(socket, length) | ||
end | ||
|
||
def recv(sock, length, timeout) | ||
Erlang.gen_tcp.recv(sock, length, timeout) | ||
def recv(socket, length, timeout) | ||
Erlang.gen_tcp.recv(socket, length, timeout) | ||
end | ||
|
||
def accept | ||
Erlang.gen_tcp.accept(@sock) | ||
Erlang.gen_tcp.accept(@socket) | ||
end | ||
|
||
def accept(timeout) | ||
Erlang.gen_tcp.accept(@sock, timeout) | ||
Erlang.gen_tcp.accept(@socket, timeout) | ||
end | ||
|
||
def send(message) | ||
Erlang.gen_tcp.send(@sock, message.to_bin) | ||
Erlang.gen_tcp.send(@socket, message.to_bin) | ||
end | ||
|
||
def close | ||
Erlang.gen_tcp.close(@sock) | ||
end | ||
Erlang.gen_tcp.close(@socket) | ||
end | ||
|
||
def shutdown | ||
Erlang.gen_tcp.shutdown(@sock) | ||
Erlang.gen_tcp.shutdown(@socket) | ||
end | ||
end | ||
|