Socket Send and Receive, Same local port, Different Remote Port

Discussion in 'Perl Misc' started by jeffburgoon@hotmail.com, Mar 1, 2005.

  1. Guest

    I'm having a problem opening a socket and receiving a response from a
    different IP/port than what I'm sending to. Here is what I'm trying to
    do:

    Open Socket with IP/Port (A,B)
    Send to location with IP/Port (C,D)
    Immediately receive response on IP/Port (A,B) from IP/Port (X,Y), or
    any IP/Port other than (C,D)

    My problem is that in order for me to send to (C,D) I have to create
    the socket with peer address/port (C,D), so only responses from (C,D)
    are allowed. I have tried setting up the socket to use (C,D), then
    tearing it down and recreating it with no peer IP/port specified but I
    think this is too slow, as the response comes back almost immediately
    and is missed by the recv() function. Here is what I'm trying:

    $socket=new IO::Socket::INET->new(PeerPort=>$dst_port_num
    ,Proto=>$'udp'
    ,PeerAddr=>$dst_addr_str
    ,LocalPort=>5060
    ,Reuse=>1);
    $socket->send($packet_text);
    close($socket);
    $socket->recv($response,1024);
    close($socket);

    The above code works as long as the response comes back from ip/port
    ($dst_addr_str, $dst_port_num). If a packet is received from a
    different IP or port, it is ignored as the socket is only listening on
    that pair. I have tried adding in the code to recreate the socket but
    it appears to be too slow as the response is never received. Here is
    what I add in, directly above the recv() line in the above code:

    $socket=new IO::Socket::INET->new(Proto=>$protocol_str
    ,LocalPort=>5060);


    I'm thinking what I need is to either

    A) Allocate a socket with no remote port/ip and instead specify port/ip
    in send()
    or
    B) Change peer IP/port to nothing on the fly immediately after sending
    packet.

    Any help would be greatly appreciated.
    , Mar 1, 2005
    #1
    1. Advertising

  2. Toni Erdmann Guest

    wrote:
    > I'm having a problem opening a socket and receiving a response from a
    > different IP/port than what I'm sending to. Here is what I'm trying to
    > do:
    >
    > Open Socket with IP/Port (A,B)
    > Send to location with IP/Port (C,D)
    > Immediately receive response on IP/Port (A,B) from IP/Port (X,Y), or
    > any IP/Port other than (C,D)
    >
    > My problem is that in order for me to send to (C,D) I have to create
    > the socket with peer address/port (C,D), so only responses from (C,D)
    > are allowed. I have tried setting up the socket to use (C,D), then
    > tearing it down and recreating it with no peer IP/port specified but I
    > think this is too slow, as the response comes back almost immediately
    > and is missed by the recv() function. Here is what I'm trying:
    >
    > $socket=new IO::Socket::INET->new(PeerPort=>$dst_port_num


    leave PeerPort undef

    > ,Proto=>$'udp'

    ^
    does this realy work? ----------------------|

    > ,PeerAddr=>$dst_addr_str


    leave PeerAddr undef

    > ,LocalPort=>5060
    > ,Reuse=>1);
    > $socket->send($packet_text);


    use $socket->send( BUF, FLAGS, TO );

    > close($socket);


    why do you close the socket?

    > $socket->recv($response,1024);


    Can you receive on a closed socket?
    If your are using SIP, take at least 1500 (IP's MTU on Ethernet)
    as buffer size.

    On the other side 65535 is the max UDP packet size.
    Why don't you use this value? Then a SIP messages arrives completely
    in $response (you don't have to take care about fragmentation).

    But then again SIP defines TCP to be used if the SIP messages is
    longer than MTU-200. So with more then 1300 bytes, you should
    use and be able to receive from TCP.

    But, if your implementing a SIP client that uses a SIP proxy,
    it should be OK to connect() this socket to communicate only
    with the SIP proxy.

    But, as a SIP client, you could use any LocalPort. 5060 is
    recommended but not mandatory for SIP clients.

    But, as a SIP proxy, you should use 5060 and be prepared to
    receive SIP messages from any SIP client.

    > close($socket);
    >
    > The above code works as long as the response comes back from ip/port
    > ($dst_addr_str, $dst_port_num). If a packet is received from a


    Yes, because by specifying 'PeerAddr'and 'PeerPort', even a UDP
    socket performs a 'Socket::connect()' and therefor is dedicated to
    communicate with (receive from) PeerAddr:peerPort only.

    > different IP or port, it is ignored as the socket is only listening on
    > that pair. I have tried adding in the code to recreate the socket but
    > it appears to be too slow as the response is never received. Here is
    > what I add in, directly above the recv() line in the above code:
    >
    > $socket=new IO::Socket::INET->new(Proto=>$protocol_str
    > ,LocalPort=>5060);
    >
    >
    > I'm thinking what I need is to either
    >
    > A) Allocate a socket with no remote port/ip and instead specify port/ip
    > in send()
    > or
    > B) Change peer IP/port to nothing on the fly immediately after sending
    > packet.
    >
    > Any help would be greatly appreciated.
    >


    Toni "use Socket;" User
    Toni Erdmann, Mar 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander N. Spitzer
    Replies:
    21
    Views:
    1,745
    Randolf Richardson
    Aug 16, 2004
  2. Steve Holden
    Replies:
    0
    Views:
    758
    Steve Holden
    Apr 11, 2008
  3. terry
    Replies:
    5
    Views:
    865
    Gabriel Genellina
    May 3, 2008
  4. Christopher Brewster
    Replies:
    5
    Views:
    330
    John Machin
    Nov 14, 2008
  5. Rémi
    Replies:
    0
    Views:
    165
    Rémi
    Jul 9, 2011
Loading...

Share This Page