[erlang-questions] programming with sockets

LUKE <>
Thu Nov 22 07:42:05 CET 2007


The first version will stop to receive data.

30> redir:start(8001,www.program.com.tw,80).
<0.27388.1>

When i connect to 127.0.0.1 8001.
31> 3.closed
 

But the next code can work correctly. 

============================================================
-module(redir).
-export([start/3,accept/3,doLocalRecv/2,doRemoteRecv/2,disPatch/3]).
-define(TCP_OPTIONS,[list, {packet, 0}, {active, false}, {reuseaddr, true}]).

start(LPort,RHost,RPort)->
     case gen_tcp:listen(LPort,?TCP_OPTIONS) of
                  {ok, ListenSocket}->spawn(redir,accept,[ListenSocket,RHost,RPort]);
                  {error,Why}->io:format("socket error0:~p~n",[Why])
                end.

accept(ListenSocket,RHost,RPort) -> 
case gen_tcp:accept(ListenSocket) of
                {ok, AcceptSocket}->spawn(redir,disPatch,[AcceptSocket,RHost,RPort]);
                {error,Why}->io:format("socket error1:~p~n",[Why])
                 end,    
        accept(ListenSocket,RHost,RPort).

disPatch(AcceptSocket,RHost,RPort)->
                              case gen_tcp:connect(RHost,RPort,?TCP_OPTIONS) of
                               %%   ConnectSocket:server to remote ;AcceptSocket:client to server 
                              {ok,ConnectSocket}->
                                  spawn(redir,doRemoteRecv,[ConnectSocket,AcceptSocket]), 
                                  spawn(redir,doLocalRecv,[AcceptSocket,ConnectSocket]);
                              {error,Reason}->io:format("1.~p~n",[Reason])
                               end.

doLocalRecv(AcceptSocket,ConnectSocket) -> 
   case gen_tcp:recv(AcceptSocket,0) of
        {ok,Packet} ->gen_tcp:send(ConnectSocket,Packet),doLocalRecv(AcceptSocket,ConnectSocket);
        {error,Reason}->io:format("2.~p~n",[Reason])
     end.    

doRemoteRecv(ConnectSocket,AcceptSocket)-> 
    case gen_tcp:recv(ConnectSocket,0) of
        {ok,Packet} ->gen_tcp:send(AcceptSocket,Packet),doRemoteRecv(ConnectSocket,AcceptSocket);
        {error,Reason}->io:format("3.~p~n",[Reason])
     end.        
============================================================

-module(redir).
-export([start/3,accept/3,doLocalRecv/2,doRemoteRecv/2]).
-define(TCP_OPTIONS,[list, {packet, 0}, {active, false}, {reuseaddr, true}]).

start(LPort,RHost,RPort)->
     case gen_tcp:listen(LPort,?TCP_OPTIONS) of
                  {ok, ListenSocket}->                             
                             case gen_tcp:connect(RHost,RPort,?TCP_OPTIONS) of
                               %%   ConnectSocket:server to remote ;AcceptSocket:client to server 
                              {ok,ConnectSocket}->
                                  spawn(redir,doRemoteRecv,[ConnectSocket,AcceptSocket]), 
                                  spawn(redir,doLocalRecv,[AcceptSocket,ConnectSocket]);
                              {error,Reason}->io:format("1.~p~n",[Reason])
                               end;
                  {error,Why}->io:format("socket error0:~p~n",[Why])
                end.

accept(ListenSocket,RHost,RPort) -> 
case gen_tcp:accept(ListenSocket) of
                {ok, AcceptSocket}->spawn(redir,disPatch,[AcceptSocket,RHost,RPort]);
                {error,Why}->io:format("socket error1:~p~n",[Why])
                 end,    
        accept(ListenSocket,RHost,RPort).

doLocalRecv(AcceptSocket,ConnectSocket) -> 
   case gen_tcp:recv(AcceptSocket,0) of
        {ok,Packet} ->gen_tcp:send(ConnectSocket,Packet),doLocalRecv(AcceptSocket,ConnectSocket);
        {error,Reason}->io:format("2.~p~n",[Reason])
     end.    

doRemoteRecv(ConnectSocket,AcceptSocket)-> 
    case gen_tcp:recv(ConnectSocket,0) of
        {ok,Packet} ->gen_tcp:send(AcceptSocket,Packet),doRemoteRecv(ConnectSocket,AcceptSocket);
        {error,Reason}->io:format("3.~p~n",[Reason])
     end.        



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071122/b60c6243/attachment.html>


More information about the erlang-questions mailing list