[erlang-questions] programming with sockets

LUKE <>
Thu Nov 22 19:14:59 CET 2007


<<Fix the source code>>

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

Why?                                             

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}->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}->                            
                    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 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.  
  ----- Original Message ----- 
  From: LUKE 
  To:  
  Sent: Thursday, November 22, 2007 2:42 PM
  Subject: [erlang-questions] programming with sockets


  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.        






  __________ NOD32 2677 (20071122) Information __________

  This message was checked by NOD32 antivirus system.
  http://www.nod32.com.hk



------------------------------------------------------------------------------


  _______________________________________________
  erlang-questions mailing list
  
  http://www.erlang.org/mailman/listinfo/erlang-questions

  __________ NOD32 2677 (20071122) Information __________

  This message was checked by NOD32 antivirus system.
  http://www.nod32.com.hk

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


More information about the erlang-questions mailing list