newbie erlang programmer has a newbie problem.. :)

Ulf Wiger etxuwig@REDACTED
Mon Nov 20 10:40:05 CET 2000


Hi.

It does seem to work as written, but you probably want to do something
with the tcp connection as well.

To run your program, I started two erlang shells: one to run
server:start(8805), and one to run

gen_tcp:connect({127,0,0,1},8805,[binary,{packet,0}]).

In the first shell, the following output occurred:

1> server:start(8805).
<0.85.0>
welcome to SuperServer 0.0
you are user #1

Of course, this is all the program can do at the moment.
Below is an example of your program extended to simply echo the
caller's input:

-module (server).
-export ([start/1,beginserver/1]).

-export([server/1]).

start(Port) ->
    proc_lib:spawn_link(server, beginserver, [Port]).

beginserver(Port) ->
    {ok, Lsock} = gen_tcp:listen(Port, [binary, {packet, 0},
					{active, false},
					{backlog, 4}]),
    accept_loop(Lsock,1).

accept_loop(Lsock, Nuser) ->
    {ok, Sock} = gen_tcp:accept(Lsock),
    WelcomeStr = ["welcome to SuperServer 0.0\nyou are user #",
		  integer_to_list(Nuser), "\n"],
    io:format (WelcomeStr),
    gen_tcp:send(Sock, WelcomeStr),
    Pid = proc_lib:spawn(server, server, [Sock]),
    gen_tcp:controlling_process(Sock, Pid),
    Pid ! continue,
    accept_loop(Lsock, Nuser+1).

server(Sock) ->
    %% the 'continue' message is simply to make absolutely sure that
    %% this process can't enter serverloop before the call to
    %% controlling_process/2
    receive
	continue ->
	    serverloop(Sock)
    after 10000 ->
	    exit(timeout)
    end.


serverloop(Sock) ->
    case gen_tcp:recv(Sock, 0) of
	{ok, B} ->
	    gen_tcp:send(Sock, ["you wrote: ", B]),
	    serverloop(Sock);
	{error, closed} ->
	    exit(normal)
    end.


In my second shell, I get the following:

1> {ok,Sock} = gen_tcp:connect({127,0,0,1},8805,[binary,{packet,0}]).
{ok,{socket,<0.51.0>,#Port<0.11>,inet_tcp}}
2> gen_tcp:send(Sock,"hello").
ok
3> receive {tcp,Sock,Bin} -> binary_to_list(Bin) after 0 -> nothing
end.
"you wrote: hello"
4> gen_tcp:send(Sock,"hello again").                                    
ok
5> f(Bin),receive {tcp,Sock,Bin} -> binary_to_list(Bin) after 0 ->
nothing end. 
"you wrote: hello again"



On Sun, 19 Nov 2000, INTERFAC wrote:

>hi all,
>
>first of all, greetings to all people in the list, this is my first 
>email..
>
>What i'm trying is a very simple program, thought i cannot resolve, 
>since i've just started with erlang. Its a tcp server that only has to 
>tell to the clients that "you are user number X", and when there are 4 
>users connected, reject them with a message like: "there are finnaly 4 
>users, bye!".
>
>i have an initial code like this:
>--------------------------
>
>-module (server).
>-export ([start/1,beginserver/1]).
>
>start(Port) ->
>    spawn(server, beginserver, [Port]).
>
>beginserver(Port) ->
>    {ok, Lsock} = gen_tcp:listen(Port, [binary, {packet, 0},{active, 
>false},{backlog,4}]),
>    serverloop(Lsock,1).
>
>serverloop(Lsock, Nuser) ->
>    {ok, Sock} = gen_tcp:accept(Lsock),
>    io:format ("welcome to SuperServer 0.0\nyou are user #~w\n",
>[Nuser]),
>    serverloop(Lsock, Nuser+1).
>
>--------------
>but doesnt works. Anyone could help me? 
>
>thanks in advance,
>Diego Fernandez
>Spain
>
> ___________________________________________________________________ 
>Consigue tu e-mail gratuito TERRA.ES
> Haz click en http://www.terra.es/correo/
>
>

-- 
Ulf Wiger                                    tfn: +46  8 719 81 95
Senior System Architect                      mob: +46 70 519 81 95
Strategic Product & System Management    ATM Multiservice Networks
Data Backbone & Optical Services Division      Ericsson Telecom AB





More information about the erlang-questions mailing list