<!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><FONT size=2>
<DIV><<Fix the source code>></DIV>
<DIV> </DIV>
<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>Why?                                             
</DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>But the next code can work correctly. </DIV></FONT></DIV>
<DIV><FONT size=2>
<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></FONT></DIV>
<DIV><FONT 
size=2>=========================================================================</FONT></DIV>
<DIV><FONT 
size=2>-module(redir).<BR>-export([start/3,accept/3,doLocalRecv/2,doRemoteRecv/2]).<BR>-define(TCP_OPTIONS,[list, 
{packet, 0}, {active, false}, {reuseaddr, 
true}]).<BR> <BR>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.<BR> <BR>accept(ListenSocket,RHost,RPort) -> <BR>case 
gen_tcp:accept(ListenSocket) 
of<BR>                
{ok, 
AcceptSocket}->                            
<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;<BR>                
{error,Why}->io:format("socket 
error1:~p~n",[Why])<BR>                 
end,  accept(ListenSocket,RHost,RPort).<BR> </FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> <BR>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.    
<BR> <BR>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.  </FONT></DIV>
<BLOCKQUOTE 
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style="FONT: 10pt 新細明體">----- Original Message ----- </DIV>
  <DIV 
  style="BACKGROUND: #e4e4e4; FONT: 10pt 新細明體; font-color: black"><B>From:</B> 
  <A title=luke@tc.program.com.tw href="mailto:luke@tc.program.com.tw">LUKE</A> 
  </DIV>
  <DIV style="FONT: 10pt 新細明體"><B>To:</B> <A title=erlang-questions@erlang.org 
  href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</A> 
  </DIV>
  <DIV style="FONT: 10pt 新細明體"><B>Sent:</B> Thursday, November 22, 2007 2:42 
  PM</DIV>
  <DIV style="FONT: 10pt 新細明體"><B>Subject:</B> [erlang-questions] programming 
  with sockets</DIV>
  <DIV><FONT size=2></FONT><BR></DIV>
  <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><FONT size=2></FONT> </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><BR><BR>__________ NOD32 2677 (20071122) 
  Information __________<BR><BR>This message was checked by NOD32 antivirus 
  system.<BR><A href="http://www.nod32.com.hk">http://www.nod32.com.hk</A><BR>
  <P>
  <HR>

  <P></P>_______________________________________________<BR>erlang-questions 
  mailing 
  list<BR>erlang-questions@erlang.org<BR>http://www.erlang.org/mailman/listinfo/erlang-questions<BR><BR>__________ 
  NOD32 2677 (20071122) Information __________<BR><BR>This message was checked 
  by NOD32 antivirus 
system.<BR>http://www.nod32.com.hk<BR><BR></BLOCKQUOTE></BODY></HTML>