The
The following code fragment provides a simple example of a client connecting to a server at port 5678, transferring a binary and closing the connection:
client() ->
SomeHostInNet = "localhost", % to make it runnable on one machine
{ok, Sock} = gen_tcp:connect(SomeHostInNet, 5678,
[binary, {packet, 0}]),
ok = gen_tcp:send(Sock, "Some Data"),
ok = gen_tcp:close(Sock).
At the other end a server is listening on port 5678, accepts the connection and receives the binary:
server() ->
{ok, LSock} = gen_tcp:listen(5678, [binary, {packet, 0},
{active, false}]),
{ok, Sock} = gen_tcp:accept(LSock),
{ok, Bin} = do_recv(Sock, []),
ok = gen_tcp:close(Sock),
Bin.
do_recv(Sock, Bs) ->
case gen_tcp:recv(Sock, 0) of
{ok, B} ->
do_recv(Sock, [Bs, B]);
{error, closed} ->
{ok, list_to_binary(Bs)}
end.
For more examples, see the
As returned by accept/1,2 and connect/3,4.
Connects to a server on TCP port
The available options are:
If the host has several network interfaces, this option specifies which one to use.
Same as
If a socket has somehow been connected without using
Set up the socket for IPv4.
Set up the socket for IPv6.
Specify which local port number to use.
Override which callback module used. Defaults to
See
Packets can be sent to the returned socket
{tcp, Socket, Data}
If the socket is closed, the following message is delivered:
{tcp_closed, Socket}
If an error occurs on the socket, the following message is delivered:
{tcp_error, Socket, Reason}
unless
The optional
The default values for options given to
Sets up a socket to listen on the port
If
The available options are:
Received
Received
If the host has several network interfaces, this option specifies which one to listen on.
Specify which local port number to use.
If a socket has somehow been connected without using
Same as
Set up the socket for IPv6.
Set up the socket for IPv4.
Override which callback module used. Defaults to
See
The returned socket
The default values for options given to
Accepts an incoming connection request on a listen socket.
Returns
Packets can be sent to the returned socket
{tcp, Socket, Data}
unless
It is worth noting that the
Sends a packet on a socket.
There is no
This function receives a packet from a socket in passive
mode. A closed socket is indicated by a return value
The
The optional
Assigns a new controlling process
Closes a TCP socket.
Immediately close a socket in one or two directions.
To be able to handle that the peer has done a shutdown on
the write side, the
The following example illustrates usage of the {active,once}
option and multiple accepts by implementing a server as a
number of worker processes doing accept on one single listen
socket. The start/2 function takes the number of worker
processes as well as a port number to listen for incoming
connections on. If
start(Num,LPort) ->
case gen_tcp:listen(LPort,[{active, false},{packet,2}]) of
{ok, ListenSock} ->
start_servers(Num,ListenSock),
{ok, Port} = inet:port(ListenSock),
Port;
{error,Reason} ->
{error,Reason}
end.
start_servers(0,_) ->
ok;
start_servers(Num,LS) ->
spawn(?MODULE,server,[LS]),
start_servers(Num-1,LS).
server(LS) ->
case gen_tcp:accept(LS) of
{ok,S} ->
loop(S),
server(LS);
Other ->
io:format("accept returned ~w - goodbye!~n",[Other]),
ok
end.
loop(S) ->
inet:setopts(S,[{active,once}]),
receive
{tcp,S,Data} ->
Answer = process(Data), % Not implemented in this example
gen_tcp:send(S,Answer),
loop(S);
{tcp_closed,S} ->
io:format("Socket ~w closed [~w]~n",[S,self()]),
ok
end.
A simple client could look like this:
client(PortNo,Message) ->
{ok,Sock} = gen_tcp:connect("localhost",PortNo,[{active,false},
{packet,2}]),
gen_tcp:send(Sock,Message),
A = gen_tcp:recv(Sock,0),
gen_tcp:close(Sock),
A.
The fact that the
Consider a process that receives data from a client process that
is to be forwarded to a server on the network. The process has
connected to the server via TCP/IP and does not get any acknowledge
for each message it sends, but has to rely on the send timeout
option to detect that the other end is unresponsive. We could use
the
...
{ok,Sock} = gen_tcp:connect(HostAddress, Port,
[{active,false},
{send_timeout, 5000},
{packet,2}]),
loop(Sock), % See below
...
In the loop where requests are handled, we can now detect send timeouts:
loop(Sock) ->
receive
{Client, send_data, Binary} ->
case gen_tcp:send(Sock,[Binary]) of
{error, timeout} ->
io:format("Send timeout, closing!~n",
[]),
handle_send_timeout(), % Not implemented here
Client ! {self(),{error_sending, timeout}},
%% Usually, it's a good idea to give up in case of a
%% send timeout, as you never know how much actually
%% reached the server, maybe only a packet header?!
gen_tcp:close(Sock);
{error, OtherSendError} ->
io:format("Some other error on socket (~p), closing",
[OtherSendError]),
Client ! {self(),{error_sending, OtherSendError}},
gen_tcp:close(Sock);
ok ->
Client ! {self(), data_sent},
loop(Sock)
end
end.
Usually it would suffice to detect timeouts on receive, as most
protocols include some sort of acknowledgment from the server,
but if the protocol is strictly one way, the