<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=big5">
<META content="MSHTML 6.00.6000.16544" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>The first version will stop to receive data.</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>30> 
redir:start(8001,www.program.com.tw,80).<BR><0.27388.1><BR></DIV>
<DIV>When i connect to 127.0.0.1 8001.</DIV>
<DIV>31> 3.closed</DIV>
<DIV> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>But the next code can work correctly. </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>============================================================</DIV>
<DIV>-module(redir).<BR>-export([start/3,accept/3,doLocalRecv/2,doRemoteRecv/2,disPatch/3]).<BR>-define(TCP_OPTIONS,[list, 
{packet, 0}, {active, false}, {reuseaddr, true}]).</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>start(LPort,RHost,RPort)-><BR>     case 
gen_tcp:listen(LPort,?TCP_OPTIONS) 
of<BR>                  
{ok, 
ListenSocket}->spawn(redir,accept,[ListenSocket,RHost,RPort]);<BR>                  
{error,Why}->io:format("socket 
error0:~p~n",[Why])<BR>                
end.</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>accept(ListenSocket,RHost,RPort) -> <BR>case 
gen_tcp:accept(ListenSocket) 
of<BR>                
{ok, AcceptSocket}-><FONT 
color=#ff0000>spawn(redir,disPatch,[AcceptSocket,RHost,RPort]);<BR></FONT>                
{error,Why}->io:format("socket 
error1:~p~n",[Why])<BR>                 
end,    <BR>        
accept(ListenSocket,RHost,RPort).</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT 
color=#ff0000>disPatch(AcceptSocket,RHost,RPort)-><BR>                              
case gen_tcp:connect(RHost,RPort,?TCP_OPTIONS) 
of<BR>                               
%%   ConnectSocket:server to remote ;AcceptSocket:client to server 
<BR>                              
{ok,ConnectSocket}-><BR>                                  
spawn(redir,doRemoteRecv,[ConnectSocket,AcceptSocket]), 
<BR>                                  
spawn(redir,doLocalRecv,[AcceptSocket,ConnectSocket]);<BR>                              
{error,Reason}->io:format("1.~p~n",[Reason])<BR>                               
end.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>doLocalRecv(AcceptSocket,ConnectSocket) -> <BR>   case 
gen_tcp:recv(AcceptSocket,0) of<BR>        
{ok,Packet} 
->gen_tcp:send(ConnectSocket,Packet),doLocalRecv(AcceptSocket,ConnectSocket);<BR>        
{error,Reason}->io:format("2.~p~n",[Reason])<BR>     
end.    </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>doRemoteRecv(ConnectSocket,AcceptSocket)-> <BR>    case 
gen_tcp:recv(ConnectSocket,0) of<BR>        
{ok,Packet} 
->gen_tcp:send(AcceptSocket,Packet),doRemoteRecv(ConnectSocket,AcceptSocket);<BR>        
{error,Reason}->io:format("3.~p~n",[Reason])<BR>     
end.        </DIV>
<DIV>============================================================</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>-module(redir).<BR>-export([start/3,accept/3,doLocalRecv/2,doRemoteRecv/2]).<BR>-define(TCP_OPTIONS,[list, 
{packet, 0}, {active, false}, {reuseaddr, true}]).</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>start(LPort,RHost,RPort)-><BR>     case 
gen_tcp:listen(LPort,?TCP_OPTIONS) 
of<BR>                  
{ok, 
ListenSocket}->                             
<BR><FONT 
color=#ff0000>                             
case gen_tcp:connect(RHost,RPort,?TCP_OPTIONS) 
of<BR>                               
%%   ConnectSocket:server to remote ;AcceptSocket:client to server 
<BR>                              
{ok,ConnectSocket}-><BR>                                  
spawn(redir,doRemoteRecv,[ConnectSocket,AcceptSocket]), 
<BR>                                  
spawn(redir,doLocalRecv,[AcceptSocket,ConnectSocket]);<BR>                              
{error,Reason}->io:format("1.~p~n",[Reason])<BR>                               
end;<BR></FONT>                  
{error,Why}->io:format("socket 
error0:~p~n",[Why])<BR>                
end.</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>accept(ListenSocket,RHost,RPort) -> <BR>case 
gen_tcp:accept(ListenSocket) 
of<BR>                
{ok, 
AcceptSocket}->spawn(redir,disPatch,[AcceptSocket,RHost,RPort]);<BR>                
{error,Why}->io:format("socket 
error1:~p~n",[Why])<BR>                 
end,    <BR>        
accept(ListenSocket,RHost,RPort).</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>doLocalRecv(AcceptSocket,ConnectSocket) -> <BR>   case 
gen_tcp:recv(AcceptSocket,0) of<BR>        
{ok,Packet} 
->gen_tcp:send(ConnectSocket,Packet),doLocalRecv(AcceptSocket,ConnectSocket);<BR>        
{error,Reason}->io:format("2.~p~n",[Reason])<BR>     
end.    </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>doRemoteRecv(ConnectSocket,AcceptSocket)-> <BR>    case 
gen_tcp:recv(ConnectSocket,0) of<BR>        
{ok,Packet} 
->gen_tcp:send(AcceptSocket,Packet),doRemoteRecv(ConnectSocket,AcceptSocket);<BR>        
{error,Reason}->io:format("3.~p~n",[Reason])<BR>     
end.        </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV></BODY></HTML>