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

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    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.
    Why am I having to set up port forwarding for this to work ?-x-png

    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.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    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, 08: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