[erlang-questions] multiple process handling one udp listen socket ?

Sergej Jurecko sergej.jurecko@REDACTED
Sat Feb 2 16:14:41 CET 2013


Google "udp punch" if you can make it work with that.
{active,false} and multiple processes calling recv I very much doubt will work. Try it :)

You will definitely not be able to handle 1gbit on 1 socket process. Not even close. If there really is no other option, I would write the program in C, which would then send data to erlang over tcp. The C program would listen, then send the data to erlang over a pool of TCP connections.


Sergej

On Feb 2, 2013, at 3:56 PM, Morgan Segalis wrote:

> Hi Sergej, 
> 
> Thank you for your answer...
> Actually, I would like to listen to port 80 for firewall blocking issue, at least I'm sure that port 80 is not blocked.
> 
> Since the issue is for bypassing firewall, unfortunately, creating another socket to communicate with the client won't work (The socket has to be initiated from the client to pass)...
> 
> What I'm really worrying about is to handle bandwidth as fast as it can goes (1GB/s) without the thread (process) to be overwhelmed by it.
> 
> What if the udp open {active, false} is set ? could I open multiple process with the same socket and wait for data on this process with gen_recv ?
> 
> Le 2 févr. 2013 à 15:44, Sergej Jurecko a écrit :
> 
>> You can't listen on multiple processes. Do you really need all communication through a single port? This is generally not how protocols based on UDP work. 
>> If you really have to do it, you have to increase receive buffer for it. inet:setopts(Sock,[{recbuf,SomethingBig}]).
>> Do not do anything other than receive bytes on that socket/process. Open a new process and socket for processing and reply.
>> {active,true} is not a good idea on that process. False or once are safer options.
>> 
>> Sergej
>> 
>> On Feb 2, 2013, at 3:29 PM, Morgan Segalis wrote:
>> 
>>> Hi everyone,
>>> 
>>> I'm scratching my head with a particular problem...
>>> I would like to create my own udp relay server.
>>> 
>>> Right now my udp relay server works fine, but it is only using one process...
>>> 
>>> [CODE]
>>> 
>>> start_server() ->
>>>  spawn(?MODULE, sl, [80]).
>>> 
>>> sl(Port) ->
>>>  {ok, Socket} = gen_udp:open(Port, [binary]),
>>>  inet:setopts(Socket, [{active, true}]),
>>>  listen(Socket).
>>> 
>>> listen(Socket) ->
>>>  receive
>>> 	{udp, _ , Host, Port, <<0>>} ->
>>> 	    io:fwrite("Host: ~p~n", [Host]),
>>> 	    io:fwrite("DistantPort: ~p~n", [Port]),
>>> 	    io:fwrite("server received:~p~n",[Socket]),
>>> 	    io:fwrite("LocalPort: ~p~n", [inet:port(Socket)]),
>>> 	    Message = lists:flatten(io_lib:format("~p\0", [Port])),
>>> 	    Res = gen_udp:send(Socket, Host, Port, Message),
>>> 	    io:fwrite("Sent back: ~p // ~p~n", [Port, Res]),
>>> 	    listen(Socket);
>>> 	{udp, _, _, _, <<1:8, 
>>> 			 Ip1:1/big-unsigned-integer-unit:8, 
>>> 			 Ip2:1/big-unsigned-integer-unit:8, 
>>> 			 Ip3:1/big-unsigned-integer-unit:8, 
>>> 			 Ip4:1/big-unsigned-integer-unit:8, 
>>> 			 Port1:1/big-unsigned-integer-unit:8,
>>> 			 Port2:1/big-unsigned-integer-unit:8,
>>> 			 Bin/binary>>} ->
>>> 	    Ip = {Ip1, Ip2, Ip3, Ip4},
>>> 	    Port = Port1 * 256 + Port2,
>>> 	    gen_udp:send(Socket, Ip, Port, Bin),
>>> 	    listen(Socket);
>>> 	Any ->
>>> 	    listen(Socket)
>>>  end.
>>> 
>>> [/CODE]
>>> 
>>> I'm worrying that if multiple (hundred, thousand ?) clients are using it, the process will be overwhelmed...
>>> Since right now I'm only listening one port (right now 80), I do not seems to see how to make multiple process to handle the same socket. Since udp is quite different from tcp since it does not need to connect/accept connections.
>>> 
>>> I would like to know if there is a way to get multiple process to handle the listen socket Even if it means that each process is handling only one client (specific host ?)... The only thing is, that the listen port should be the same for all client...
>>> 
>>> Thank you for your help,
>>> 
>>> Morgan.
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>> 
> 




More information about the erlang-questions mailing list