Why am I having to set up port forwarding for this to work ?

This is a discussion on Why am I having to set up port forwarding for this to work ? within the Networking/Device Communication forums, part of the General Programming Boards category; I'm sending and receiving 10 megabytes of UDP data (for testing the speed!) with the given pair of programs. But ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Why am I having to set up port forwarding for this to work ?

    I'm sending and receiving 10 megabytes of UDP data (for testing the speed!) with the given pair of programs.

    But for them to work, I have to set up my home router to know about the port.
    Name:  x.png
Views: 262
Size:  17.3 KB

    But AFAIK, numerous other programs (eg: torrent clients) operate in the same way and do not need this to be set.
    Most even allow the port no.to be randomized.

    What do I have to do to avoid doing this ?

    Sender:
    Code:
    #include<Poco/Net/DatagramSocket.h>
    #include<Poco/Net/SocketAddress.h>
    #include<Poco/Net/NetException.h>
    
    #include<iostream>
    #include<cstring>
    int main()
    {
        try
        {
    
            Poco::Net::DatagramSocket dgs;
            
            for(int i=0;i<1024*10;++i)
            {
                char data[1024];
                const char* num=std::to_string(i).c_str();
                std::strncpy(data+100,num,7);
                
                dgs.sendTo(data,1024,{"172.16.150.194",7479});
                
                std::cout<<"Sent :"<<data+100<<"\n";
                
            }
        }
        catch(const Poco::Net::NetException& e)
        {
            std::cout<<e.what()<<e.displayText();
        }
        
        return 0;
    }
    Receiver:
    Code:
    #include<Poco/Net/DatagramSocket.h>
    #include<Poco/Net/SocketAddress.h>
    #include<Poco/Net/NetException.h>
    
    #include<iostream>
    #include<chrono>
    
    int main()
    {
        try
        {
    
            Poco::Net::SocketAddress sa(Poco::Net::IPAddress(),7479);
            Poco::Net::DatagramSocket dgs(sa);
            
            bool flag=false;
            std::chrono::time_point<std::chrono::system_clock> start, end;
            for(int i=0;i<1024*10;++i)
            {
                char data[1024];
                Poco::Net::SocketAddress sender;
                
                dgs.receiveFrom(data,1024,sender);
    
                if(!flag)
                {
                    flag=true;
                    start=std::chrono::system_clock::now();
                }
                
                
                end=std::chrono::system_clock::now();
                float speed=(1000.0*i)/std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
                std::cout<<"Received: "<<data+100<<". Current speed: "<<speed<<" KB/s.\n"; 
            }
            
        
        
        }
        catch(const Poco::Net::NetException& e)
        {
            std::cout<<e.what()<<e.displayText();
        }
        
        return 0;
    }
    (I know this is not a good way to write UDP code, but in this case I do not care if some of the chunks do not make it through.
    Just getting an idea of the average speed would be enough. )
    Last edited by manasij7479; 10-14-2013 at 01:01 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,682
    The little I remember is often UDP is off by default going though network devices.
    And, the direction of packet matters; some will only accept incoming packets after seeing outgoing packets.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    On some routers you can control how UDP and TCP packets are handled. You can set UDP to be address and port restricted on my DIR-655.

    Were you thinking of opening a port through the router by way of UPnP? I thought that's how most torrent programs and things like Skype do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No port forwarding, question.
    By TJMaster in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-13-2010, 07:21 PM
  2. Port Forwarding 2 routers
    By cerin in forum Tech Board
    Replies: 1
    Last Post: 06-14-2009, 10:47 AM
  3. Port Forwarding
    By cerin in forum Tech Board
    Replies: 1
    Last Post: 04-05-2007, 03:41 PM
  4. uTorrent, routers, and port forwarding.
    By psychopath in forum Tech Board
    Replies: 6
    Last Post: 09-24-2006, 02:16 PM
  5. Port Forwarding DI-624
    By Tonto in forum Tech Board
    Replies: 0
    Last Post: 08-27-2006, 10:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21