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.
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. )